UtilitiesPython2/py2flakes/checker.py

Wed, 03 Oct 2012 12:18:51 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 03 Oct 2012 12:18:51 +0200
changeset 2088
73a2ca4ac409
parent 1544
d06ffad32855
child 2302
f29e9405c851
permissions
-rw-r--r--

Added code to handle unknown node types to the pyflakes checker.

802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
1509
c0b5e693b0eb Updated copyright for 2012.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 945
diff changeset
3 # Copyright (c) 2011 - 2012 Detlev Offenbach <detlev@die-offenbachs.de>
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
5 # Original (c) 2005-2010 Divmod, Inc.
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 #
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 # This module is based on pyflakes for Python2 but was heavily hacked to
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 # work within eric5
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import __builtin__
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import os.path
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
12 import _ast
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 from py2flakes import messages
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
16
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
17 # utility function to iterate over an AST node's children, adapted
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
18 # from Python 2.6's standard ast module
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
19 try:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
20 import ast
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
21 iter_child_nodes = ast.iter_child_nodes
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
22 except (ImportError, AttributeError):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
23 def iter_child_nodes(node, astcls=_ast.AST):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
24 """
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
25 Yield all direct child nodes of *node*, that is, all fields that are nodes
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
26 and all items of fields that are lists of nodes.
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
27 """
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
28 for name in node._fields:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
29 field = getattr(node, name, None)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
30 if isinstance(field, astcls):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
31 yield field
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
32 elif isinstance(field, list):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
33 for item in field:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
34 yield item
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
35
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
36
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 class Binding(object):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 Represents the binding of a value to a name.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 The checker uses this to keep track of which names have been bound and
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 which names have not. See L{Assignment} for a special type of binding that
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 is checked with stricter rules.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 def __init__(self, name, source):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 self.name = name
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 self.source = source
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 self.used = False
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 def __str__(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 return self.name
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 def __repr__(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 return '<%s object %r from line %r at 0x%x>' % (
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 self.__class__.__name__,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 self.name,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 self.source.lineno,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 id(self))
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
60
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 class UnBinding(Binding):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 '''
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 Created by the 'del' operator.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 '''
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
66
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 class Importation(Binding):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 A binding created by an import statement.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 def __init__(self, name, source):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 self.fullName = name
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 name = name.split('.')[0]
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 super(Importation, self).__init__(name, source)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
76
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 class Argument(Binding):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 Represents binding a name as an argument.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
82
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 class Assignment(Binding):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 Represents binding a name with an explicit assignment.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 The checker will raise warnings for any Assignment that isn't used. Also,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 the checker does not consider assignments in tuple/list unpacking to be
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 Assignments, rather it treats them as simple Bindings.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
92
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 class FunctionDefinition(Binding):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 Represents a function definition.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 pass
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
99
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 class ExportBinding(Binding):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 A binding created by an __all__ assignment. If the names in the list
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 can be determined statically, they will be treated as names for export and
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 additional checking applied to them.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 The only __all__ assignment that can be recognized is one which takes
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 the value of a literal list containing literal strings. For example::
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 __all__ = ["foo", "bar"]
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 Names which are imported and not otherwise used but appear in the value of
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 __all__ will not have an unused import warning reported for them.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 def names(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 Return a list of the names referenced by this binding.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 names = []
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
119 if isinstance(self.source, _ast.List):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
120 for node in self.source.elts:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
121 if isinstance(node, _ast.Str):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
122 names.append(node.s)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 return names
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
125
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 class Scope(dict):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 Class defining the scope base class.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 importStarred = False # set to True when import * is found
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 def __repr__(self):
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
133 return '<%s at 0x%x %s>' % (
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
134 self.__class__.__name__, id(self), dict.__repr__(self))
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 def __init__(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 super(Scope, self).__init__()
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
139
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 class ClassScope(Scope):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 Class representing a name scope for a class.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 pass
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
146
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 class FunctionScope(Scope):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 Class representing a name scope for a function.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 def __init__(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 super(FunctionScope, self).__init__()
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 self.globals = {}
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
155
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 class ModuleScope(Scope):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 Class representing a name scope for a module.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 pass
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 # Globally defined names which are not attributes of the __builtin__ module.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 _MAGIC_GLOBALS = ['__file__', '__builtins__']
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
165
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 class Checker(object):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 Class to check the cleanliness and sanity of Python code.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 nodeDepth = 0
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 traceTree = False
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 def __init__(self, module, filename='(none)'):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 Constructor
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 @param module parsed module tree or module source code
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 @param filename name of the module file (string)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 self._deferredFunctions = []
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 self._deferredAssignments = []
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 self.dead_scopes = []
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 self.messages = []
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 self.filename = filename
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 self.scopeStack = [ModuleScope()]
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 self.futuresAllowed = True
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 if isinstance(module, str):
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
189 module = ast.parse(module, filename, "exec")
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 self.handleChildren(module)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 self._runDeferred(self._deferredFunctions)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 # Set _deferredFunctions to None so that deferFunction will fail
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 # noisily if called after we've run through the deferred functions.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 self._deferredFunctions = None
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 self._runDeferred(self._deferredAssignments)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 # Set _deferredAssignments to None so that deferAssignment will fail
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 # noisly if called after we've run through the deferred assignments.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 self._deferredAssignments = None
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 del self.scopeStack[1:]
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 self.popScope()
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 self.check_dead_scopes()
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 def deferFunction(self, callable):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 '''
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 Schedule a function handler to be called just before completion.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 This is used for handling function bodies, which must be deferred
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 because code later in the file might modify the global scope. When
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 `callable` is called, the scope at the time this is called will be
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 restored, however it will contain any new bindings added to it.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 '''
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 self._deferredFunctions.append((callable, self.scopeStack[:]))
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 def deferAssignment(self, callable):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 Schedule an assignment handler to be called just after deferred
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 function handlers.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 self._deferredAssignments.append((callable, self.scopeStack[:]))
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 def _runDeferred(self, deferred):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 """
1544
d06ffad32855 Fixed a source docu in py2flakes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1535
diff changeset
223 Run the callables in deferred using their associated scope stack.
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 for handler, scope in deferred:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 self.scopeStack = scope
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 handler()
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 def scope(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 return self.scopeStack[-1]
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 scope = property(scope)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 def popScope(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 self.dead_scopes.append(self.scopeStack.pop())
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 def check_dead_scopes(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 Look at scopes which have been fully examined and report names in them
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 which were imported but unused.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 for scope in self.dead_scopes:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 export = isinstance(scope.get('__all__'), ExportBinding)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 if export:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 all = scope['__all__'].names()
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 if os.path.split(self.filename)[1] != '__init__.py':
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 # Look for possible mistakes in the export list
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 undefined = set(all) - set(scope)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 for name in undefined:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 self.report(
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 messages.UndefinedExport,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 scope['__all__'].source.lineno,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 name)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 else:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 all = []
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 # Look for imported names that aren't used.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 for importation in scope.itervalues():
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 if isinstance(importation, Importation):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 if not importation.used and importation.name not in all:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 self.report(
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 messages.UnusedImport,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 importation.source.lineno,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 importation.name)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 def pushFunctionScope(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 self.scopeStack.append(FunctionScope())
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 def pushClassScope(self):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 self.scopeStack.append(ClassScope())
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 def report(self, messageClass, *args, **kwargs):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 self.messages.append(messageClass(self.filename, *args, **kwargs))
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 def handleChildren(self, tree):
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
275 for node in iter_child_nodes(tree):
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 self.handleNode(node, tree)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
278 def isDocstring(self, node):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
279 """
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
280 Determine if the given node is a docstring, as long as it is at the
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
281 correct place in the node tree.
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
282 """
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
283 return isinstance(node, _ast.Str) or \
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
284 (isinstance(node, _ast.Expr) and
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
285 isinstance(node.value, _ast.Str))
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
286
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 def handleNode(self, node, parent):
2088
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
288 if node:
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
289 node.parent = parent
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
290 if self.traceTree:
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
291 print ' ' * self.nodeDepth + node.__class__.__name__
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
292 self.nodeDepth += 1
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
293 if self.futuresAllowed and not \
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
294 (isinstance(node, _ast.ImportFrom) or self.isDocstring(node)):
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
295 self.futuresAllowed = False
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
296 nodeType = node.__class__.__name__.upper()
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
297 try:
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
298 handler = getattr(self, nodeType)
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
299 handler(node)
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
300 except AttributeError:
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
301 print nodeType, "not supported yet. Please report this."
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
302 finally:
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
303 self.nodeDepth -= 1
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
304 if self.traceTree:
73a2ca4ac409 Added code to handle unknown node types to the pyflakes checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1544
diff changeset
305 print ' ' * self.nodeDepth + 'end ' + node.__class__.__name__
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 def ignore(self, node):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 pass
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
310 # "stmt" type nodes
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
311 RETURN = DELETE = PRINT = WHILE = IF = WITH = RAISE = TRYEXCEPT = \
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
312 TRYFINALLY = ASSERT = EXEC = EXPR = handleChildren
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
313
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
314 CONTINUE = BREAK = PASS = ignore
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
315
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
316 # "expr" type nodes
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
317 BOOLOP = BINOP = UNARYOP = IFEXP = DICT = SET = YIELD = COMPARE = \
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
318 CALL = REPR = ATTRIBUTE = SUBSCRIPT = LIST = TUPLE = handleChildren
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
319
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
320 NUM = STR = ELLIPSIS = ignore
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
322 # "slice" type nodes
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
323 SLICE = EXTSLICE = INDEX = handleChildren
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
324
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
325 # expression contexts are node instances too, though being constants
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
326 LOAD = STORE = DEL = AUGLOAD = AUGSTORE = PARAM = ignore
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
327
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
328 # same for operators
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
329 AND = OR = ADD = SUB = MULT = DIV = MOD = POW = LSHIFT = RSHIFT = \
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
330 BITOR = BITXOR = BITAND = FLOORDIV = INVERT = NOT = UADD = USUB = \
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
331 EQ = NOTEQ = LT = LTE = GT = GTE = IS = ISNOT = IN = NOTIN = ignore
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
332
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
333 # additional node types
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
334 COMPREHENSION = EXCEPTHANDLER = KEYWORD = handleChildren
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 def addBinding(self, lineno, value, reportRedef=True):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 '''
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 Called when a binding is altered.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 @param lineno line of the statement responsible for the change (integer)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 @param value the optional new value, a Binding instance, associated
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 with the binding; if None, the binding is deleted if it exists
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 @param reportRedef flag indicating if rebinding while unused will be
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 reported (boolean)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 '''
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 if (isinstance(self.scope.get(value.name), FunctionDefinition)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 and isinstance(value, FunctionDefinition)):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 self.report(messages.RedefinedFunction,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 lineno, value.name, self.scope[value.name].source.lineno)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 if not isinstance(self.scope, ClassScope):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 for scope in self.scopeStack[::-1]:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 existing = scope.get(value.name)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 if (isinstance(existing, Importation)
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
355 and not existing.used
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
356 and (not isinstance(value, Importation) or
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
357 value.fullName == existing.fullName)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
358 and reportRedef):
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 self.report(messages.RedefinedWhileUnused,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 lineno, value.name, scope[value.name].source.lineno)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 if isinstance(value, UnBinding):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 try:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 del self.scope[value.name]
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 except KeyError:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 self.report(messages.UndefinedName, lineno, value.name)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 else:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 self.scope[value.name] = value
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 def GLOBAL(self, node):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 Keep track of globals declarations.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 if isinstance(self.scope, FunctionScope):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 self.scope.globals.update(dict.fromkeys(node.names))
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 def LISTCOMP(self, node):
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
379 # handle generators before element
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
380 for gen in node.generators:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
381 self.handleNode(gen, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
382 self.handleNode(node.elt, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
383
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
384 GENERATOREXP = SETCOMP = LISTCOMP
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
386 # dictionary comprehensions; introduced in Python 2.7
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
387 def DICTCOMP(self, node):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
388 for gen in node.generators:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
389 self.handleNode(gen, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
390 self.handleNode(node.key, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
391 self.handleNode(node.value, node)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 def FOR(self, node):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 Process bindings for loop variables.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 vars = []
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
398
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 def collectLoopVars(n):
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
400 if isinstance(n, _ast.Name):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
401 vars.append(n.id)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
402 elif isinstance(n, _ast.expr_context):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
403 return
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 else:
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
405 for c in iter_child_nodes(n):
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 collectLoopVars(c)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
408 collectLoopVars(node.target)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 for varn in vars:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 if (isinstance(self.scope.get(varn), Importation)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 # unused ones will get an unused import warning
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 and self.scope[varn].used):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 self.report(messages.ImportShadowedByLoopVar,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 node.lineno, varn, self.scope[varn].source.lineno)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 self.handleChildren(node)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 def NAME(self, node):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 Locate the name in locals / function / globals scopes.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 """
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
422 if isinstance(node.ctx, (_ast.Load, _ast.AugLoad)):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
423 # try local scope
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
424 importStarred = self.scope.importStarred
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 try:
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
426 self.scope[node.id].used = (self.scope, node.lineno)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 except KeyError:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 pass
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 else:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 return
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
432 # try enclosing function scopes
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
434 for scope in self.scopeStack[-2:0:-1]:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
435 importStarred = importStarred or scope.importStarred
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
436 if not isinstance(scope, FunctionScope):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
437 continue
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
438 try:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
439 scope[node.id].used = (self.scope, node.lineno)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
440 except KeyError:
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 pass
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 else:
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
443 return
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
444
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
445 # try global scope
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
447 importStarred = importStarred or self.scopeStack[0].importStarred
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
448 try:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
449 self.scopeStack[0][node.id].used = (self.scope, node.lineno)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
450 except KeyError:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
451 if ((not hasattr(__builtin__, node.id))
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
452 and node.id not in _MAGIC_GLOBALS
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
453 and not importStarred):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
454 if (os.path.basename(self.filename) == '__init__.py' and
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
455 node.id == '__path__'):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
456 # the special name __path__ is valid only in packages
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
457 pass
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
458 else:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
459 self.report(messages.UndefinedName, node.lineno, node.id)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
460 elif isinstance(node.ctx, (_ast.Store, _ast.AugStore)):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
461 # if the name hasn't already been defined in the current scope
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
462 if isinstance(self.scope, FunctionScope) and node.id not in self.scope:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
463 # for each function or module scope above us
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
464 for scope in self.scopeStack[:-1]:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
465 if not isinstance(scope, (FunctionScope, ModuleScope)):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
466 continue
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
467 # if the name was defined in that scope, and the name has
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
468 # been accessed already in the current scope, and hasn't
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
469 # been declared global
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
470 if (node.id in scope
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
471 and scope[node.id].used
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
472 and scope[node.id].used[0] is self.scope
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
473 and node.id not in self.scope.globals):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
474 # then it's probably a mistake
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
475 self.report(messages.UndefinedLocal,
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
476 scope[node.id].used[1],
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
477 node.id,
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
478 scope[node.id].source.lineno)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
479 break
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
480
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
481 if isinstance(node.parent,
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
482 (_ast.For, _ast.comprehension, _ast.Tuple, _ast.List)):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
483 binding = Binding(node.id, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
484 elif (node.id == '__all__' and
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
485 isinstance(self.scope, ModuleScope)):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
486 binding = ExportBinding(node.id, node.parent.value)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
487 else:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
488 binding = Assignment(node.id, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
489 if node.id in self.scope:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
490 binding.used = self.scope[node.id].used
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
491 self.addBinding(node.lineno, binding)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
492 elif isinstance(node.ctx, _ast.Del):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
493 if isinstance(self.scope, FunctionScope) and \
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
494 node.id in self.scope.globals:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
495 del self.scope.globals[node.id]
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
496 else:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
497 self.addBinding(node.lineno, UnBinding(node.id, node))
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
498 else:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
499 # must be a Param context -- this only happens for names in function
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
500 # arguments, but these aren't dispatched through here
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
501 raise RuntimeError(
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
502 "Got impossible expression context: %r" % (node.ctx,))
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
503
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
504 def FUNCTIONDEF(self, node):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
505 if hasattr(node, 'decorators'):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
506 for deco in node.decorators:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
507 self.handleNode(deco, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
508 else:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
509 for deco in node.decorator_list:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
510 self.handleNode(deco, node)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 self.addBinding(node.lineno, FunctionDefinition(node.name, node))
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 self.LAMBDA(node)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 def LAMBDA(self, node):
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
515 for default in node.args.defaults:
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 self.handleNode(default, node)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 def runFunction():
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 args = []
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 def addArgs(arglist):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 for arg in arglist:
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
523 if isinstance(arg, _ast.Tuple):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
524 addArgs(arg.elts)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 else:
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
526 if arg.id in args:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
527 self.report(messages.DuplicateArgument, node.lineno, arg.id)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
528 args.append(arg.id)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 self.pushFunctionScope()
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
531 addArgs(node.args.args)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
532 # vararg/kwarg identifiers are not Name nodes
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
533 if node.args.vararg:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
534 args.append(node.args.vararg)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
535 if node.args.kwarg:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
536 args.append(node.args.kwarg)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 for name in args:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 self.addBinding(node.lineno, Argument(name, node), reportRedef=False)
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
539 if isinstance(node.body, list):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
540 # case for FunctionDefs
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
541 for stmt in node.body:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
542 self.handleNode(stmt, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
543 else:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
544 # case for Lambdas
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
545 self.handleNode(node.body, node)
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 802
diff changeset
546
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 def checkUnusedAssignments():
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 Check to see if any assignments have not been used.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551 for name, binding in self.scope.iteritems():
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
552 if (not binding.used and not name in self.scope.globals
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 and isinstance(binding, Assignment)):
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 self.report(messages.UnusedVariable,
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
555 binding.source.lineno, name)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556 self.deferAssignment(checkUnusedAssignments)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557 self.popScope()
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 self.deferFunction(runFunction)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
561 def CLASSDEF(self, node):
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 """
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
563 Check names used in a class definition, including its decorators, base
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 classes, and the body of its definition. Additionally, add its name to
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 the current scope.
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 """
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
567 # decorator_list is present as of Python 2.6
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
568 for deco in getattr(node, 'decorator_list', []):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
569 self.handleNode(deco, node)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 for baseNode in node.bases:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 self.handleNode(baseNode, node)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 self.pushClassScope()
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
573 for stmt in node.body:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
574 self.handleNode(stmt, node)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 self.popScope()
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
576 self.addBinding(node.lineno, Binding(node.name, node))
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 def ASSIGN(self, node):
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
579 self.handleNode(node.value, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
580 for target in node.targets:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
581 self.handleNode(target, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
582
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
583 def AUGASSIGN(self, node):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
584 # AugAssign is awkward: must set the context explicitly and visit twice,
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
585 # once with AugLoad context, once with AugStore context
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
586 node.target.ctx = _ast.AugLoad()
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
587 self.handleNode(node.target, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
588 self.handleNode(node.value, node)
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
589 node.target.ctx = _ast.AugStore()
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
590 self.handleNode(node.target, node)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592 def IMPORT(self, node):
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
593 for alias in node.names:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
594 name = alias.asname or alias.name
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 importation = Importation(name, node)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 self.addBinding(node.lineno, importation)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
598 def IMPORTFROM(self, node):
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
599 if node.module == '__future__':
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 if not self.futuresAllowed:
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
601 self.report(messages.LateFutureImport, node.lineno,
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
602 [n.name for n in node.names])
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 else:
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 self.futuresAllowed = False
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
606 for alias in node.names:
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
607 if alias.name == '*':
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 self.scope.importStarred = True
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
609 self.report(messages.ImportStarUsed, node.lineno, node.module)
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 continue
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
611 name = alias.asname or alias.name
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 importation = Importation(name, node)
1535
f2650ea19bb8 Migrated py2flakes to v0.5.0 in order to fix issues for Python 2.7+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
613 if node.module == '__future__':
802
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
614 importation.used = (self.scope, node.lineno)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
615 self.addBinding(node.lineno, importation)
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617 #
e8882d16384c Added a pyflakes checker function for Python 2 files and made some additional Python 2 related changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 # eflag: FileType = Python2

eric ide

mercurial