Pylint - Pylint unable to import flask.ext.wtf?


Question

I've got my Pylint install importing flask just fine. And with that same installation of flask, I have wtforms running just fine in my application. However, when I run Pylint on a file importing wtforms:

from flask.ext import wtf
from flask.ext.wtf import validators

class PostForm(wtf.Form):
    content = wtf.TextAreaField('Content', validators=[validators.Required()])

From Pylint I get:

E:  1,0: No name 'wtf' in module 'flask.ext'
E:  2,0: No name 'wtf' in module 'flask.ext'
F:  2,0: Unable to import 'flask.ext.wtf'

While searching around I found this discussion suggesting it may be because flask.ext libraries are actually just "shortcuts" to libraries.

Any idea of how I can fix this? Thank you much!

1
17
4/17/2013 1:34:07 PM

Accepted Answer

So flask.ext is actually a custom importer written by Armin in an awesome way. It allows people install extensions to flask in separate packages but import them in one consistent way. (Really you should go read the code for it. It is fantastic.) That said, apparently pylint doesn't appreciate the elegance (and this is actually a bug, in my opinion) but you're in luck. The easy way around this is to do the following

import flask_wtf as wtf
# The above is the equivalent line as:
# from flask.ext import wtf
from flask_wtf import validators
# This is the same as:
# from flask.ext.wtf import validators

This should make pylint happy. It isn't as nice as using flask.ext but you have to pick shutting up pylint or using elegant code, but you can't have both (right now).

25
8/30/2013 9:08:55 AM

Having been annoyed by this bug for a while, I created a pylint plugin to solve this issue. Code is at https://github.com/jschaf/pylint-flask

To enable pylint to 'see' the flask.ext modules do the following:

  1. pip install pylint-flask
  2. run pylint --load-plugins=pylint_flask <your module>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon