eric7/Plugins/CheckerPlugins/SyntaxChecker/pyflakes/checker.py

branch
eric7
changeset 8682
04e80d1aaebf
parent 8312
800c432b34c8
child 8881
54e42bc2437a
equal deleted inserted replaced
8681:6285e8374d99 8682:04e80d1aaebf
280 field = getattr(node, name, None) 280 field = getattr(node, name, None)
281 if isinstance(field, ast.AST): 281 if isinstance(field, ast.AST):
282 yield field 282 yield field
283 elif isinstance(field, list): 283 elif isinstance(field, list):
284 for item in field: 284 for item in field:
285 yield item 285 if isinstance(item, ast.AST):
286 yield item
286 287
287 288
288 def convert_to_value(item): 289 def convert_to_value(item):
289 if isinstance(item, ast.Str): 290 if isinstance(item, ast.Str):
290 return item.s 291 return item.s
696 # Returns node.id, or node.name, or None 697 # Returns node.id, or node.name, or None
697 if hasattr(node, 'id'): # One of the many nodes with an id 698 if hasattr(node, 'id'): # One of the many nodes with an id
698 return node.id 699 return node.id
699 if hasattr(node, 'name'): # an ExceptHandler node 700 if hasattr(node, 'name'): # an ExceptHandler node
700 return node.name 701 return node.name
702 if hasattr(node, 'rest'): # a MatchMapping node
703 return node.rest
701 704
702 705
703 TYPING_MODULES = frozenset(('typing', 'typing_extensions')) 706 TYPING_MODULES = frozenset(('typing', 'typing_extensions'))
704 707
705 708
722 is_name_match_fn(scope[name].real_name) 725 is_name_match_fn(scope[name].real_name)
723 ) 726 )
724 727
725 return False 728 return False
726 729
730 def _module_scope_is_typing(name):
731 for scope in reversed(scope_stack):
732 if name in scope:
733 return (
734 isinstance(scope[name], Importation) and
735 scope[name].fullName in TYPING_MODULES
736 )
737
738 return False
739
727 return ( 740 return (
728 ( 741 (
729 isinstance(node, ast.Name) and 742 isinstance(node, ast.Name) and
730 _bare_name_is_attr(node.id) 743 _bare_name_is_attr(node.id)
731 ) or ( 744 ) or (
732 isinstance(node, ast.Attribute) and 745 isinstance(node, ast.Attribute) and
733 isinstance(node.value, ast.Name) and 746 isinstance(node.value, ast.Name) and
734 node.value.id in TYPING_MODULES and 747 _module_scope_is_typing(node.value.id) and
735 is_name_match_fn(node.attr) 748 is_name_match_fn(node.attr)
736 ) 749 )
737 ) 750 )
738 751
739 752
873 if PY35_PLUS: 886 if PY35_PLUS:
874 _ast_node_scope[ast.AsyncFunctionDef] = FunctionScope 887 _ast_node_scope[ast.AsyncFunctionDef] = FunctionScope
875 888
876 nodeDepth = 0 889 nodeDepth = 0
877 offset = None 890 offset = None
878 traceTree = False
879 _in_annotation = AnnotationState.NONE 891 _in_annotation = AnnotationState.NONE
880 _in_deferred = False 892 _in_deferred = False
881 893
882 builtIns = set(builtin_vars).union(_MAGIC_GLOBALS) 894 builtIns = set(builtin_vars).union(_MAGIC_GLOBALS)
883 _customBuiltIns = os.environ.get('PYFLAKES_BUILTINS') 895 _customBuiltIns = os.environ.get('PYFLAKES_BUILTINS')
1398 if node is None: 1410 if node is None:
1399 return 1411 return
1400 if self.offset and getattr(node, 'lineno', None) is not None: 1412 if self.offset and getattr(node, 'lineno', None) is not None:
1401 node.lineno += self.offset[0] 1413 node.lineno += self.offset[0]
1402 node.col_offset += self.offset[1] 1414 node.col_offset += self.offset[1]
1403 if self.traceTree:
1404 print(' ' * self.nodeDepth + node.__class__.__name__)
1405 if self.futuresAllowed and not (isinstance(node, ast.ImportFrom) or 1415 if self.futuresAllowed and not (isinstance(node, ast.ImportFrom) or
1406 self.isDocstring(node)): 1416 self.isDocstring(node)):
1407 self.futuresAllowed = False 1417 self.futuresAllowed = False
1408 self.nodeDepth += 1 1418 self.nodeDepth += 1
1409 node._pyflakes_depth = self.nodeDepth 1419 node._pyflakes_depth = self.nodeDepth
1411 try: 1421 try:
1412 handler = self.getNodeHandler(node.__class__) 1422 handler = self.getNodeHandler(node.__class__)
1413 handler(node) 1423 handler(node)
1414 finally: 1424 finally:
1415 self.nodeDepth -= 1 1425 self.nodeDepth -= 1
1416 if self.traceTree:
1417 print(' ' * self.nodeDepth + 'end ' + node.__class__.__name__)
1418 1426
1419 _getDoctestExamples = doctest.DocTestParser().get_examples 1427 _getDoctestExamples = doctest.DocTestParser().get_examples
1420 1428
1421 def handleDoctests(self, node): 1429 def handleDoctests(self, node):
1422 try: 1430 try:
2388 ): 2396 ):
2389 self.report(messages.IsLiteral, node) 2397 self.report(messages.IsLiteral, node)
2390 left = right 2398 left = right
2391 2399
2392 self.handleChildren(node) 2400 self.handleChildren(node)
2401
2402 MATCH = MATCH_CASE = MATCHCLASS = MATCHOR = MATCHSEQUENCE = handleChildren
2403 MATCHSINGLETON = MATCHVALUE = handleChildren
2404
2405 def _match_target(self, node):
2406 self.handleNodeStore(node)
2407 self.handleChildren(node)
2408
2409 MATCHAS = MATCHMAPPING = MATCHSTAR = _match_target

eric ide

mercurial