89 elif isinstance(field, list): |
113 elif isinstance(field, list): |
90 for item in field: |
114 for item in field: |
91 yield item |
115 yield item |
92 |
116 |
93 |
117 |
|
118 def convert_to_value(item): |
|
119 if isinstance(item, ast.Str): |
|
120 return item.s |
|
121 elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes): |
|
122 return item.s |
|
123 elif isinstance(item, ast.Tuple): |
|
124 return tuple(convert_to_value(i) for i in item.elts) |
|
125 elif isinstance(item, ast.Num): |
|
126 return item.n |
|
127 elif isinstance(item, ast.Name): |
|
128 result = VariableKey(item=item) |
|
129 constants_lookup = { |
|
130 'True': True, |
|
131 'False': False, |
|
132 'None': None, |
|
133 } |
|
134 return constants_lookup.get( |
|
135 result.name, |
|
136 result, |
|
137 ) |
|
138 elif (not PY33) and isinstance(item, ast.NameConstant): |
|
139 # None, True, False are nameconstants in python3, but names in 2 |
|
140 return item.value |
|
141 else: |
|
142 return UnhandledKeyType() |
|
143 |
|
144 |
94 class Binding(object): |
145 class Binding(object): |
95 """ |
146 """ |
96 Represents the binding of a value to a name. |
147 Represents the binding of a value to a name. |
97 |
148 |
98 The checker uses this to keep track of which names have been bound and |
149 The checker uses this to keep track of which names have been bound and |
99 which names have not. See L{Assignment} for a special type of binding that |
150 which names have not. See L{Assignment} for a special type of binding that |
100 is checked with stricter rules. |
151 is checked with stricter rules. |
101 |
152 |
102 @ivar used: pair of (L{Scope}, line-number) indicating the scope and |
153 @ivar used: pair of (L{Scope}, node) indicating the scope and |
103 line number that this binding was last used |
154 the node that this binding was last used. |
104 """ |
155 """ |
105 |
156 |
106 def __init__(self, name, source): |
157 def __init__(self, name, source): |
107 self.name = name |
158 self.name = name |
108 self.source = source |
159 self.source = source |
125 """ |
176 """ |
126 A binding that defines a function or a class. |
177 A binding that defines a function or a class. |
127 """ |
178 """ |
128 |
179 |
129 |
180 |
|
181 class UnhandledKeyType(object): |
|
182 """ |
|
183 A dictionary key of a type that we cannot or do not check for duplicates. |
|
184 """ |
|
185 |
|
186 |
|
187 class VariableKey(object): |
|
188 """ |
|
189 A dictionary key which is a variable. |
|
190 |
|
191 @ivar item: The variable AST object. |
|
192 """ |
|
193 def __init__(self, item): |
|
194 self.name = item.id |
|
195 |
|
196 def __eq__(self, compare): |
|
197 return ( |
|
198 compare.__class__ == self.__class__ |
|
199 and compare.name == self.name |
|
200 ) |
|
201 |
|
202 def __hash__(self): |
|
203 return hash(self.name) |
|
204 |
|
205 |
130 class Importation(Definition): |
206 class Importation(Definition): |
131 """ |
207 """ |
132 A binding created by an import statement. |
208 A binding created by an import statement. |
133 |
209 |
134 @ivar fullName: The complete name given to the import statement, |
210 @ivar fullName: The complete name given to the import statement, |
135 possibly including multiple dotted components. |
211 possibly including multiple dotted components. |
136 @type fullName: C{str} |
212 @type fullName: C{str} |
137 """ |
213 """ |
138 |
214 |
|
215 def __init__(self, name, source, full_name=None): |
|
216 self.fullName = full_name or name |
|
217 self.redefined = [] |
|
218 super(Importation, self).__init__(name, source) |
|
219 |
|
220 def redefines(self, other): |
|
221 if isinstance(other, SubmoduleImportation): |
|
222 # See note in SubmoduleImportation about RedefinedWhileUnused |
|
223 return self.fullName == other.fullName |
|
224 return isinstance(other, Definition) and self.name == other.name |
|
225 |
|
226 def _has_alias(self): |
|
227 """Return whether importation needs an as clause.""" |
|
228 return not self.fullName.split('.')[-1] == self.name |
|
229 |
|
230 @property |
|
231 def source_statement(self): |
|
232 """Generate a source statement equivalent to the import.""" |
|
233 if self._has_alias(): |
|
234 return 'import %s as %s' % (self.fullName, self.name) |
|
235 else: |
|
236 return 'import %s' % self.fullName |
|
237 |
|
238 def __str__(self): |
|
239 """Return import full name with alias.""" |
|
240 if self._has_alias(): |
|
241 return self.fullName + ' as ' + self.name |
|
242 else: |
|
243 return self.fullName |
|
244 |
|
245 |
|
246 class SubmoduleImportation(Importation): |
|
247 """ |
|
248 A binding created by a submodule import statement. |
|
249 |
|
250 A submodule import is a special case where the root module is implicitly |
|
251 imported, without an 'as' clause, and the submodule is also imported. |
|
252 Python does not restrict which attributes of the root module may be used. |
|
253 |
|
254 This class is only used when the submodule import is without an 'as' clause. |
|
255 |
|
256 pyflakes handles this case by registering the root module name in the scope, |
|
257 allowing any attribute of the root module to be accessed. |
|
258 |
|
259 RedefinedWhileUnused is suppressed in `redefines` unless the submodule |
|
260 name is also the same, to avoid false positives. |
|
261 """ |
|
262 |
139 def __init__(self, name, source): |
263 def __init__(self, name, source): |
|
264 # A dot should only appear in the name when it is a submodule import |
|
265 assert '.' in name and (not source or isinstance(source, ast.Import)) |
|
266 package_name = name.split('.')[0] |
|
267 super(SubmoduleImportation, self).__init__(package_name, source) |
140 self.fullName = name |
268 self.fullName = name |
141 self.redefined = [] |
|
142 name = name.split('.')[0] |
|
143 super(Importation, self).__init__(name, source) |
|
144 |
269 |
145 def redefines(self, other): |
270 def redefines(self, other): |
146 if isinstance(other, Importation): |
271 if isinstance(other, Importation): |
147 return self.fullName == other.fullName |
272 return self.fullName == other.fullName |
148 return isinstance(other, Definition) and self.name == other.name |
273 return super(SubmoduleImportation, self).redefines(other) |
|
274 |
|
275 def __str__(self): |
|
276 return self.fullName |
|
277 |
|
278 @property |
|
279 def source_statement(self): |
|
280 return 'import ' + self.fullName |
|
281 |
|
282 |
|
283 class ImportationFrom(Importation): |
|
284 |
|
285 def __init__(self, name, source, module, real_name=None): |
|
286 self.module = module |
|
287 self.real_name = real_name or name |
|
288 |
|
289 if module.endswith('.'): |
|
290 full_name = module + self.real_name |
|
291 else: |
|
292 full_name = module + '.' + self.real_name |
|
293 |
|
294 super(ImportationFrom, self).__init__(name, source, full_name) |
|
295 |
|
296 def __str__(self): |
|
297 """Return import full name with alias.""" |
|
298 if self.real_name != self.name: |
|
299 return self.fullName + ' as ' + self.name |
|
300 else: |
|
301 return self.fullName |
|
302 |
|
303 @property |
|
304 def source_statement(self): |
|
305 if self.real_name != self.name: |
|
306 return 'from %s import %s as %s' % (self.module, |
|
307 self.real_name, |
|
308 self.name) |
|
309 else: |
|
310 return 'from %s import %s' % (self.module, self.name) |
|
311 |
|
312 |
|
313 class StarImportation(Importation): |
|
314 """A binding created by an 'from x import *' statement.""" |
|
315 |
|
316 def __init__(self, name, source): |
|
317 super(StarImportation, self).__init__('*', source) |
|
318 # Each star importation needs a unique name, and |
|
319 # may not be the module name otherwise it will be deemed imported |
|
320 self.name = name + '.*' |
|
321 self.fullName = name |
|
322 |
|
323 @property |
|
324 def source_statement(self): |
|
325 return 'from ' + self.fullName + ' import *' |
|
326 |
|
327 def __str__(self): |
|
328 # When the module ends with a ., avoid the ambiguous '..*' |
|
329 if self.fullName.endswith('.'): |
|
330 return self.source_statement |
|
331 else: |
|
332 return self.name |
|
333 |
|
334 |
|
335 class FutureImportation(ImportationFrom): |
|
336 """ |
|
337 A binding created by a from `__future__` import statement. |
|
338 |
|
339 `__future__` imports are implicitly used. |
|
340 """ |
|
341 |
|
342 def __init__(self, name, source, scope): |
|
343 super(FutureImportation, self).__init__(name, source, '__future__') |
|
344 self.used = (scope, source) |
149 |
345 |
150 |
346 |
151 class Argument(Binding): |
347 class Argument(Binding): |
152 """ |
348 """ |
153 Represents binding a name as an argument. |
349 Represents binding a name as an argument. |
349 """ |
567 """ |
350 Look at scopes which have been fully examined and report names in them |
568 Look at scopes which have been fully examined and report names in them |
351 which were imported but unused. |
569 which were imported but unused. |
352 """ |
570 """ |
353 for scope in self.deadScopes: |
571 for scope in self.deadScopes: |
354 if isinstance(scope.get('__all__'), ExportBinding): |
572 # imports in classes are public members |
355 all_names = set(scope['__all__'].names) |
573 if isinstance(scope, ClassScope): |
|
574 continue |
|
575 |
|
576 all_binding = scope.get('__all__') |
|
577 if all_binding and not isinstance(all_binding, ExportBinding): |
|
578 all_binding = None |
|
579 |
|
580 if all_binding: |
|
581 all_names = set(all_binding.names) |
|
582 undefined = all_names.difference(scope) |
|
583 else: |
|
584 all_names = undefined = [] |
|
585 |
|
586 if undefined: |
356 if not scope.importStarred and \ |
587 if not scope.importStarred and \ |
357 os.path.basename(self.filename) != '__init__.py': |
588 os.path.basename(self.filename) != '__init__.py': |
358 # Look for possible mistakes in the export list |
589 # Look for possible mistakes in the export list |
359 undefined = all_names.difference(scope) |
|
360 for name in undefined: |
590 for name in undefined: |
361 self.report(messages.UndefinedExport, |
591 self.report(messages.UndefinedExport, |
362 scope['__all__'].source, name) |
592 scope['__all__'].source, name) |
363 else: |
593 |
364 all_names = [] |
594 # mark all import '*' as used by the undefined in __all__ |
|
595 if scope.importStarred: |
|
596 for binding in scope.values(): |
|
597 if isinstance(binding, StarImportation): |
|
598 binding.used = all_binding |
365 |
599 |
366 # Look for imported names that aren't used. |
600 # Look for imported names that aren't used. |
367 for value in scope.values(): |
601 for value in scope.values(): |
368 if isinstance(value, Importation): |
602 if isinstance(value, Importation): |
369 used = value.used or value.name in all_names |
603 used = value.used or value.name in all_names |
370 if not used: |
604 if not used: |
371 messg = messages.UnusedImport |
605 messg = messages.UnusedImport |
372 self.report(messg, value.source, value.name) |
606 self.report(messg, value.source, str(value)) |
373 for node in value.redefined: |
607 for node in value.redefined: |
374 if isinstance(self.getParent(node), ast.For): |
608 if isinstance(self.getParent(node), ast.For): |
375 messg = messages.ImportShadowedByLoopVar |
609 messg = messages.ImportShadowedByLoopVar |
376 elif used: |
610 elif used: |
377 continue |
611 continue |
471 |
705 |
472 def handleNodeLoad(self, node): |
706 def handleNodeLoad(self, node): |
473 name = getNodeName(node) |
707 name = getNodeName(node) |
474 if not name: |
708 if not name: |
475 return |
709 return |
476 # try local scope |
710 |
477 try: |
711 in_generators = None |
478 self.scope[name].used = (self.scope, node) |
712 importStarred = None |
479 except KeyError: |
|
480 pass |
|
481 else: |
|
482 return |
|
483 |
|
484 scopes = [scope for scope in self.scopeStack[:-1] |
|
485 if isinstance(scope, (FunctionScope, ModuleScope, GeneratorScope))] |
|
486 if isinstance(self.scope, GeneratorScope) and scopes[-1] != self.scopeStack[-2]: |
|
487 scopes.append(self.scopeStack[-2]) |
|
488 |
713 |
489 # try enclosing function scopes and global scope |
714 # try enclosing function scopes and global scope |
490 importStarred = self.scope.importStarred |
715 for scope in self.scopeStack[-1::-1]: |
491 for scope in reversed(scopes): |
716 # only generators used in a class scope can access the names |
492 importStarred = importStarred or scope.importStarred |
717 # of the class. this is skipped during the first iteration |
|
718 if in_generators is False and isinstance(scope, ClassScope): |
|
719 continue |
|
720 |
493 try: |
721 try: |
494 scope[name].used = (self.scope, node) |
722 scope[name].used = (self.scope, node) |
495 except KeyError: |
723 except KeyError: |
496 pass |
724 pass |
497 else: |
725 else: |
498 return |
726 return |
499 |
727 |
|
728 importStarred = importStarred or scope.importStarred |
|
729 |
|
730 if in_generators is not False: |
|
731 in_generators = isinstance(scope, GeneratorScope) |
|
732 |
500 # look in the built-ins |
733 # look in the built-ins |
501 if importStarred or name in self.builtIns: |
734 if name in self.builtIns: |
502 return |
735 return |
|
736 |
|
737 if importStarred: |
|
738 from_list = [] |
|
739 |
|
740 for scope in self.scopeStack[-1::-1]: |
|
741 for binding in scope.values(): |
|
742 if isinstance(binding, StarImportation): |
|
743 # mark '*' imports as used for each scope |
|
744 binding.used = (self.scope, node) |
|
745 from_list.append(binding.fullName) |
|
746 |
|
747 # report * usage, with a list of possible sources |
|
748 from_list = ', '.join(sorted(from_list)) |
|
749 self.report(messages.ImportStarUsage, node, name, from_list) |
|
750 return |
|
751 |
503 if name == '__path__' and os.path.basename(self.filename) == '__init__.py': |
752 if name == '__path__' and os.path.basename(self.filename) == '__init__.py': |
504 # the special name __path__ is valid only in packages |
753 # the special name __path__ is valid only in packages |
505 return |
754 return |
506 |
755 |
507 # protected with a NameError handler? |
756 # protected with a NameError handler? |
629 # e.g. line 6 of the docstring for <string> has inconsistent |
883 # e.g. line 6 of the docstring for <string> has inconsistent |
630 # leading whitespace: ... |
884 # leading whitespace: ... |
631 return |
885 return |
632 if not examples: |
886 if not examples: |
633 return |
887 return |
|
888 |
|
889 # Place doctest in module scope |
|
890 saved_stack = self.scopeStack |
|
891 self.scopeStack = [self.scopeStack[0]] |
634 node_offset = self.offset or (0, 0) |
892 node_offset = self.offset or (0, 0) |
635 self.pushScope() |
893 self.pushScope(DoctestScope) |
636 underscore_in_builtins = '_' in self.builtIns |
894 underscore_in_builtins = '_' in self.builtIns |
637 if not underscore_in_builtins: |
895 if not underscore_in_builtins: |
638 self.builtIns.add('_') |
896 self.builtIns.add('_') |
639 for example in examples: |
897 for example in examples: |
640 try: |
898 try: |
641 tree = compile(example.source, "<doctest>", "exec", ast.PyCF_ONLY_AST) |
899 tree = compile(example.source, "<doctest>", "exec", ast.PyCF_ONLY_AST) |
642 except SyntaxError: |
900 except SyntaxError: |
643 e = sys.exc_info()[1] |
901 e = sys.exc_info()[1] |
|
902 if PYPY: |
|
903 e.offset += 1 |
644 position = (node_lineno + example.lineno + e.lineno, |
904 position = (node_lineno + example.lineno + e.lineno, |
645 example.indent + 4 + (e.offset or 0)) |
905 example.indent + 4 + (e.offset or 0)) |
646 self.report(messages.DoctestSyntaxError, node, position) |
906 self.report(messages.DoctestSyntaxError, node, position) |
647 else: |
907 else: |
648 self.offset = (node_offset[0] + node_lineno + example.lineno, |
908 self.offset = (node_offset[0] + node_lineno + example.lineno, |
650 self.handleChildren(tree) |
910 self.handleChildren(tree) |
651 self.offset = node_offset |
911 self.offset = node_offset |
652 if not underscore_in_builtins: |
912 if not underscore_in_builtins: |
653 self.builtIns.remove('_') |
913 self.builtIns.remove('_') |
654 self.popScope() |
914 self.popScope() |
|
915 self.scopeStack = saved_stack |
655 |
916 |
656 def ignore(self, node): |
917 def ignore(self, node): |
657 pass |
918 pass |
658 |
919 |
659 # "stmt" type nodes |
920 # "stmt" type nodes |
660 DELETE = PRINT = FOR = ASYNCFOR = WHILE = IF = WITH = WITHITEM = \ |
921 DELETE = PRINT = FOR = ASYNCFOR = WHILE = IF = WITH = WITHITEM = \ |
661 ASYNCWITH = ASYNCWITHITEM = RAISE = TRYFINALLY = ASSERT = EXEC = \ |
922 ASYNCWITH = ASYNCWITHITEM = RAISE = TRYFINALLY = EXEC = \ |
662 EXPR = ASSIGN = handleChildren |
923 EXPR = ASSIGN = handleChildren |
663 |
924 |
664 CONTINUE = BREAK = PASS = ignore |
925 PASS = ignore |
665 |
926 |
666 # "expr" type nodes |
927 # "expr" type nodes |
667 BOOLOP = BINOP = UNARYOP = IFEXP = DICT = SET = \ |
928 BOOLOP = BINOP = UNARYOP = IFEXP = SET = \ |
668 COMPARE = CALL = REPR = ATTRIBUTE = SUBSCRIPT = LIST = TUPLE = \ |
929 COMPARE = CALL = REPR = ATTRIBUTE = SUBSCRIPT = \ |
669 STARRED = NAMECONSTANT = handleChildren |
930 STARRED = NAMECONSTANT = handleChildren |
670 |
931 |
671 NUM = STR = BYTES = ELLIPSIS = ignore |
932 NUM = STR = BYTES = ELLIPSIS = ignore |
672 |
933 |
673 # "slice" type nodes |
934 # "slice" type nodes |
677 LOAD = STORE = DEL = AUGLOAD = AUGSTORE = PARAM = ignore |
938 LOAD = STORE = DEL = AUGLOAD = AUGSTORE = PARAM = ignore |
678 |
939 |
679 # same for operators |
940 # same for operators |
680 AND = OR = ADD = SUB = MULT = DIV = MOD = POW = LSHIFT = RSHIFT = \ |
941 AND = OR = ADD = SUB = MULT = DIV = MOD = POW = LSHIFT = RSHIFT = \ |
681 BITOR = BITXOR = BITAND = FLOORDIV = INVERT = NOT = UADD = USUB = \ |
942 BITOR = BITXOR = BITAND = FLOORDIV = INVERT = NOT = UADD = USUB = \ |
682 EQ = NOTEQ = LT = LTE = GT = GTE = IS = ISNOT = IN = NOTIN = ignore |
943 EQ = NOTEQ = LT = LTE = GT = GTE = IS = ISNOT = IN = NOTIN = \ |
|
944 MATMULT = ignore |
683 |
945 |
684 # additional node types |
946 # additional node types |
685 COMPREHENSION = KEYWORD = handleChildren |
947 COMPREHENSION = KEYWORD = FORMATTEDVALUE = handleChildren |
|
948 |
|
949 def DICT(self, node): |
|
950 # Complain if there are duplicate keys with different values |
|
951 # If they have the same value it's not going to cause potentially |
|
952 # unexpected behaviour so we'll not complain. |
|
953 keys = [ |
|
954 convert_to_value(key) for key in node.keys |
|
955 ] |
|
956 |
|
957 key_counts = counter(keys) |
|
958 duplicate_keys = [ |
|
959 key for key, count in key_counts.items() |
|
960 if count > 1 |
|
961 ] |
|
962 |
|
963 for key in duplicate_keys: |
|
964 key_indices = [i for i, i_key in enumerate(keys) if i_key == key] |
|
965 |
|
966 values = counter( |
|
967 convert_to_value(node.values[index]) |
|
968 for index in key_indices |
|
969 ) |
|
970 if any(count == 1 for value, count in values.items()): |
|
971 for key_index in key_indices: |
|
972 key_node = node.keys[key_index] |
|
973 if isinstance(key, VariableKey): |
|
974 self.report(messages.MultiValueRepeatedKeyVariable, |
|
975 key_node, |
|
976 key.name) |
|
977 else: |
|
978 self.report( |
|
979 messages.MultiValueRepeatedKeyLiteral, |
|
980 key_node, |
|
981 key, |
|
982 ) |
|
983 self.handleChildren(node) |
|
984 |
|
985 def ASSERT(self, node): |
|
986 if isinstance(node.test, ast.Tuple) and node.test.elts != []: |
|
987 self.report(messages.AssertTuple, node) |
|
988 self.handleChildren(node) |
686 |
989 |
687 def GLOBAL(self, node): |
990 def GLOBAL(self, node): |
688 """ |
991 """ |
689 Keep track of globals declarations. |
992 Keep track of globals declarations. |
690 """ |
993 """ |
691 # In doctests, the global scope is an anonymous function at index 1. |
994 global_scope_index = 1 if self._in_doctest() else 0 |
692 global_scope_index = 0#1 if self.withDoctest else 0 |
|
693 global_scope = self.scopeStack[global_scope_index] |
995 global_scope = self.scopeStack[global_scope_index] |
694 |
996 |
695 # Ignore 'global' statement in global scope. |
997 # Ignore 'global' statement in global scope. |
696 if self.scope is not global_scope: |
998 if self.scope is not global_scope: |
697 |
999 |
698 # One 'global' statement can bind multiple (comma-delimited) names. |
1000 # One 'global' statement can bind multiple (comma-delimited) names. |
699 for node_name in node.names: |
1001 for node_name in node.names: |
700 node_value = Assignment(node_name, node) |
1002 node_value = Assignment(node_name, node) |
701 |
1003 |
702 # Remove UndefinedName messages already reported for this name. |
1004 # Remove UndefinedName messages already reported for this name. |
|
1005 # TODO: if the global is not used in this scope, it does not |
|
1006 # become a globally defined name. See test_unused_global. |
703 self.messages = [ |
1007 self.messages = [ |
704 m for m in self.messages if not |
1008 m for m in self.messages if not |
705 isinstance(m, messages.UndefinedName) or |
1009 isinstance(m, messages.UndefinedName) or |
706 m.message_args[0] != node_name] |
1010 m.message_args[0] != node_name] |
707 |
1011 |
744 else: |
1046 else: |
745 # must be a Param context -- this only happens for names in function |
1047 # must be a Param context -- this only happens for names in function |
746 # arguments, but these aren't dispatched through here |
1048 # arguments, but these aren't dispatched through here |
747 raise RuntimeError("Got impossible expression context: %r" % (node.ctx,)) |
1049 raise RuntimeError("Got impossible expression context: %r" % (node.ctx,)) |
748 |
1050 |
|
1051 def CONTINUE(self, node): |
|
1052 # Walk the tree up until we see a loop (OK), a function or class |
|
1053 # definition (not OK), for 'continue', a finally block (not OK), or |
|
1054 # the top module scope (not OK) |
|
1055 n = node |
|
1056 while hasattr(n, 'parent'): |
|
1057 n, n_child = n.parent, n |
|
1058 if isinstance(n, LOOP_TYPES): |
|
1059 # Doesn't apply unless it's in the loop itself |
|
1060 if n_child not in n.orelse: |
|
1061 return |
|
1062 if isinstance(n, (ast.FunctionDef, ast.ClassDef)): |
|
1063 break |
|
1064 # Handle Try/TryFinally difference in Python < and >= 3.3 |
|
1065 if hasattr(n, 'finalbody') and isinstance(node, ast.Continue): |
|
1066 if n_child in n.finalbody: |
|
1067 self.report(messages.ContinueInFinally, node) |
|
1068 return |
|
1069 if isinstance(node, ast.Continue): |
|
1070 self.report(messages.ContinueOutsideLoop, node) |
|
1071 else: # ast.Break |
|
1072 self.report(messages.BreakOutsideLoop, node) |
|
1073 |
|
1074 BREAK = CONTINUE |
|
1075 |
749 def RETURN(self, node): |
1076 def RETURN(self, node): |
750 if isinstance(self.scope, ClassScope): |
1077 if isinstance(self.scope, (ClassScope, ModuleScope)): |
751 self.report(messages.ReturnOutsideFunction, node) |
1078 self.report(messages.ReturnOutsideFunction, node) |
752 return |
1079 return |
753 |
1080 |
754 if ( |
1081 if ( |
755 node.value and |
1082 node.value and |
758 ): |
1085 ): |
759 self.scope.returnValue = node.value |
1086 self.scope.returnValue = node.value |
760 self.handleNode(node.value, node) |
1087 self.handleNode(node.value, node) |
761 |
1088 |
762 def YIELD(self, node): |
1089 def YIELD(self, node): |
|
1090 if isinstance(self.scope, (ClassScope, ModuleScope)): |
|
1091 self.report(messages.YieldOutsideFunction, node) |
|
1092 return |
|
1093 |
763 self.scope.isGenerator = True |
1094 self.scope.isGenerator = True |
764 self.handleNode(node.value, node) |
1095 self.handleNode(node.value, node) |
765 |
1096 |
766 AWAIT = YIELDFROM = YIELD |
1097 AWAIT = YIELDFROM = YIELD |
767 |
1098 |
768 def FUNCTIONDEF(self, node): |
1099 def FUNCTIONDEF(self, node): |
769 for deco in node.decorator_list: |
1100 for deco in node.decorator_list: |
770 self.handleNode(deco, node) |
1101 self.handleNode(deco, node) |
771 self.LAMBDA(node) |
1102 self.LAMBDA(node) |
772 self.addBinding(node, FunctionDefinition(node.name, node)) |
1103 self.addBinding(node, FunctionDefinition(node.name, node)) |
773 if self.withDoctest: |
1104 # doctest does not process doctest within a doctest, |
|
1105 # or in nested functions. |
|
1106 if (self.withDoctest and |
|
1107 not self._in_doctest() and |
|
1108 not isinstance(self.scope, FunctionScope)): |
774 self.deferFunction(lambda: self.handleDoctests(node)) |
1109 self.deferFunction(lambda: self.handleDoctests(node)) |
775 |
1110 |
776 ASYNCFUNCTIONDEF = FUNCTIONDEF |
1111 ASYNCFUNCTIONDEF = FUNCTIONDEF |
777 |
1112 |
778 def LAMBDA(self, node): |
1113 def LAMBDA(self, node): |
880 def AUGASSIGN(self, node): |
1219 def AUGASSIGN(self, node): |
881 self.handleNodeLoad(node.target) |
1220 self.handleNodeLoad(node.target) |
882 self.handleNode(node.value, node) |
1221 self.handleNode(node.value, node) |
883 self.handleNode(node.target, node) |
1222 self.handleNode(node.target, node) |
884 |
1223 |
|
1224 def TUPLE(self, node): |
|
1225 if not PY2 and isinstance(node.ctx, ast.Store): |
|
1226 # Python 3 advanced tuple unpacking: a, *b, c = d. |
|
1227 # Only one starred expression is allowed, and no more than 1<<8 |
|
1228 # assignments are allowed before a stared expression. There is |
|
1229 # also a limit of 1<<24 expressions after the starred expression, |
|
1230 # which is impossible to test due to memory restrictions, but we |
|
1231 # add it here anyway |
|
1232 has_starred = False |
|
1233 star_loc = -1 |
|
1234 for i, n in enumerate(node.elts): |
|
1235 if isinstance(n, ast.Starred): |
|
1236 if has_starred: |
|
1237 self.report(messages.TwoStarredExpressions, node) |
|
1238 # The SyntaxError doesn't distinguish two from more |
|
1239 # than two. |
|
1240 break |
|
1241 has_starred = True |
|
1242 star_loc = i |
|
1243 if star_loc >= 1 << 8 or len(node.elts) - star_loc - 1 >= 1 << 24: |
|
1244 self.report(messages.TooManyExpressionsInStarredAssignment, node) |
|
1245 self.handleChildren(node) |
|
1246 |
|
1247 LIST = TUPLE |
|
1248 |
885 def IMPORT(self, node): |
1249 def IMPORT(self, node): |
886 for alias in node.names: |
1250 for alias in node.names: |
887 name = alias.asname or alias.name |
1251 if '.' in alias.name and not alias.asname: |
888 importation = Importation(name, node) |
1252 importation = SubmoduleImportation(alias.name, node) |
|
1253 else: |
|
1254 name = alias.asname or alias.name |
|
1255 importation = Importation(name, node, alias.name) |
889 self.addBinding(node, importation) |
1256 self.addBinding(node, importation) |
890 |
1257 |
891 def IMPORTFROM(self, node): |
1258 def IMPORTFROM(self, node): |
892 if node.module == '__future__': |
1259 if node.module == '__future__': |
893 if not self.futuresAllowed: |
1260 if not self.futuresAllowed: |
894 self.report(messages.LateFutureImport, |
1261 self.report(messages.LateFutureImport, |
895 node, [n.name for n in node.names]) |
1262 node, [n.name for n in node.names]) |
896 else: |
1263 else: |
897 self.futuresAllowed = False |
1264 self.futuresAllowed = False |
898 |
1265 |
|
1266 module = ('.' * node.level) + (node.module or '') |
|
1267 |
899 for alias in node.names: |
1268 for alias in node.names: |
900 if alias.name == '*': |
1269 name = alias.asname or alias.name |
|
1270 if node.module == '__future__': |
|
1271 importation = FutureImportation(name, node, self.scope) |
|
1272 if alias.name not in __future__.all_feature_names: |
|
1273 self.report(messages.FutureFeatureNotDefined, |
|
1274 node, alias.name) |
|
1275 elif alias.name == '*': |
|
1276 # Only Python 2, local import * is a SyntaxWarning |
|
1277 if not PY2 and not isinstance(self.scope, ModuleScope): |
|
1278 self.report(messages.ImportStarNotPermitted, |
|
1279 node, module) |
|
1280 continue |
|
1281 |
901 self.scope.importStarred = True |
1282 self.scope.importStarred = True |
902 self.report(messages.ImportStarUsed, node, node.module) |
1283 self.report(messages.ImportStarUsed, node, module) |
903 continue |
1284 importation = StarImportation(module, node) |
904 name = alias.asname or alias.name |
1285 else: |
905 importation = Importation(name, node) |
1286 importation = ImportationFrom(name, node, |
906 if node.module == '__future__': |
1287 module, alias.name) |
907 importation.used = (self.scope, node) |
|
908 self.addBinding(node, importation) |
1288 self.addBinding(node, importation) |
909 |
1289 |
910 def TRY(self, node): |
1290 def TRY(self, node): |
911 handler_names = [] |
1291 handler_names = [] |
912 # List the exception handlers |
1292 # List the exception handlers |
913 for handler in node.handlers: |
1293 for i, handler in enumerate(node.handlers): |
914 if isinstance(handler.type, ast.Tuple): |
1294 if isinstance(handler.type, ast.Tuple): |
915 for exc_type in handler.type.elts: |
1295 for exc_type in handler.type.elts: |
916 handler_names.append(getNodeName(exc_type)) |
1296 handler_names.append(getNodeName(exc_type)) |
917 elif handler.type: |
1297 elif handler.type: |
918 handler_names.append(getNodeName(handler.type)) |
1298 handler_names.append(getNodeName(handler.type)) |
|
1299 |
|
1300 if handler.type is None and i < len(node.handlers) - 1: |
|
1301 self.report(messages.DefaultExceptNotLast, handler) |
919 # Memorize the except handlers and process the body |
1302 # Memorize the except handlers and process the body |
920 self.exceptHandlers.append(handler_names) |
1303 self.exceptHandlers.append(handler_names) |
921 for child in node.body: |
1304 for child in node.body: |
922 self.handleNode(child, node) |
1305 self.handleNode(child, node) |
923 self.exceptHandlers.pop() |
1306 self.exceptHandlers.pop() |
925 self.handleChildren(node, omit='body') |
1308 self.handleChildren(node, omit='body') |
926 |
1309 |
927 TRYEXCEPT = TRY |
1310 TRYEXCEPT = TRY |
928 |
1311 |
929 def EXCEPTHANDLER(self, node): |
1312 def EXCEPTHANDLER(self, node): |
930 # 3.x: in addition to handling children, we must handle the name of |
1313 if PY2 or node.name is None: |
931 # the exception, which is not a Name node, but a simple string. |
1314 self.handleChildren(node) |
932 if isinstance(node.name, str): |
1315 return |
933 self.handleNodeStore(node) |
1316 |
|
1317 # 3.x: the name of the exception, which is not a Name node, but |
|
1318 # a simple string, creates a local that is only bound within the scope |
|
1319 # of the except: block. |
|
1320 |
|
1321 for scope in self.scopeStack[::-1]: |
|
1322 if node.name in scope: |
|
1323 is_name_previously_defined = True |
|
1324 break |
|
1325 else: |
|
1326 is_name_previously_defined = False |
|
1327 |
|
1328 self.handleNodeStore(node) |
934 self.handleChildren(node) |
1329 self.handleChildren(node) |
|
1330 if not is_name_previously_defined: |
|
1331 # See discussion on https://github.com/pyflakes/pyflakes/pull/59. |
|
1332 |
|
1333 # We're removing the local name since it's being unbound |
|
1334 # after leaving the except: block and it's always unbound |
|
1335 # if the except: block is never entered. This will cause an |
|
1336 # "undefined name" error raised if the checked code tries to |
|
1337 # use the name afterwards. |
|
1338 # |
|
1339 # Unless it's been removed already. Then do nothing. |
|
1340 |
|
1341 try: |
|
1342 del self.scope[node.name] |
|
1343 except KeyError: |
|
1344 pass |
935 |
1345 |
936 # |
1346 # |
937 # eflag: noqa = M702 |
1347 # eflag: noqa = M702 |