Re: pyflakes and star imports

Manlio Perillo <[email protected]> Fri, 11 Jul 2008 21:33:25 +0200
Newsgroups gmane.comp.python.quotient.dev
Message-ID <[email protected]>
Jean-Paul Calderone ha scritto:
> [...]
> It's impossible to reliably determine the value of __all__ without 
> executing
> the code in the module.  It's possible to guess and probably be right 
> most of
> the time by looking at the AST very carefully.  If you want, you could 
> submit
> a patch for this.  

For the moment this is all I can write:

import sys
import compiler
from compiler import ast


tree = compiler.parseFile(sys.argv[1])
all = []

for node in [node for node in tree.node.nodes if isinstance(node, 
ast.Assign)]:
     if len(node.nodes) != 1:
         continue

     nameNode = node.nodes[0]
     if isinstance(nameNode, ast.AssName):
         name = nameNode.name
         expr = node.expr
         if name == '__all__':
             assert isinstance(expr, ast.List)
             for node in expr.nodes:
                 assert isinstance(node, ast.Const)
                 all.append(node.value)

print all

> Or you could break yourself of the bad habit of using 
> import
> *. :)
> 

This seems the best solution.

> Jean-Paul
> 


Thanks   Manlio Perillo