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

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7960
e8fc383322f7
child 8043
0acf98cd089a
child 8208
37836fa8e4ea
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
3 # Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
4 #
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
5 # Original (c) 2005-2010 Divmod, Inc.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
6 #
7960
e8fc383322f7 Harmonized some user visible strings and changed the term 'eric6' to the more generic 'eric'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
7 # This module is based on pyflakes but was modified to work with eric
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
8 """
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
9 Main module.
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
10
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
11 Implement the central Checker class.
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
12 Also, it models the Bindings and Scopes.
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
13 """
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
14 import __future__
6353
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
15 import ast
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
16 import bisect
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
17 import collections
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
18 import contextlib
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
19 import doctest
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
20 import functools
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
21 import os
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
22 import re
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
23 import string
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
24 import sys
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
25 import tokenize
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
26
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
27 from . import messages
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
28
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
29 PY2 = sys.version_info < (3, 0)
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
30 PY35_PLUS = sys.version_info >= (3, 5) # Python 3.5 and above
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
31 PY36_PLUS = sys.version_info >= (3, 6) # Python 3.6 and above
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
32 PY38_PLUS = sys.version_info >= (3, 8)
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
33 try:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
34 sys.pypy_version_info
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
35 PYPY = True
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
36 except AttributeError:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
37 PYPY = False
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
38
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
39 builtin_vars = dir(__import__('__builtin__' if PY2 else 'builtins'))
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
40
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
41 parse_format_string = string.Formatter().parse
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
42
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
43 if PY2:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
44 tokenize_tokenize = tokenize.generate_tokens
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
45 else:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
46 tokenize_tokenize = tokenize.tokenize
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
47
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
48 if PY2:
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
49 def getNodeType(node_class):
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
50 # workaround str.upper() which is locale-dependent
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
51 return str(unicode(node_class.__name__).upper()) # __IGNORE_WARNING__
6353
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
52
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
53 def get_raise_argument(node):
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
54 return node.type
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
55
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
56 else:
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
57 def getNodeType(node_class):
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
58 return node_class.__name__.upper()
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
59
6353
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
60 def get_raise_argument(node):
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
61 return node.exc
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
62
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
63 # Silence `pyflakes` from reporting `undefined name 'unicode'` in Python 3.
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
64 unicode = str
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
65
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
66 # Python >= 3.3 uses ast.Try instead of (ast.TryExcept + ast.TryFinally)
6353
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
67 if PY2:
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
68 def getAlternatives(n):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
69 if isinstance(n, (ast.If, ast.TryFinally)):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
70 return [n.body]
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
71 if isinstance(n, ast.TryExcept):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
72 return [n.body + n.orelse] + [[hdl] for hdl in n.handlers]
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
73 else:
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
74 def getAlternatives(n):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
75 if isinstance(n, ast.If):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
76 return [n.body]
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
77 if isinstance(n, ast.Try):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
78 return [n.body + n.orelse] + [[hdl] for hdl in n.handlers]
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
79
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
80 if PY35_PLUS:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
81 FOR_TYPES = (ast.For, ast.AsyncFor)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
82 LOOP_TYPES = (ast.While, ast.For, ast.AsyncFor)
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
83 FUNCTION_TYPES = (ast.FunctionDef, ast.AsyncFunctionDef)
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
84 else:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
85 FOR_TYPES = (ast.For,)
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
86 LOOP_TYPES = (ast.While, ast.For)
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
87 FUNCTION_TYPES = (ast.FunctionDef,)
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
88
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
89
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
90 if PY38_PLUS:
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
91 def _is_singleton(node): # type: (ast.AST) -> bool
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
92 return (
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
93 isinstance(node, ast.Constant) and
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
94 isinstance(node.value, (bool, type(Ellipsis), type(None)))
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
95 )
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
96 elif not PY2:
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
97 def _is_singleton(node): # type: (ast.AST) -> bool
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
98 return isinstance(node, (ast.NameConstant, ast.Ellipsis))
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
99 else:
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
100 def _is_singleton(node): # type: (ast.AST) -> bool
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
101 return (
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
102 isinstance(node, ast.Name) and
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
103 node.id in {'True', 'False', 'Ellipsis', 'None'}
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
104 )
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
105
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
106
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
107 def _is_tuple_constant(node): # type: (ast.AST) -> bool
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
108 return (
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
109 isinstance(node, ast.Tuple) and
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
110 all(_is_constant(elt) for elt in node.elts)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
111 )
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
112
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
113
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
114 if PY38_PLUS:
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
115 def _is_constant(node):
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
116 return isinstance(node, ast.Constant) or _is_tuple_constant(node)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
117 else:
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
118 _const_tps = (ast.Str, ast.Num)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
119 if not PY2:
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
120 _const_tps += (ast.Bytes,)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
121
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
122 def _is_constant(node):
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
123 return (
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
124 isinstance(node, _const_tps) or
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
125 _is_singleton(node) or
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
126 _is_tuple_constant(node)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
127 )
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
128
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
129
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
130 def _is_const_non_singleton(node): # type: (ast.AST) -> bool
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
131 return _is_constant(node) and not _is_singleton(node)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
132
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
133
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
134 # https://github.com/python/typed_ast/blob/1.4.0/ast27/Parser/tokenizer.c#L102-L104
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
135 TYPE_COMMENT_RE = re.compile(r'^#\s*type:\s*')
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
136 # https://github.com/python/typed_ast/blob/1.4.0/ast27/Parser/tokenizer.c#L1408-L1413
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
137 ASCII_NON_ALNUM = ''.join([chr(i) for i in range(128) if not chr(i).isalnum()])
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
138 TYPE_IGNORE_RE = re.compile(
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
139 TYPE_COMMENT_RE.pattern + r'ignore([{}]|$)'.format(ASCII_NON_ALNUM))
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
140 # https://github.com/python/typed_ast/blob/1.4.0/ast27/Grammar/Grammar#L147
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
141 TYPE_FUNC_RE = re.compile(r'^(\(.*?\))\s*->\s*(.*)$')
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
142
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
143
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
144 MAPPING_KEY_RE = re.compile(r'\(([^()]*)\)')
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
145 CONVERSION_FLAG_RE = re.compile('[#0+ -]*')
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
146 WIDTH_RE = re.compile(r'(?:\*|\d*)')
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
147 PRECISION_RE = re.compile(r'(?:\.(?:\*|\d*))?')
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
148 LENGTH_RE = re.compile('[hlL]?')
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
149 # https://docs.python.org/3/library/stdtypes.html#old-string-formatting
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
150 VALID_CONVERSIONS = frozenset('diouxXeEfFgGcrsa%')
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
151
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
152
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
153 def _must_match(regex, string, pos):
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
154 # type: (Pattern[str], str, int) -> Match[str]
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
155 match = regex.match(string, pos)
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
156 assert match is not None
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
157 return match
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
158
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
159
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
160 def parse_percent_format(s): # type: (str) -> Tuple[PercentFormat, ...]
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
161 """Parses the string component of a `'...' % ...` format call
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
162
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
163 Copied from https://github.com/asottile/pyupgrade at v1.20.1
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
164 """
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
165
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
166 def _parse_inner():
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
167 # type: () -> Generator[PercentFormat, None, None]
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
168 string_start = 0
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
169 string_end = 0
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
170 in_fmt = False
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
171
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
172 i = 0
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
173 while i < len(s):
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
174 if not in_fmt:
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
175 try:
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
176 i = s.index('%', i)
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
177 except ValueError: # no more % fields!
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
178 yield s[string_start:], None
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
179 return
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
180 else:
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
181 string_end = i
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
182 i += 1
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
183 in_fmt = True
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
184 else:
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
185 key_match = MAPPING_KEY_RE.match(s, i)
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
186 if key_match:
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
187 key = key_match.group(1) # type: Optional[str]
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
188 i = key_match.end()
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
189 else:
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
190 key = None
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
191
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
192 conversion_flag_match = _must_match(CONVERSION_FLAG_RE, s, i)
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
193 conversion_flag = conversion_flag_match.group() or None
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
194 i = conversion_flag_match.end()
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
195
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
196 width_match = _must_match(WIDTH_RE, s, i)
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
197 width = width_match.group() or None
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
198 i = width_match.end()
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
199
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
200 precision_match = _must_match(PRECISION_RE, s, i)
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
201 precision = precision_match.group() or None
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
202 i = precision_match.end()
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
203
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
204 # length modifier is ignored
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
205 i = _must_match(LENGTH_RE, s, i).end()
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
206
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
207 try:
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
208 conversion = s[i]
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
209 except IndexError:
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
210 raise ValueError('end-of-string while parsing format')
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
211 i += 1
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
212
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
213 fmt = (key, conversion_flag, width, precision, conversion)
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
214 yield s[string_start:string_end], fmt
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
215
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
216 in_fmt = False
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
217 string_start = i
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
218
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
219 if in_fmt:
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
220 raise ValueError('end-of-string while parsing format')
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
221
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
222 return tuple(_parse_inner())
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
223
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
224
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
225 class _FieldsOrder(dict):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
226 """Fix order of AST node fields."""
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
227
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
228 def _get_fields(self, node_class):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
229 # handle iter before target, and generators before element
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
230 fields = node_class._fields
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
231 if 'iter' in fields:
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
232 key_first = 'iter'.find
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
233 elif 'generators' in fields:
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
234 key_first = 'generators'.find
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
235 else:
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
236 key_first = 'value'.find
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
237 return tuple(sorted(fields, key=key_first, reverse=True))
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
238
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
239 def __missing__(self, node_class):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
240 self[node_class] = fields = self._get_fields(node_class)
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
241 return fields
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
242
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
243
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
244 def counter(items):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
245 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
246 Simplest required implementation of collections.Counter. Required as 2.6
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
247 does not have Counter in collections.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
248 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
249 results = {}
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
250 for item in items:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
251 results[item] = results.get(item, 0) + 1
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
252 return results
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
253
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
254
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
255 def iter_child_nodes(node, omit=None, _fields_order=_FieldsOrder()):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
256 """
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
257 Yield all direct child nodes of *node*, that is, all fields that
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
258 are nodes and all items of fields that are lists of nodes.
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
259
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
260 :param node: AST node to be iterated upon
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
261 :param omit: String or tuple of strings denoting the
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
262 attributes of the node to be omitted from
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
263 further parsing
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
264 :param _fields_order: Order of AST node fields
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
265 """
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
266 for name in _fields_order[node.__class__]:
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
267 if omit and name in omit:
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
268 continue
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
269 field = getattr(node, name, None)
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
270 if isinstance(field, ast.AST):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
271 yield field
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
272 elif isinstance(field, list):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
273 for item in field:
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
274 yield item
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
275
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
276
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
277 def convert_to_value(item):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
278 if isinstance(item, ast.Str):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
279 return item.s
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
280 elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
281 return item.s
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
282 elif isinstance(item, ast.Tuple):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
283 return tuple(convert_to_value(i) for i in item.elts)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
284 elif isinstance(item, ast.Num):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
285 return item.n
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
286 elif isinstance(item, ast.Name):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
287 result = VariableKey(item=item)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
288 constants_lookup = {
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
289 'True': True,
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
290 'False': False,
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
291 'None': None,
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
292 }
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
293 return constants_lookup.get(
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
294 result.name,
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
295 result,
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
296 )
6353
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
297 elif (not PY2) and isinstance(item, ast.NameConstant):
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
298 # None, True, False are nameconstants in python3, but names in 2
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
299 return item.value
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
300 else:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
301 return UnhandledKeyType()
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
302
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
303
6353
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
304 def is_notimplemented_name_node(node):
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
305 return isinstance(node, ast.Name) and getNodeName(node) == 'NotImplemented'
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
306
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
307
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
308 class Binding(object):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
309 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
310 Represents the binding of a value to a name.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
311
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
312 The checker uses this to keep track of which names have been bound and
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
313 which names have not. See L{Assignment} for a special type of binding that
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
314 is checked with stricter rules.
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
315
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
316 @ivar used: pair of (L{Scope}, node) indicating the scope and
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
317 the node that this binding was last used.
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
318 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
319
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
320 def __init__(self, name, source):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
321 self.name = name
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
322 self.source = source
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
323 self.used = False
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
324
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
325 def __str__(self):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
326 return self.name
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
327
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
328 def __repr__(self):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
329 return '<%s object %r from line %r at 0x%x>' % (self.__class__.__name__,
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
330 self.name,
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
331 self.source.lineno,
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
332 id(self))
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
333
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
334 def redefines(self, other):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
335 return isinstance(other, Definition) and self.name == other.name
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
336
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
337
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
338 class Definition(Binding):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
339 """
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
340 A binding that defines a function or a class.
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
341 """
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
342
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
343
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
344 class Builtin(Definition):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
345 """A definition created for all Python builtins."""
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
346
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
347 def __init__(self, name):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
348 super(Builtin, self).__init__(name, None)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
349
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
350 def __repr__(self):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
351 return '<%s object %r at 0x%x>' % (self.__class__.__name__,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
352 self.name,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
353 id(self))
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
354
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
355
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
356 class UnhandledKeyType(object):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
357 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
358 A dictionary key of a type that we cannot or do not check for duplicates.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
359 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
360
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
361
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
362 class VariableKey(object):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
363 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
364 A dictionary key which is a variable.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
365
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
366 @ivar item: The variable AST object.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
367 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
368 def __init__(self, item):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
369 self.name = item.id
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
370
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
371 def __eq__(self, compare):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
372 return (
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
373 compare.__class__ == self.__class__ and
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
374 compare.name == self.name
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
375 )
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
376
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
377 def __hash__(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
378 return hash(self.name)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
379
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
380
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
381 class Importation(Definition):
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
382 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
383 A binding created by an import statement.
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
384
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
385 @ivar fullName: The complete name given to the import statement,
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
386 possibly including multiple dotted components.
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
387 @type fullName: C{str}
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
388 """
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
389
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
390 def __init__(self, name, source, full_name=None):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
391 self.fullName = full_name or name
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
392 self.redefined = []
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
393 super(Importation, self).__init__(name, source)
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
394
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
395 def redefines(self, other):
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
396 if isinstance(other, SubmoduleImportation):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
397 # See note in SubmoduleImportation about RedefinedWhileUnused
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
398 return self.fullName == other.fullName
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
399 return isinstance(other, Definition) and self.name == other.name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
400
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
401 def _has_alias(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
402 """Return whether importation needs an as clause."""
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
403 return not self.fullName.split('.')[-1] == self.name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
404
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
405 @property
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
406 def source_statement(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
407 """Generate a source statement equivalent to the import."""
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
408 if self._has_alias():
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
409 return 'import %s as %s' % (self.fullName, self.name)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
410 else:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
411 return 'import %s' % self.fullName
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
412
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
413 def __str__(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
414 """Return import full name with alias."""
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
415 if self._has_alias():
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
416 return self.fullName + ' as ' + self.name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
417 else:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
418 return self.fullName
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
419
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
420
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
421 class SubmoduleImportation(Importation):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
422 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
423 A binding created by a submodule import statement.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
424
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
425 A submodule import is a special case where the root module is implicitly
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
426 imported, without an 'as' clause, and the submodule is also imported.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
427 Python does not restrict which attributes of the root module may be used.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
428
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
429 This class is only used when the submodule import is without an 'as' clause.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
430
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
431 pyflakes handles this case by registering the root module name in the scope,
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
432 allowing any attribute of the root module to be accessed.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
433
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
434 RedefinedWhileUnused is suppressed in `redefines` unless the submodule
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
435 name is also the same, to avoid false positives.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
436 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
437
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
438 def __init__(self, name, source):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
439 # A dot should only appear in the name when it is a submodule import
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
440 assert '.' in name and (not source or isinstance(source, ast.Import))
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
441 package_name = name.split('.')[0]
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
442 super(SubmoduleImportation, self).__init__(package_name, source)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
443 self.fullName = name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
444
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
445 def redefines(self, other):
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
446 if isinstance(other, Importation):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
447 return self.fullName == other.fullName
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
448 return super(SubmoduleImportation, self).redefines(other)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
449
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
450 def __str__(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
451 return self.fullName
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
452
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
453 @property
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
454 def source_statement(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
455 return 'import ' + self.fullName
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
456
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
457
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
458 class ImportationFrom(Importation):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
459
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
460 def __init__(self, name, source, module, real_name=None):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
461 self.module = module
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
462 self.real_name = real_name or name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
463
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
464 if module.endswith('.'):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
465 full_name = module + self.real_name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
466 else:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
467 full_name = module + '.' + self.real_name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
468
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
469 super(ImportationFrom, self).__init__(name, source, full_name)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
470
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
471 def __str__(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
472 """Return import full name with alias."""
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
473 if self.real_name != self.name:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
474 return self.fullName + ' as ' + self.name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
475 else:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
476 return self.fullName
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
477
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
478 @property
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
479 def source_statement(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
480 if self.real_name != self.name:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
481 return 'from %s import %s as %s' % (self.module,
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
482 self.real_name,
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
483 self.name)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
484 else:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
485 return 'from %s import %s' % (self.module, self.name)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
486
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
487
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
488 class StarImportation(Importation):
5510
cdcd0cd34e79 Upgraded pyflakes to version 1.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5389
diff changeset
489 """A binding created by a 'from x import *' statement."""
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
490
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
491 def __init__(self, name, source):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
492 super(StarImportation, self).__init__('*', source)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
493 # Each star importation needs a unique name, and
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
494 # may not be the module name otherwise it will be deemed imported
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
495 self.name = name + '.*'
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
496 self.fullName = name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
497
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
498 @property
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
499 def source_statement(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
500 return 'from ' + self.fullName + ' import *'
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
501
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
502 def __str__(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
503 # When the module ends with a ., avoid the ambiguous '..*'
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
504 if self.fullName.endswith('.'):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
505 return self.source_statement
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
506 else:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
507 return self.name
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
508
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
509
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
510 class FutureImportation(ImportationFrom):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
511 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
512 A binding created by a from `__future__` import statement.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
513
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
514 `__future__` imports are implicitly used.
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
515 """
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
516
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
517 def __init__(self, name, source, scope):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
518 super(FutureImportation, self).__init__(name, source, '__future__')
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
519 self.used = (scope, source)
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
520
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
521
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
522 class Argument(Binding):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
523 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
524 Represents binding a name as an argument.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
525 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
526
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
527
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
528 class Assignment(Binding):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
529 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
530 Represents binding a name with an explicit assignment.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
531
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
532 The checker will raise warnings for any Assignment that isn't used. Also,
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
533 the checker does not consider assignments in tuple/list unpacking to be
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
534 Assignments, rather it treats them as simple Bindings.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
535 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
536
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
537
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
538 class FunctionDefinition(Definition):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
539 pass
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
540
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
541
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
542 class ClassDefinition(Definition):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
543 pass
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
544
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
545
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
546 class ExportBinding(Binding):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
547 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
548 A binding created by an C{__all__} assignment. If the names in the list
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
549 can be determined statically, they will be treated as names for export and
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
550 additional checking applied to them.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
551
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
552 The only recognized C{__all__} assignment via list concatenation is in the
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
553 following format:
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
554
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
555 __all__ = ['a'] + ['b'] + ['c']
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
556
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
557 Names which are imported and not otherwise used but appear in the value of
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
558 C{__all__} will not have an unused import warning reported for them.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
559 """
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
560
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
561 def __init__(self, name, source, scope):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
562 if '__all__' in scope and isinstance(source, ast.AugAssign):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
563 self.names = list(scope['__all__'].names)
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
564 else:
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
565 self.names = []
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
566
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
567 def _add_to_names(container):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
568 for node in container.elts:
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
569 if isinstance(node, ast.Str):
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
570 self.names.append(node.s)
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
571
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
572 if isinstance(source.value, (ast.List, ast.Tuple)):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
573 _add_to_names(source.value)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
574 # If concatenating lists
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
575 elif isinstance(source.value, ast.BinOp):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
576 currentValue = source.value
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
577 while isinstance(currentValue.right, ast.List):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
578 left = currentValue.left
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
579 right = currentValue.right
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
580 _add_to_names(right)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
581 # If more lists are being added
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
582 if isinstance(left, ast.BinOp):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
583 currentValue = left
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
584 # If just two lists are being added
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
585 elif isinstance(left, ast.List):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
586 _add_to_names(left)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
587 # All lists accounted for - done
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
588 break
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
589 # If not list concatenation
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
590 else:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
591 break
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
592 super(ExportBinding, self).__init__(name, source)
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
593
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
594
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
595 class Scope(dict):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
596 importStarred = False # set to True when import * is found
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
597
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
598 def __repr__(self):
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
599 scope_cls = self.__class__.__name__
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
600 return '<%s at 0x%x %s>' % (scope_cls, id(self), dict.__repr__(self))
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
601
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
602
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
603 class ClassScope(Scope):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
604 pass
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
605
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
606
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
607 class FunctionScope(Scope):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
608 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
609 I represent a name scope for a function.
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
610
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
611 @ivar globals: Names declared 'global' in this function.
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
612 """
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
613 usesLocals = False
6353
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
614 alwaysUsed = {'__tracebackhide__', '__traceback_info__',
6a0f3abd6878 upgraded pyflakes to version 2.0.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
615 '__traceback_supplement__'}
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
616
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
617 def __init__(self):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
618 super(FunctionScope, self).__init__()
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
619 # Simplify: manage the special locals as globals
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
620 self.globals = self.alwaysUsed.copy()
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
621 self.returnValue = None # First non-empty return
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
622 self.isGenerator = False # Detect a generator
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
623
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
624 def unusedAssignments(self):
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
625 """
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
626 Return a generator for the assignments which have not been used.
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
627 """
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
628 for name, binding in self.items():
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
629 if (not binding.used and
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
630 name != '_' and # see issue #202
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
631 name not in self.globals and
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
632 not self.usesLocals and
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
633 isinstance(binding, Assignment)):
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
634 yield name, binding
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
635
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
636
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
637 class GeneratorScope(Scope):
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
638 pass
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
639
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
640
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
641 class ModuleScope(Scope):
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
642 """Scope for a module."""
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
643 _futures_allowed = True
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
644 _annotations_future_enabled = False
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
645
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
646
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
647 class DoctestScope(ModuleScope):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
648 """Scope for a doctest."""
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
649
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
650
7064
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
651 class DummyNode(object):
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
652 """Used in place of an `ast.AST` to set error message positions"""
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
653 def __init__(self, lineno, col_offset):
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
654 self.lineno = lineno
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
655 self.col_offset = col_offset
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
656
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
657
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
658 # Globally defined names which are not attributes of the builtins module, or
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
659 # are only present on some platforms.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
660 _MAGIC_GLOBALS = ['__file__', '__builtins__', 'WindowsError']
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
661 # module scope annotation will store in `__annotations__`, see also PEP 526.
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
662 if PY36_PLUS:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
663 _MAGIC_GLOBALS.append('__annotations__')
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
664
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
665
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
666 def getNodeName(node):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
667 # Returns node.id, or node.name, or None
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
668 if hasattr(node, 'id'): # One of the many nodes with an id
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
669 return node.id
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
670 if hasattr(node, 'name'): # an ExceptHandler node
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
671 return node.name
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
672
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
673
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
674 TYPING_MODULES = frozenset(('typing', 'typing_extensions'))
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
675
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
676
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
677 def _is_typing_helper(node, is_name_match_fn, scope_stack):
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
678 """
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
679 Internal helper to determine whether or not something is a member of a
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
680 typing module. This is used as part of working out whether we are within a
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
681 type annotation context.
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
682
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
683 Note: you probably don't want to use this function directly. Instead see the
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
684 utils below which wrap it (`_is_typing` and `_is_any_typing_member`).
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
685 """
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
686
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
687 def _bare_name_is_attr(name):
7064
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
688 for scope in reversed(scope_stack):
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
689 if name in scope:
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
690 return (
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
691 isinstance(scope[name], ImportationFrom) and
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
692 scope[name].module in TYPING_MODULES and
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
693 is_name_match_fn(scope[name].real_name)
7064
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
694 )
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
695
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
696 return False
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
697
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
698 return (
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
699 (
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
700 isinstance(node, ast.Name) and
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
701 _bare_name_is_attr(node.id)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
702 ) or (
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
703 isinstance(node, ast.Attribute) and
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
704 isinstance(node.value, ast.Name) and
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
705 node.value.id in TYPING_MODULES and
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
706 is_name_match_fn(node.attr)
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
707 )
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
708 )
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
709
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
710
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
711 def _is_typing(node, typing_attr, scope_stack):
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
712 """
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
713 Determine whether `node` represents the member of a typing module specified
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
714 by `typing_attr`.
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
715
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
716 This is used as part of working out whether we are within a type annotation
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
717 context.
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
718 """
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
719 return _is_typing_helper(node, lambda x: x == typing_attr, scope_stack)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
720
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
721
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
722 def _is_any_typing_member(node, scope_stack):
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
723 """
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
724 Determine whether `node` represents any member of a typing module.
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
725
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
726 This is used as part of working out whether we are within a type annotation
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
727 context.
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
728 """
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
729 return _is_typing_helper(node, lambda x: True, scope_stack)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
730
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
731
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
732 def is_typing_overload(value, scope_stack):
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
733 return (
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
734 isinstance(value.source, FUNCTION_TYPES) and
7064
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
735 any(
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
736 _is_typing(dec, 'overload', scope_stack)
7064
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
737 for dec in value.source.decorator_list
1010f737def2 pyflakes: upgraded to version 2.1.1 plus repository changes to fix some issues (until 2019-07-09).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7060
diff changeset
738 )
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
739 )
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
740
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
741
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
742 def in_annotation(func):
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
743 @functools.wraps(func)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
744 def in_annotation_func(self, *args, **kwargs):
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
745 with self._enter_annotation():
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
746 return func(self, *args, **kwargs)
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
747 return in_annotation_func
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
748
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
749
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
750 def make_tokens(code):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
751 # PY3: tokenize.tokenize requires readline of bytes
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
752 if not isinstance(code, bytes):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
753 code = code.encode('UTF-8')
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
754 lines = iter(code.splitlines(True))
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
755 # next(lines, b'') is to prevent an error in pypy3
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
756 return tuple(tokenize_tokenize(lambda: next(lines, b'')))
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
757
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
758
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
759 class _TypeableVisitor(ast.NodeVisitor):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
760 """Collect the line number and nodes which are deemed typeable by
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
761 PEP 484
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
762
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
763 https://www.python.org/dev/peps/pep-0484/#type-comments
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
764 """
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
765 def __init__(self):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
766 self.typeable_lines = [] # type: List[int]
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
767 self.typeable_nodes = {} # type: Dict[int, ast.AST]
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
768
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
769 def _typeable(self, node):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
770 # if there is more than one typeable thing on a line last one wins
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
771 self.typeable_lines.append(node.lineno)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
772 self.typeable_nodes[node.lineno] = node
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
773
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
774 self.generic_visit(node)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
775
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
776 visit_Assign = visit_For = visit_FunctionDef = visit_With = _typeable
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
777 visit_AsyncFor = visit_AsyncFunctionDef = visit_AsyncWith = _typeable
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
778
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
779
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
780 def _collect_type_comments(tree, tokens):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
781 visitor = _TypeableVisitor()
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
782 visitor.visit(tree)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
783
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
784 type_comments = collections.defaultdict(list)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
785 for tp, text, start, _, _ in tokens:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
786 if (
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
787 tp != tokenize.COMMENT or # skip non comments
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
788 not TYPE_COMMENT_RE.match(text) or # skip non-type comments
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
789 TYPE_IGNORE_RE.match(text) # skip ignores
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
790 ):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
791 continue
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
792
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
793 # search for the typeable node at or before the line number of the
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
794 # type comment.
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
795 # if the bisection insertion point is before any nodes this is an
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
796 # invalid type comment which is ignored.
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
797 lineno, _ = start
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
798 idx = bisect.bisect_right(visitor.typeable_lines, lineno)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
799 if idx == 0:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
800 continue
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
801 node = visitor.typeable_nodes[visitor.typeable_lines[idx - 1]]
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
802 type_comments[node].append((start, text))
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
803
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
804 return type_comments
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
805
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
806
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
807 class Checker(object):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
808 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
809 I check the cleanliness and sanity of Python code.
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
810
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
811 @ivar _deferredFunctions: Tracking list used by L{deferFunction}. Elements
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
812 of the list are two-tuples. The first element is the callable passed
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
813 to L{deferFunction}. The second element is a copy of the scope stack
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
814 at the time L{deferFunction} was called.
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
815
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
816 @ivar _deferredAssignments: Similar to C{_deferredFunctions}, but for
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
817 callables which are deferred assignment checks.
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
818 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
819
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
820 _ast_node_scope = {
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
821 ast.Module: ModuleScope,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
822 ast.ClassDef: ClassScope,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
823 ast.FunctionDef: FunctionScope,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
824 ast.Lambda: FunctionScope,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
825 ast.ListComp: GeneratorScope,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
826 ast.SetComp: GeneratorScope,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
827 ast.GeneratorExp: GeneratorScope,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
828 ast.DictComp: GeneratorScope,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
829 }
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
830 if PY35_PLUS:
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
831 _ast_node_scope[ast.AsyncFunctionDef] = FunctionScope
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
832
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
833 nodeDepth = 0
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
834 offset = None
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
835 traceTree = False
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
836 _in_annotation = False
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
837 _in_typing_literal = False
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
838 _in_deferred = False
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
839
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
840 builtIns = set(builtin_vars).union(_MAGIC_GLOBALS)
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
841 _customBuiltIns = os.environ.get('PYFLAKES_BUILTINS')
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
842 if _customBuiltIns:
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
843 builtIns.update(_customBuiltIns.split(','))
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
844 del _customBuiltIns
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
845
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
846 # TODO: file_tokens= is required to perform checks on type comments,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
847 # eventually make this a required positional argument. For now it
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
848 # is defaulted to `()` for api compatibility.
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
849 def __init__(self, tree, filename='(none)', builtins=None,
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
850 withDoctest='PYFLAKES_DOCTEST' in os.environ, file_tokens=()):
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
851 self._nodeHandlers = {}
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
852 self._deferredFunctions = []
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
853 self._deferredAssignments = []
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
854 self.deadScopes = []
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
855 self.messages = []
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
856 self.filename = filename
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
857 if builtins:
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
858 self.builtIns = self.builtIns.union(builtins)
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
859 self.withDoctest = withDoctest
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
860 try:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
861 self.scopeStack = [Checker._ast_node_scope[type(tree)]()]
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
862 except KeyError:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
863 raise RuntimeError('No scope implemented for the node %r' % tree)
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
864 self.exceptHandlers = [()]
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
865 self.root = tree
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
866 self._type_comments = _collect_type_comments(tree, file_tokens)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
867 for builtin in self.builtIns:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
868 self.addBinding(None, Builtin(builtin))
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
869 self.handleChildren(tree)
7617
a0e162a50ad7 Syntax Checker: updated pyflakes to version 2.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7395
diff changeset
870 self._in_deferred = True
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
871 self.runDeferred(self._deferredFunctions)
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
872 # Set _deferredFunctions to None so that deferFunction will fail
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
873 # noisily if called after we've run through the deferred functions.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
874 self._deferredFunctions = None
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
875 self.runDeferred(self._deferredAssignments)
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
876 # Set _deferredAssignments to None so that deferAssignment will fail
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
877 # noisily if called after we've run through the deferred assignments.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
878 self._deferredAssignments = None
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
879 del self.scopeStack[1:]
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
880 self.popScope()
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
881 self.checkDeadScopes()
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
882
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
883 def deferFunction(self, callable):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
884 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
885 Schedule a function handler to be called just before completion.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
886
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
887 This is used for handling function bodies, which must be deferred
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
888 because code later in the file might modify the global scope. When
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
889 `callable` is called, the scope at the time this is called will be
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
890 restored, however it will contain any new bindings added to it.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
891 """
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
892 self._deferredFunctions.append((callable, self.scopeStack[:], self.offset))
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
893
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
894 def deferAssignment(self, callable):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
895 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
896 Schedule an assignment handler to be called just after deferred
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
897 function handlers.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
898 """
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
899 self._deferredAssignments.append((callable, self.scopeStack[:], self.offset))
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
900
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
901 def runDeferred(self, deferred):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
902 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
903 Run the callables in C{deferred} using their associated scope stack.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
904 """
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
905 for handler, scope, offset in deferred:
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
906 self.scopeStack = scope
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
907 self.offset = offset
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
908 handler()
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
909
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
910 def _in_doctest(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
911 return (len(self.scopeStack) >= 2 and
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
912 isinstance(self.scopeStack[1], DoctestScope))
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
913
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
914 @property
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
915 def futuresAllowed(self):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
916 if not all(isinstance(scope, ModuleScope)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
917 for scope in self.scopeStack):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
918 return False
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
919
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
920 return self.scope._futures_allowed
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
921
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
922 @futuresAllowed.setter
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
923 def futuresAllowed(self, value):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
924 assert value is False
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
925 if isinstance(self.scope, ModuleScope):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
926 self.scope._futures_allowed = False
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
927
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
928 @property
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
929 def annotationsFutureEnabled(self):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
930 scope = self.scopeStack[0]
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
931 if not isinstance(scope, ModuleScope):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
932 return False
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
933 return scope._annotations_future_enabled
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
934
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
935 @annotationsFutureEnabled.setter
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
936 def annotationsFutureEnabled(self, value):
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
937 assert value is True
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
938 assert isinstance(self.scope, ModuleScope)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
939 self.scope._annotations_future_enabled = True
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
940
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
941 @property
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
942 def scope(self):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
943 return self.scopeStack[-1]
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
944
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
945 def popScope(self):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
946 self.deadScopes.append(self.scopeStack.pop())
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
947
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
948 def checkDeadScopes(self):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
949 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
950 Look at scopes which have been fully examined and report names in them
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
951 which were imported but unused.
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
952 """
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
953 for scope in self.deadScopes:
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
954 # imports in classes are public members
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
955 if isinstance(scope, ClassScope):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
956 continue
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
957
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
958 all_binding = scope.get('__all__')
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
959 if all_binding and not isinstance(all_binding, ExportBinding):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
960 all_binding = None
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
961
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
962 if all_binding:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
963 all_names = set(all_binding.names)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
964 undefined = all_names.difference(scope)
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
965 else:
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
966 all_names = undefined = []
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
967
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
968 if undefined:
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
969 if not scope.importStarred and \
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
970 os.path.basename(self.filename) != '__init__.py':
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
971 # Look for possible mistakes in the export list
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
972 for name in undefined:
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
973 self.report(messages.UndefinedExport,
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
974 scope['__all__'].source, name)
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
975
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
976 # mark all import '*' as used by the undefined in __all__
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
977 if scope.importStarred:
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
978 from_list = []
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
979 for binding in scope.values():
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
980 if isinstance(binding, StarImportation):
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
981 binding.used = all_binding
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
982 from_list.append(binding.fullName)
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
983 # report * usage, with a list of possible sources
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
984 from_list = ', '.join(sorted(from_list))
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
985 for name in undefined:
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
986 self.report(messages.ImportStarUsage,
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
987 scope['__all__'].source, name, from_list)
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
988
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
989 # Look for imported names that aren't used.
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
990 for value in scope.values():
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
991 if isinstance(value, Importation):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
992 used = value.used or value.name in all_names
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
993 if not used:
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
994 messg = messages.UnusedImport
5067
e2f171f08af8 Updated pyflakes to 1.2.3+.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5065
diff changeset
995 self.report(messg, value.source, str(value))
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
996 for node in value.redefined:
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
997 if isinstance(self.getParent(node), FOR_TYPES):
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
998 messg = messages.ImportShadowedByLoopVar
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
999 elif used:
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1000 continue
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1001 else:
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1002 messg = messages.RedefinedWhileUnused
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1003 self.report(messg, node, value.name, value.source)
3177
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
1004
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
1005 def pushScope(self, scopeClass=FunctionScope):
5af61402d74d Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
1006 self.scopeStack.append(scopeClass())
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1007
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1008 def report(self, messageClass, *args, **kwargs):
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1009 self.messages.append(messageClass(self.filename, *args, **kwargs))
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1010
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1011 def getParent(self, node):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1012 # Lookup the first parent which is not Tuple, List or Starred
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1013 while True:
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1014 node = node._pyflakes_parent
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1015 if not hasattr(node, 'elts') and not hasattr(node, 'ctx'):
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1016 return node
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1017
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1018 def getCommonAncestor(self, lnode, rnode, stop):
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1019 if (
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1020 stop in (lnode, rnode) or
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1021 not (
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1022 hasattr(lnode, '_pyflakes_parent') and
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1023 hasattr(rnode, '_pyflakes_parent')
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1024 )
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1025 ):
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1026 return None
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1027 if lnode is rnode:
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1028 return lnode
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1029
7395
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1030 if (lnode._pyflakes_depth > rnode._pyflakes_depth):
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1031 return self.getCommonAncestor(lnode._pyflakes_parent, rnode, stop)
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1032 if (lnode._pyflakes_depth < rnode._pyflakes_depth):
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1033 return self.getCommonAncestor(lnode, rnode._pyflakes_parent, stop)
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1034 return self.getCommonAncestor(
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1035 lnode._pyflakes_parent,
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1036 rnode._pyflakes_parent,
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1037 stop,
dd50d0f4c588 pyflakes: updated to repository as of 2020-02-03.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
1038 )
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1039
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1040 def descendantOf(self, node, ancestors, stop):
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1041 for a in ancestors:
3544
431c842fd09a updated pyflakes to version 0.8.1 (Python 3.4.0 compatible)
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3456
diff changeset
1042 if self.getCommonAncestor(node, a, stop):
2571
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1043 return True
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1044 return False
e6bb19eb87ea Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1045
6742
7cb30f7f94f6 Upgraded pyflakes to 2.1.0
T.Rzepka&