[code-quality] Pylint Werkzeug plugin

Claudiu Popa pcmanticore at gmail.com
Sat Jun 27 16:57:26 CEST 2015


On Sat, Jun 27, 2015 at 8:18 AM, Joe Schafer <joesmoe10 at gmail.com> wrote:
> Hi all,
>
> I'm trying to extend pylint to recognize Werkzeug's import trickery.
>
> Werkzeug uses a variable called `all_by_module` in
> https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/__init__.py to
> lazily load modules.  Pylint doesn't recognize this scheme and flags
> statements like:
>
>     from werkzeug import secure_filename
>
> My attempt to fix the problem is at
> https://github.com/jschaf/pylint-werkzeug/blob/master/pylint_werkzeug/__init__.py
>
> I don't think I'm following an optimal path to success.  Basically, I'm
> trying to recreate Python's import logic and then generate an AST with
> astroid.  It's not going well.  The current solution recurses endlessly,
> since creating the AST seems to trigger the pylint transform plugin.  I'm
> looking for simpler or more robust alternatives to my current approach with
> imports.
>
> My problem seems somewhat related to the issue about changing flask.ext
> imports at https://bitbucket.org/logilab/astroid/issue/10/
>
> Do you have any suggestions?
>
> Thanks,
> Joe Schafer
>


Hi Joe,

You could try something similar to the six.moves transform:
https://bitbucket.org/logilab/astroid/src/0fa5de5411866d2d17ef0c6f43665f6463e83fa5/astroid/brain/pysix_moves.py?at=default#cl-239

Basically something along these lines:

   def werkzeug_transform():
       code = textwrap.dedent('''
       from werkzeug.useragents import UserAgent
       from werkzeug.http import parse_etags
      ....
       ''')
      return AstroidBuilder().string_build(code)

   astroid.register_module_extender(MANAGER, 'werkzeug', werkzeug_transform)


More information about the code-quality mailing list