Skip to content

Commit 4ed1ba4

Browse files
authored
Make pyflakes more resistant to syntax additions (#490)
1 parent be88036 commit 4ed1ba4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pyflakes/checker.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,12 +1009,30 @@ def addBinding(self, node, value):
10091009

10101010
self.scope[value.name] = value
10111011

1012+
def _unknown_handler(self, node):
1013+
# this environment variable configures whether to error on unknown
1014+
# ast types.
1015+
#
1016+
# this is silent by default but the error is enabled for the pyflakes
1017+
# testsuite.
1018+
#
1019+
# this allows new syntax to be added to python without *requiring*
1020+
# changes from the pyflakes side. but will still produce an error
1021+
# in the pyflakes testsuite (so more specific handling can be added if
1022+
# needed).
1023+
if os.environ.get('PYFLAKES_ERROR_UNKNOWN'):
1024+
raise NotImplementedError('Unexpected type: {}'.format(type(node)))
1025+
else:
1026+
self.handleChildren(node)
1027+
10121028
def getNodeHandler(self, node_class):
10131029
try:
10141030
return self._nodeHandlers[node_class]
10151031
except KeyError:
10161032
nodeType = getNodeType(node_class)
1017-
self._nodeHandlers[node_class] = handler = getattr(self, nodeType)
1033+
self._nodeHandlers[node_class] = handler = getattr(
1034+
self, nodeType, self._unknown_handler,
1035+
)
10181036
return handler
10191037

10201038
def handleNodeLoad(self, node):

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ envlist =
55

66
[testenv]
77
deps = flake8==3.6.0
8+
setenv = PYFLAKES_ERROR_UNKNOWN=1
89
commands =
910
python -m unittest discover pyflakes
1011
flake8 pyflakes setup.py

0 commit comments

Comments
 (0)