4
Implement the central Checker class.
5
Also, it models the Bindings and Scopes.
12
PY2 = sys.version_info < (3, 0)
13
PY32 = sys.version_info < (3, 3) # Python 2.5 to 3.2
14
PY33 = sys.version_info < (3, 4) # Python 2.5 to 3.3
15
PY34 = sys.version_info < (3, 5) # Python 2.5 to 3.4
19
except AttributeError:
22
builtin_vars = dir(__import__('__builtin__' if PY2 else 'builtins'))
26
except ImportError: # Python 2.5
29
if 'decorator_list' not in ast.ClassDef._fields:
30
# Patch the missing attribute 'decorator_list'
31
ast.ClassDef.decorator_list = ()
32
ast.FunctionDef.decorator_list = property(lambda s: s.decorators)
34
from pyflakes import messages
38
def getNodeType(node_class):
39
# workaround str.upper() which is locale-dependent
40
return str(unicode(node_class.__name__).upper())
42
def getNodeType(node_class):
43
return node_class.__name__.upper()
45
# Python >= 3.3 uses ast.Try instead of (ast.TryExcept + ast.TryFinally)
47
def getAlternatives(n):
48
if isinstance(n, (ast.If, ast.TryFinally)):
50
if isinstance(n, ast.TryExcept):
51
return [n.body + n.orelse] + [[hdl] for hdl in n.handlers]
53
def getAlternatives(n):
54
if isinstance(n, ast.If):
56
if isinstance(n, ast.Try):
57
return [n.body + n.orelse] + [[hdl] for hdl in n.handlers]
60
LOOP_TYPES = (ast.While, ast.For)
62
LOOP_TYPES = (ast.While, ast.For, ast.AsyncFor)
65
class _FieldsOrder(dict):
66
"""Fix order of AST node fields."""
68
def _get_fields(self, node_class):
69
# handle iter before target, and generators before element
70
fields = node_class._fields
72
key_first = 'iter'.find
73
elif 'generators' in fields:
74
key_first = 'generators'.find
76
key_first = 'value'.find
77
return tuple(sorted(fields, key=key_first, reverse=True))
79
def __missing__(self, node_class):
80
self[node_class] = fields = self._get_fields(node_class)
86
Simplest required implementation of collections.Counter. Required as 2.6
87
does not have Counter in collections.
91
results[item] = results.get(item, 0) + 1
95
def iter_child_nodes(node, omit=None, _fields_order=_FieldsOrder()):
97
Yield all direct child nodes of *node*, that is, all fields that
98
are nodes and all items of fields that are lists of nodes.
100
for name in _fields_order[node.__class__]:
103
field = getattr(node, name, None)
104
if isinstance(field, ast.AST):
106
elif isinstance(field, list):
111
def convert_to_value(item):
112
if isinstance(item, ast.Str):
114
elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes):
116
elif isinstance(item, ast.Tuple):
117
return tuple(convert_to_value(i) for i in item.elts)
118
elif isinstance(item, ast.Num):
120
elif isinstance(item, ast.Name):
121
result = VariableKey(item=item)
127
return constants_lookup.get(
131
elif (not PY33) and isinstance(item, ast.NameConstant):
132
# None, True, False are nameconstants in python3, but names in 2
135
return UnhandledKeyType()
138
class Binding(object):
140
Represents the binding of a value to a name.
142
The checker uses this to keep track of which names have been bound and
143
which names have not. See L{Assignment} for a special type of binding that
144
is checked with stricter rules.
146
@ivar used: pair of (L{Scope}, node) indicating the scope and
147
the node that this binding was last used.
150
def __init__(self, name, source):
159
return '<%s object %r from line %r at 0x%x>' % (self.__class__.__name__,
164
def redefines(self, other):
165
return isinstance(other, Definition) and self.name == other.name
168
class Definition(Binding):
170
A binding that defines a function or a class.
174
class UnhandledKeyType(object):
176
A dictionary key of a type that we cannot or do not check for duplicates.
180
class VariableKey(object):
182
A dictionary key which is a variable.
184
@ivar item: The variable AST object.
186
def __init__(self, item):
189
def __eq__(self, compare):
191
compare.__class__ == self.__class__
192
and compare.name == self.name
196
return hash(self.name)
199
class Importation(Definition):
201
A binding created by an import statement.
203
@ivar fullName: The complete name given to the import statement,
204
possibly including multiple dotted components.
205
@type fullName: C{str}
208
def __init__(self, name, source, full_name=None):
209
self.fullName = full_name or name
211
super(Importation, self).__init__(name, source)
213
def redefines(self, other):
214
if isinstance(other, SubmoduleImportation):
215
# See note in SubmoduleImportation about RedefinedWhileUnused
216
return self.fullName == other.fullName
217
return isinstance(other, Definition) and self.name == other.name
219
def _has_alias(self):
220
"""Return whether importation needs an as clause."""
221
return not self.fullName.split('.')[-1] == self.name
224
def source_statement(self):
225
"""Generate a source statement equivalent to the import."""
226
if self._has_alias():
227
return 'import %s as %s' % (self.fullName, self.name)
229
return 'import %s' % self.fullName
232
"""Return import full name with alias."""
233
if self._has_alias():
234
return self.fullName + ' as ' + self.name
239
class SubmoduleImportation(Importation):
241
A binding created by a submodule import statement.
243
A submodule import is a special case where the root module is implicitly
244
imported, without an 'as' clause, and the submodule is also imported.
245
Python does not restrict which attributes of the root module may be used.
247
This class is only used when the submodule import is without an 'as' clause.
249
pyflakes handles this case by registering the root module name in the scope,
250
allowing any attribute of the root module to be accessed.
252
RedefinedWhileUnused is suppressed in `redefines` unless the submodule
253
name is also the same, to avoid false positives.
256
def __init__(self, name, source):
257
# A dot should only appear in the name when it is a submodule import
258
assert '.' in name and (not source or isinstance(source, ast.Import))
259
package_name = name.split('.')[0]
260
super(SubmoduleImportation, self).__init__(package_name, source)
263
def redefines(self, other):
264
if isinstance(other, Importation):
265
return self.fullName == other.fullName
266
return super(SubmoduleImportation, self).redefines(other)
272
def source_statement(self):
273
return 'import ' + self.fullName
276
class ImportationFrom(Importation):
278
def __init__(self, name, source, module, real_name=None):
280
self.real_name = real_name or name
282
if module.endswith('.'):
283
full_name = module + self.real_name
285
full_name = module + '.' + self.real_name
287
super(ImportationFrom, self).__init__(name, source, full_name)
290
"""Return import full name with alias."""
291
if self.real_name != self.name:
292
return self.fullName + ' as ' + self.name
297
def source_statement(self):
298
if self.real_name != self.name:
299
return 'from %s import %s as %s' % (self.module,
303
return 'from %s import %s' % (self.module, self.name)
306
class StarImportation(Importation):
307
"""A binding created by a 'from x import *' statement."""
309
def __init__(self, name, source):
310
super(StarImportation, self).__init__('*', source)
311
# Each star importation needs a unique name, and
312
# may not be the module name otherwise it will be deemed imported
313
self.name = name + '.*'
317
def source_statement(self):
318
return 'from ' + self.fullName + ' import *'
321
# When the module ends with a ., avoid the ambiguous '..*'
322
if self.fullName.endswith('.'):
323
return self.source_statement
328
class FutureImportation(ImportationFrom):
330
A binding created by a from `__future__` import statement.
332
`__future__` imports are implicitly used.
335
def __init__(self, name, source, scope):
336
super(FutureImportation, self).__init__(name, source, '__future__')
337
self.used = (scope, source)
340
class Argument(Binding):
342
Represents binding a name as an argument.
346
class Assignment(Binding):
348
Represents binding a name with an explicit assignment.
350
The checker will raise warnings for any Assignment that isn't used. Also,
351
the checker does not consider assignments in tuple/list unpacking to be
352
Assignments, rather it treats them as simple Bindings.
356
class FunctionDefinition(Definition):
360
class ClassDefinition(Definition):
364
class ExportBinding(Binding):
366
A binding created by an C{__all__} assignment. If the names in the list
367
can be determined statically, they will be treated as names for export and
368
additional checking applied to them.
370
The only C{__all__} assignment that can be recognized is one which takes
371
the value of a literal list containing literal strings. For example::
373
__all__ = ["foo", "bar"]
375
Names which are imported and not otherwise used but appear in the value of
376
C{__all__} will not have an unused import warning reported for them.
379
def __init__(self, name, source, scope):
380
if '__all__' in scope and isinstance(source, ast.AugAssign):
381
self.names = list(scope['__all__'].names)
384
if isinstance(source.value, (ast.List, ast.Tuple)):
385
for node in source.value.elts:
386
if isinstance(node, ast.Str):
387
self.names.append(node.s)
388
super(ExportBinding, self).__init__(name, source)
392
importStarred = False # set to True when import * is found
395
scope_cls = self.__class__.__name__
396
return '<%s at 0x%x %s>' % (scope_cls, id(self), dict.__repr__(self))
399
class ClassScope(Scope):
403
class FunctionScope(Scope):
405
I represent a name scope for a function.
407
@ivar globals: Names declared 'global' in this function.
410
alwaysUsed = set(['__tracebackhide__',
411
'__traceback_info__', '__traceback_supplement__'])
414
super(FunctionScope, self).__init__()
415
# Simplify: manage the special locals as globals
416
self.globals = self.alwaysUsed.copy()
417
self.returnValue = None # First non-empty return
418
self.isGenerator = False # Detect a generator
420
def unusedAssignments(self):
422
Return a generator for the assignments which have not been used.
424
for name, binding in self.items():
425
if (not binding.used and name not in self.globals
426
and not self.usesLocals
427
and isinstance(binding, Assignment)):
431
class GeneratorScope(Scope):
435
class ModuleScope(Scope):
436
"""Scope for a module."""
437
_futures_allowed = True
440
class DoctestScope(ModuleScope):
441
"""Scope for a doctest."""
444
# Globally defined names which are not attributes of the builtins module, or
445
# are only present on some platforms.
446
_MAGIC_GLOBALS = ['__file__', '__builtins__', 'WindowsError']
449
def getNodeName(node):
450
# Returns node.id, or node.name, or None
451
if hasattr(node, 'id'): # One of the many nodes with an id
453
if hasattr(node, 'name'): # an ExceptHandler node
457
class Checker(object):
459
I check the cleanliness and sanity of Python code.
461
@ivar _deferredFunctions: Tracking list used by L{deferFunction}. Elements
462
of the list are two-tuples. The first element is the callable passed
463
to L{deferFunction}. The second element is a copy of the scope stack
464
at the time L{deferFunction} was called.
466
@ivar _deferredAssignments: Similar to C{_deferredFunctions}, but for
467
callables which are deferred assignment checks.
474
builtIns = set(builtin_vars).union(_MAGIC_GLOBALS)
475
_customBuiltIns = os.environ.get('PYFLAKES_BUILTINS')
477
builtIns.update(_customBuiltIns.split(','))
480
def __init__(self, tree, filename='(none)', builtins=None,
481
withDoctest='PYFLAKES_DOCTEST' in os.environ):
482
self._nodeHandlers = {}
483
self._deferredFunctions = []
484
self._deferredAssignments = []
487
self.filename = filename
489
self.builtIns = self.builtIns.union(builtins)
490
self.withDoctest = withDoctest
491
self.scopeStack = [ModuleScope()]
492
self.exceptHandlers = [()]
494
self.handleChildren(tree)
495
self.runDeferred(self._deferredFunctions)
496
# Set _deferredFunctions to None so that deferFunction will fail
497
# noisily if called after we've run through the deferred functions.
498
self._deferredFunctions = None
499
self.runDeferred(self._deferredAssignments)
500
# Set _deferredAssignments to None so that deferAssignment will fail
501
# noisily if called after we've run through the deferred assignments.
502
self._deferredAssignments = None
503
del self.scopeStack[1:]
505
self.checkDeadScopes()
507
def deferFunction(self, callable):
509
Schedule a function handler to be called just before completion.
511
This is used for handling function bodies, which must be deferred
512
because code later in the file might modify the global scope. When
513
`callable` is called, the scope at the time this is called will be
514
restored, however it will contain any new bindings added to it.
516
self._deferredFunctions.append((callable, self.scopeStack[:], self.offset))
518
def deferAssignment(self, callable):
520
Schedule an assignment handler to be called just after deferred
523
self._deferredAssignments.append((callable, self.scopeStack[:], self.offset))
525
def runDeferred(self, deferred):
527
Run the callables in C{deferred} using their associated scope stack.
529
for handler, scope, offset in deferred:
530
self.scopeStack = scope
534
def _in_doctest(self):
535
return (len(self.scopeStack) >= 2 and
536
isinstance(self.scopeStack[1], DoctestScope))
539
def futuresAllowed(self):
540
if not all(isinstance(scope, ModuleScope)
541
for scope in self.scopeStack):
544
return self.scope._futures_allowed
546
@futuresAllowed.setter
547
def futuresAllowed(self, value):
548
assert value is False
549
if isinstance(self.scope, ModuleScope):
550
self.scope._futures_allowed = False
554
return self.scopeStack[-1]
557
self.deadScopes.append(self.scopeStack.pop())
559
def checkDeadScopes(self):
561
Look at scopes which have been fully examined and report names in them
562
which were imported but unused.
564
for scope in self.deadScopes:
565
# imports in classes are public members
566
if isinstance(scope, ClassScope):
569
all_binding = scope.get('__all__')
570
if all_binding and not isinstance(all_binding, ExportBinding):
574
all_names = set(all_binding.names)
575
undefined = all_names.difference(scope)
577
all_names = undefined = []
580
if not scope.importStarred and \
581
os.path.basename(self.filename) != '__init__.py':
582
# Look for possible mistakes in the export list
583
for name in undefined:
584
self.report(messages.UndefinedExport,
585
scope['__all__'].source, name)
587
# mark all import '*' as used by the undefined in __all__
588
if scope.importStarred:
589
for binding in scope.values():
590
if isinstance(binding, StarImportation):
591
binding.used = all_binding
593
# Look for imported names that aren't used.
594
for value in scope.values():
595
if isinstance(value, Importation):
596
used = value.used or value.name in all_names
598
messg = messages.UnusedImport
599
self.report(messg, value.source, str(value))
600
for node in value.redefined:
601
if isinstance(self.getParent(node), ast.For):
602
messg = messages.ImportShadowedByLoopVar
606
messg = messages.RedefinedWhileUnused
607
self.report(messg, node, value.name, value.source)
609
def pushScope(self, scopeClass=FunctionScope):
610
self.scopeStack.append(scopeClass())
612
def report(self, messageClass, *args, **kwargs):
613
self.messages.append(messageClass(self.filename, *args, **kwargs))
615
def getParent(self, node):
616
# Lookup the first parent which is not Tuple, List or Starred
619
if not hasattr(node, 'elts') and not hasattr(node, 'ctx'):
622
def getCommonAncestor(self, lnode, rnode, stop):
623
if stop in (lnode, rnode) or not (hasattr(lnode, 'parent') and
624
hasattr(rnode, 'parent')):
629
if (lnode.depth > rnode.depth):
630
return self.getCommonAncestor(lnode.parent, rnode, stop)
631
if (lnode.depth < rnode.depth):
632
return self.getCommonAncestor(lnode, rnode.parent, stop)
633
return self.getCommonAncestor(lnode.parent, rnode.parent, stop)
635
def descendantOf(self, node, ancestors, stop):
637
if self.getCommonAncestor(node, a, stop):
641
def differentForks(self, lnode, rnode):
642
"""True, if lnode and rnode are located on different forks of IF/TRY"""
643
ancestor = self.getCommonAncestor(lnode, rnode, self.root)
644
parts = getAlternatives(ancestor)
647
if self.descendantOf(lnode, items, ancestor) ^ \
648
self.descendantOf(rnode, items, ancestor):
652
def addBinding(self, node, value):
654
Called when a binding is altered.
656
- `node` is the statement responsible for the change
657
- `value` is the new value, a Binding instance
659
# assert value.source in (node, node.parent):
660
for scope in self.scopeStack[::-1]:
661
if value.name in scope:
663
existing = scope.get(value.name)
665
if existing and not self.differentForks(node, existing.source):
667
parent_stmt = self.getParent(value.source)
668
if isinstance(existing, Importation) and isinstance(parent_stmt, ast.For):
669
self.report(messages.ImportShadowedByLoopVar,
670
node, value.name, existing.source)
672
elif scope is self.scope:
673
if (isinstance(parent_stmt, ast.comprehension) and
674
not isinstance(self.getParent(existing.source),
675
(ast.For, ast.comprehension))):
676
self.report(messages.RedefinedInListComp,
677
node, value.name, existing.source)
678
elif not existing.used and value.redefines(existing):
679
self.report(messages.RedefinedWhileUnused,
680
node, value.name, existing.source)
682
elif isinstance(existing, Importation) and value.redefines(existing):
683
existing.redefined.append(node)
685
if value.name in self.scope:
686
# then assume the rebound name is used as a global or within a loop
687
value.used = self.scope[value.name].used
689
self.scope[value.name] = value
691
def getNodeHandler(self, node_class):
693
return self._nodeHandlers[node_class]
695
nodeType = getNodeType(node_class)
696
self._nodeHandlers[node_class] = handler = getattr(self, nodeType)
699
def handleNodeLoad(self, node):
700
name = getNodeName(node)
707
# try enclosing function scopes and global scope
708
for scope in self.scopeStack[-1::-1]:
709
# only generators used in a class scope can access the names
710
# of the class. this is skipped during the first iteration
711
if in_generators is False and isinstance(scope, ClassScope):
715
scope[name].used = (self.scope, node)
721
importStarred = importStarred or scope.importStarred
723
if in_generators is not False:
724
in_generators = isinstance(scope, GeneratorScope)
726
# look in the built-ins
727
if name in self.builtIns:
733
for scope in self.scopeStack[-1::-1]:
734
for binding in scope.values():
735
if isinstance(binding, StarImportation):
736
# mark '*' imports as used for each scope
737
binding.used = (self.scope, node)
738
from_list.append(binding.fullName)
740
# report * usage, with a list of possible sources
741
from_list = ', '.join(sorted(from_list))
742
self.report(messages.ImportStarUsage, node, name, from_list)
745
if name == '__path__' and os.path.basename(self.filename) == '__init__.py':
746
# the special name __path__ is valid only in packages
749
# protected with a NameError handler?
750
if 'NameError' not in self.exceptHandlers[-1]:
751
self.report(messages.UndefinedName, node, name)
753
def handleNodeStore(self, node):
754
name = getNodeName(node)
757
# if the name hasn't already been defined in the current scope
758
if isinstance(self.scope, FunctionScope) and name not in self.scope:
759
# for each function or module scope above us
760
for scope in self.scopeStack[:-1]:
761
if not isinstance(scope, (FunctionScope, ModuleScope)):
763
# if the name was defined in that scope, and the name has
764
# been accessed already in the current scope, and hasn't
765
# been declared global
766
used = name in scope and scope[name].used
767
if used and used[0] is self.scope and name not in self.scope.globals:
768
# then it's probably a mistake
769
self.report(messages.UndefinedLocal,
770
scope[name].used[1], name, scope[name].source)
773
parent_stmt = self.getParent(node)
774
if isinstance(parent_stmt, (ast.For, ast.comprehension)) or (
775
parent_stmt != node.parent and
776
not self.isLiteralTupleUnpacking(parent_stmt)):
777
binding = Binding(name, node)
778
elif name == '__all__' and isinstance(self.scope, ModuleScope):
779
binding = ExportBinding(name, node.parent, self.scope)
781
binding = Assignment(name, node)
782
self.addBinding(node, binding)
784
def handleNodeDelete(self, node):
786
def on_conditional_branch():
788
Return `True` if node is part of a conditional body.
790
current = getattr(node, 'parent', None)
792
if isinstance(current, (ast.If, ast.While, ast.IfExp)):
794
current = getattr(current, 'parent', None)
797
name = getNodeName(node)
801
if on_conditional_branch():
802
# We cannot predict if this conditional branch is going to
806
if isinstance(self.scope, FunctionScope) and name in self.scope.globals:
807
self.scope.globals.remove(name)
812
self.report(messages.UndefinedName, node, name)
814
def handleChildren(self, tree, omit=None):
815
for node in iter_child_nodes(tree, omit=omit):
816
self.handleNode(node, tree)
818
def isLiteralTupleUnpacking(self, node):
819
if isinstance(node, ast.Assign):
820
for child in node.targets + [node.value]:
821
if not hasattr(child, 'elts'):
825
def isDocstring(self, node):
827
Determine if the given node is a docstring, as long as it is at the
828
correct place in the node tree.
830
return isinstance(node, ast.Str) or (isinstance(node, ast.Expr) and
831
isinstance(node.value, ast.Str))
833
def getDocstring(self, node):
834
if isinstance(node, ast.Expr):
836
if not isinstance(node, ast.Str):
840
doctest_lineno = node.lineno - 1
842
# Computed incorrectly if the docstring has backslash
843
doctest_lineno = node.lineno - node.s.count('\n') - 1
845
return (node.s, doctest_lineno)
847
def handleNode(self, node, parent):
850
if self.offset and getattr(node, 'lineno', None) is not None:
851
node.lineno += self.offset[0]
852
node.col_offset += self.offset[1]
854
print(' ' * self.nodeDepth + node.__class__.__name__)
855
if self.futuresAllowed and not (isinstance(node, ast.ImportFrom) or
856
self.isDocstring(node)):
857
self.futuresAllowed = False
859
node.depth = self.nodeDepth
862
handler = self.getNodeHandler(node.__class__)
867
print(' ' * self.nodeDepth + 'end ' + node.__class__.__name__)
869
_getDoctestExamples = doctest.DocTestParser().get_examples
871
def handleDoctests(self, node):
873
(docstring, node_lineno) = self.getDocstring(node.body[0])
874
examples = docstring and self._getDoctestExamples(docstring)
875
except (ValueError, IndexError):
876
# e.g. line 6 of the docstring for <string> has inconsistent
877
# leading whitespace: ...
882
# Place doctest in module scope
883
saved_stack = self.scopeStack
884
self.scopeStack = [self.scopeStack[0]]
885
node_offset = self.offset or (0, 0)
886
self.pushScope(DoctestScope)
887
underscore_in_builtins = '_' in self.builtIns
888
if not underscore_in_builtins:
889
self.builtIns.add('_')
890
for example in examples:
892
tree = compile(example.source, "<doctest>", "exec", ast.PyCF_ONLY_AST)
894
e = sys.exc_info()[1]
897
position = (node_lineno + example.lineno + e.lineno,
898
example.indent + 4 + (e.offset or 0))
899
self.report(messages.DoctestSyntaxError, node, position)
901
self.offset = (node_offset[0] + node_lineno + example.lineno,
902
node_offset[1] + example.indent + 4)
903
self.handleChildren(tree)
904
self.offset = node_offset
905
if not underscore_in_builtins:
906
self.builtIns.remove('_')
908
self.scopeStack = saved_stack
910
def ignore(self, node):
914
DELETE = PRINT = FOR = ASYNCFOR = WHILE = IF = WITH = WITHITEM = \
915
ASYNCWITH = ASYNCWITHITEM = RAISE = TRYFINALLY = EXEC = \
916
EXPR = ASSIGN = handleChildren
921
BOOLOP = BINOP = UNARYOP = IFEXP = SET = \
922
COMPARE = CALL = REPR = ATTRIBUTE = SUBSCRIPT = \
923
STARRED = NAMECONSTANT = handleChildren
925
NUM = STR = BYTES = ELLIPSIS = ignore
928
SLICE = EXTSLICE = INDEX = handleChildren
930
# expression contexts are node instances too, though being constants
931
LOAD = STORE = DEL = AUGLOAD = AUGSTORE = PARAM = ignore
934
AND = OR = ADD = SUB = MULT = DIV = MOD = POW = LSHIFT = RSHIFT = \
935
BITOR = BITXOR = BITAND = FLOORDIV = INVERT = NOT = UADD = USUB = \
936
EQ = NOTEQ = LT = LTE = GT = GTE = IS = ISNOT = IN = NOTIN = \
939
# additional node types
940
COMPREHENSION = KEYWORD = FORMATTEDVALUE = JOINEDSTR = handleChildren
942
def DICT(self, node):
943
# Complain if there are duplicate keys with different values
944
# If they have the same value it's not going to cause potentially
945
# unexpected behaviour so we'll not complain.
947
convert_to_value(key) for key in node.keys
950
key_counts = counter(keys)
952
key for key, count in key_counts.items()
956
for key in duplicate_keys:
957
key_indices = [i for i, i_key in enumerate(keys) if i_key == key]
960
convert_to_value(node.values[index])
961
for index in key_indices
963
if any(count == 1 for value, count in values.items()):
964
for key_index in key_indices:
965
key_node = node.keys[key_index]
966
if isinstance(key, VariableKey):
967
self.report(messages.MultiValueRepeatedKeyVariable,
972
messages.MultiValueRepeatedKeyLiteral,
976
self.handleChildren(node)
978
def ASSERT(self, node):
979
if isinstance(node.test, ast.Tuple) and node.test.elts != []:
980
self.report(messages.AssertTuple, node)
981
self.handleChildren(node)
983
def GLOBAL(self, node):
985
Keep track of globals declarations.
987
global_scope_index = 1 if self._in_doctest() else 0
988
global_scope = self.scopeStack[global_scope_index]
990
# Ignore 'global' statement in global scope.
991
if self.scope is not global_scope:
993
# One 'global' statement can bind multiple (comma-delimited) names.
994
for node_name in node.names:
995
node_value = Assignment(node_name, node)
997
# Remove UndefinedName messages already reported for this name.
998
# TODO: if the global is not used in this scope, it does not
999
# become a globally defined name. See test_unused_global.
1001
m for m in self.messages if not
1002
isinstance(m, messages.UndefinedName) or
1003
m.message_args[0] != node_name]
1005
# Bind name to global scope if it doesn't exist already.
1006
global_scope.setdefault(node_name, node_value)
1008
# Bind name to non-global scopes, but as already "used".
1009
node_value.used = (global_scope, node)
1010
for scope in self.scopeStack[global_scope_index + 1:]:
1011
scope[node_name] = node_value
1015
def GENERATOREXP(self, node):
1016
self.pushScope(GeneratorScope)
1017
self.handleChildren(node)
1020
LISTCOMP = handleChildren if PY2 else GENERATOREXP
1022
DICTCOMP = SETCOMP = GENERATOREXP
1024
def NAME(self, node):
1026
Handle occurrence of Name (which can be a load/store/delete access.)
1028
# Locate the name in locals / function / globals scopes.
1029
if isinstance(node.ctx, (ast.Load, ast.AugLoad)):
1030
self.handleNodeLoad(node)
1031
if (node.id == 'locals' and isinstance(self.scope, FunctionScope)
1032
and isinstance(node.parent, ast.Call)):
1033
# we are doing locals() call in current scope
1034
self.scope.usesLocals = True
1035
elif isinstance(node.ctx, (ast.Store, ast.AugStore)):
1036
self.handleNodeStore(node)
1037
elif isinstance(node.ctx, ast.Del):
1038
self.handleNodeDelete(node)
1040
# must be a Param context -- this only happens for names in function
1041
# arguments, but these aren't dispatched through here
1042
raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))
1044
def CONTINUE(self, node):
1045
# Walk the tree up until we see a loop (OK), a function or class
1046
# definition (not OK), for 'continue', a finally block (not OK), or
1047
# the top module scope (not OK)
1049
while hasattr(n, 'parent'):
1050
n, n_child = n.parent, n
1051
if isinstance(n, LOOP_TYPES):
1052
# Doesn't apply unless it's in the loop itself
1053
if n_child not in n.orelse:
1055
if isinstance(n, (ast.FunctionDef, ast.ClassDef)):
1057
# Handle Try/TryFinally difference in Python < and >= 3.3
1058
if hasattr(n, 'finalbody') and isinstance(node, ast.Continue):
1059
if n_child in n.finalbody:
1060
self.report(messages.ContinueInFinally, node)
1062
if isinstance(node, ast.Continue):
1063
self.report(messages.ContinueOutsideLoop, node)
1065
self.report(messages.BreakOutsideLoop, node)
1069
def RETURN(self, node):
1070
if isinstance(self.scope, (ClassScope, ModuleScope)):
1071
self.report(messages.ReturnOutsideFunction, node)
1076
hasattr(self.scope, 'returnValue') and
1077
not self.scope.returnValue
1079
self.scope.returnValue = node.value
1080
self.handleNode(node.value, node)
1082
def YIELD(self, node):
1083
if isinstance(self.scope, (ClassScope, ModuleScope)):
1084
self.report(messages.YieldOutsideFunction, node)
1087
self.scope.isGenerator = True
1088
self.handleNode(node.value, node)
1090
AWAIT = YIELDFROM = YIELD
1092
def FUNCTIONDEF(self, node):
1093
for deco in node.decorator_list:
1094
self.handleNode(deco, node)
1096
self.addBinding(node, FunctionDefinition(node.name, node))
1097
# doctest does not process doctest within a doctest,
1098
# or in nested functions.
1099
if (self.withDoctest and
1100
not self._in_doctest() and
1101
not isinstance(self.scope, FunctionScope)):
1102
self.deferFunction(lambda: self.handleDoctests(node))
1104
ASYNCFUNCTIONDEF = FUNCTIONDEF
1106
def LAMBDA(self, node):
1111
def addArgs(arglist):
1113
if isinstance(arg, ast.Tuple):
1117
addArgs(node.args.args)
1118
defaults = node.args.defaults
1120
for arg in node.args.args + node.args.kwonlyargs:
1121
args.append(arg.arg)
1122
annotations.append(arg.annotation)
1123
defaults = node.args.defaults + node.args.kw_defaults
1125
# Only for Python3 FunctionDefs
1126
is_py3_func = hasattr(node, 'returns')
1128
for arg_name in ('vararg', 'kwarg'):
1129
wildcard = getattr(node.args, arg_name)
1132
args.append(wildcard if PY33 else wildcard.arg)
1134
if PY33: # Python 2.5 to 3.3
1135
argannotation = arg_name + 'annotation'
1136
annotations.append(getattr(node.args, argannotation))
1137
else: # Python >= 3.4
1138
annotations.append(wildcard.annotation)
1141
annotations.append(node.returns)
1143
if len(set(args)) < len(args):
1144
for (idx, arg) in enumerate(args):
1145
if arg in args[:idx]:
1146
self.report(messages.DuplicateArgument, node, arg)
1148
for child in annotations + defaults:
1150
self.handleNode(child, node)
1156
self.addBinding(node, Argument(name, node))
1157
if isinstance(node.body, list):
1158
# case for FunctionDefs
1159
for stmt in node.body:
1160
self.handleNode(stmt, node)
1163
self.handleNode(node.body, node)
1165
def checkUnusedAssignments():
1167
Check to see if any assignments have not been used.
1169
for name, binding in self.scope.unusedAssignments():
1170
self.report(messages.UnusedVariable, binding.source, name)
1171
self.deferAssignment(checkUnusedAssignments)
1174
def checkReturnWithArgumentInsideGenerator():
1176
Check to see if there is any return statement with
1177
arguments but the function is a generator.
1179
if self.scope.isGenerator and self.scope.returnValue:
1180
self.report(messages.ReturnWithArgsInsideGenerator,
1181
self.scope.returnValue)
1182
self.deferAssignment(checkReturnWithArgumentInsideGenerator)
1185
self.deferFunction(runFunction)
1187
def CLASSDEF(self, node):
1189
Check names used in a class definition, including its decorators, base
1190
classes, and the body of its definition. Additionally, add its name to
1193
for deco in node.decorator_list:
1194
self.handleNode(deco, node)
1195
for baseNode in node.bases:
1196
self.handleNode(baseNode, node)
1198
for keywordNode in node.keywords:
1199
self.handleNode(keywordNode, node)
1200
self.pushScope(ClassScope)
1201
# doctest does not process doctest within a doctest
1202
# classes within classes are processed.
1203
if (self.withDoctest and
1204
not self._in_doctest() and
1205
not isinstance(self.scope, FunctionScope)):
1206
self.deferFunction(lambda: self.handleDoctests(node))
1207
for stmt in node.body:
1208
self.handleNode(stmt, node)
1210
self.addBinding(node, ClassDefinition(node.name, node))
1212
def AUGASSIGN(self, node):
1213
self.handleNodeLoad(node.target)
1214
self.handleNode(node.value, node)
1215
self.handleNode(node.target, node)
1217
def TUPLE(self, node):
1218
if not PY2 and isinstance(node.ctx, ast.Store):
1219
# Python 3 advanced tuple unpacking: a, *b, c = d.
1220
# Only one starred expression is allowed, and no more than 1<<8
1221
# assignments are allowed before a stared expression. There is
1222
# also a limit of 1<<24 expressions after the starred expression,
1223
# which is impossible to test due to memory restrictions, but we
1224
# add it here anyway
1227
for i, n in enumerate(node.elts):
1228
if isinstance(n, ast.Starred):
1230
self.report(messages.TwoStarredExpressions, node)
1231
# The SyntaxError doesn't distinguish two from more
1236
if star_loc >= 1 << 8 or len(node.elts) - star_loc - 1 >= 1 << 24:
1237
self.report(messages.TooManyExpressionsInStarredAssignment, node)
1238
self.handleChildren(node)
1242
def IMPORT(self, node):
1243
for alias in node.names:
1244
if '.' in alias.name and not alias.asname:
1245
importation = SubmoduleImportation(alias.name, node)
1247
name = alias.asname or alias.name
1248
importation = Importation(name, node, alias.name)
1249
self.addBinding(node, importation)
1251
def IMPORTFROM(self, node):
1252
if node.module == '__future__':
1253
if not self.futuresAllowed:
1254
self.report(messages.LateFutureImport,
1255
node, [n.name for n in node.names])
1257
self.futuresAllowed = False
1259
module = ('.' * node.level) + (node.module or '')
1261
for alias in node.names:
1262
name = alias.asname or alias.name
1263
if node.module == '__future__':
1264
importation = FutureImportation(name, node, self.scope)
1265
if alias.name not in __future__.all_feature_names:
1266
self.report(messages.FutureFeatureNotDefined,
1268
elif alias.name == '*':
1269
# Only Python 2, local import * is a SyntaxWarning
1270
if not PY2 and not isinstance(self.scope, ModuleScope):
1271
self.report(messages.ImportStarNotPermitted,
1275
self.scope.importStarred = True
1276
self.report(messages.ImportStarUsed, node, module)
1277
importation = StarImportation(module, node)
1279
importation = ImportationFrom(name, node,
1281
self.addBinding(node, importation)
1283
def TRY(self, node):
1285
# List the exception handlers
1286
for i, handler in enumerate(node.handlers):
1287
if isinstance(handler.type, ast.Tuple):
1288
for exc_type in handler.type.elts:
1289
handler_names.append(getNodeName(exc_type))
1291
handler_names.append(getNodeName(handler.type))
1293
if handler.type is None and i < len(node.handlers) - 1:
1294
self.report(messages.DefaultExceptNotLast, handler)
1295
# Memorize the except handlers and process the body
1296
self.exceptHandlers.append(handler_names)
1297
for child in node.body:
1298
self.handleNode(child, node)
1299
self.exceptHandlers.pop()
1300
# Process the other nodes: "except:", "else:", "finally:"
1301
self.handleChildren(node, omit='body')
1305
def EXCEPTHANDLER(self, node):
1306
if PY2 or node.name is None:
1307
self.handleChildren(node)
1310
# 3.x: the name of the exception, which is not a Name node, but
1311
# a simple string, creates a local that is only bound within the scope
1312
# of the except: block.
1314
for scope in self.scopeStack[::-1]:
1315
if node.name in scope:
1316
is_name_previously_defined = True
1319
is_name_previously_defined = False
1321
self.handleNodeStore(node)
1322
self.handleChildren(node)
1323
if not is_name_previously_defined:
1324
# See discussion on https://github.com/PyCQA/pyflakes/pull/59
1326
# We're removing the local name since it's being unbound
1327
# after leaving the except: block and it's always unbound
1328
# if the except: block is never entered. This will cause an
1329
# "undefined name" error raised if the checked code tries to
1330
# use the name afterwards.
1332
# Unless it's been removed already. Then do nothing.
1335
del self.scope[node.name]
1339
def ANNASSIGN(self, node):
1341
Annotated assignments don't have annotations evaluated on function
1342
scope, hence the custom implementation.
1347
# Only bind the *targets* if the assignment has a value.
1348
# Otherwise it's not really ast.Store and shouldn't silence
1349
# UndefinedLocal warnings.
1350
self.handleNode(node.target, node)
1351
if not isinstance(self.scope, FunctionScope):
1352
self.handleNode(node.annotation, node)
1354
# If the assignment has value, handle the *value* now.
1355
self.handleNode(node.value, node)