Pylint: jinja templates are handled as str instances

Created on 11 Mar 2015  路  6Comments  路  Source: PyCQA/pylint

Originally reported by: Florian Bruhin (BitBucket: The-Compiler, GitHub: @The-Compiler?)


Given this code using jinja2:

#!python
import jinja2

class Loader(jinja2.BaseLoader):

    def get_source(self, _env, _template):
        return 'Hello {{ var }}', None, True

env = jinja2.Environment(loader=Loader())
template = env.get_template('foo')
print(template.render(var='World'))

pylint says: Instance of 'str' has no 'render' member (no-member)

However, get_template() should always return a jinja2.Template according to the documentation, and also does here.


bug

Most helpful comment

Same problem.. with default loader

env = Environment(loader=FileSystemLoader('./src/templates'))
template = env.get_template('page.html')
template.render({})

All 6 comments

_Original comment by_ Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore):


This seems to be similar with the one regarding the configparser issue.

get_template is implemented as:

#!python

        if isinstance(name, Template):
            return name
        if parent is not None:
            name = self.join_path(name, parent)
        return self._load_template(name, self.make_globals(globals))

astroid will happily see that there's a return out there and it will not consider the if statement. Since the argument you provided was an actual string, it considers that name is a string and returns it. For some reason, the call to self._load_template can't be inferred, would be interesting to see why.

Since it will take some time until we'll have flow control understanding, would you like to work on a patch? ;-) Basically, we should add support for Environment.get_template, by providing a transform in astroid.brain. Probably the users of jinja2 and pylint will be happy about it.

It seems this got fixed (by no-member not being emitted if various types got inferred?)

Whoops, nevermind that last comment. I actually changed the code to call a wrapper function. :laughing:

Nope, not just yet, but I'll get to it in the following days.

Same problem.. with default loader

env = Environment(loader=FileSystemLoader('./src/templates'))
template = env.get_template('page.html')
template.render({})

Similar problem with add_extention. Code:

app.jinja_env.add_extension('jinja2.ext.do')

Error:

E: 23, 0: Method 'jinja_env' has no 'add_extension' member (no-member)

Info:

pip show pylint

Name: pylint
Version: 1.7.1
Location: /usr/local/lib/python2.7/dist-packages
Requires: configparser, backports.functools-lru-cache, six, astroid, isort, mccabe, singledispatch

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elirnm picture elirnm  路  3Comments

glmdgrielson picture glmdgrielson  路  3Comments

PCManticore picture PCManticore  路  3Comments

DevynCJohnson picture DevynCJohnson  路  3Comments

adamtheturtle picture adamtheturtle  路  3Comments