Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 869
golmschenk Avatar asked Apr 17 '13 13:04

golmschenk


2 Answers

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).

like image 172
Ian Stapleton Cordasco Avatar answered Nov 02 '22 21:11

Ian Stapleton Cordasco


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>
like image 22
Joe Avatar answered Nov 02 '22 19:11

Joe