diff options
-rw-r--r-- | checkers/variables.py | 9 | ||||
-rw-r--r-- | test/input/func_module___dict__.py | 9 | ||||
-rw-r--r-- | test/messages/func_module___dict__.txt | 1 |
3 files changed, 18 insertions, 1 deletions
diff --git a/checkers/variables.py b/checkers/variables.py index dab949c..a3b1501 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -132,7 +132,14 @@ builtins. Remember that you should avoid to define new builtins when possible.' """visit module : update consumption analysis variable checks globals doesn't overrides builtins """ - self._to_consume = [(copy(node.locals), {}, 'module')] + mlocals = copy(node.locals) + # __dict__ is added to module's locals but not available in module's namespace + # (unlike __doc__, __name__, etc...). But take care __dict__ may be assigned + # somewhere in the module, so remove astng inserted nodes (having None lineno) + mlocals['__dict__'] = [n for n in mlocals['__dict__'] if n.lineno is not None] + if not mlocals['__dict__']: + del mlocals['__dict__'] + self._to_consume = [(mlocals, {}, 'module')] self._vars = [] for name, stmts in node.locals.items(): if name in ('__name__', '__doc__', '__file__', '__path__') \ diff --git a/test/input/func_module___dict__.py b/test/input/func_module___dict__.py new file mode 100644 index 0000000..0cf1550 --- /dev/null +++ b/test/input/func_module___dict__.py @@ -0,0 +1,9 @@ +"""http://www.logilab.org/ticket/6949.""" + +__revision__ = None + +print __dict__ is not None + +__dict__ = {} + +print __dict__ is not None diff --git a/test/messages/func_module___dict__.txt b/test/messages/func_module___dict__.txt new file mode 100644 index 0000000..4169824 --- /dev/null +++ b/test/messages/func_module___dict__.txt @@ -0,0 +1 @@ +E: 5: Using variable '__dict__' before assignment |