Fix reporting of UndefinedLocal pyflakes error

This commit is contained in:
Anthony Sottile 2019-01-31 21:53:19 -08:00
parent 79148a02ee
commit 8df38c92b9
2 changed files with 23 additions and 5 deletions

View file

@ -57,10 +57,7 @@ def patch_pyflakes():
"""Add error codes to Pyflakes messages."""
for name, obj in vars(pyflakes.messages).items():
if name[0].isupper() and obj.message:
obj.flake8_msg = "%s %s" % (
FLAKE8_PYFLAKES_CODES.get(name, "F999"),
obj.message,
)
obj.flake8_code = FLAKE8_PYFLAKES_CODES.get(name, "F999")
patch_pyflakes()
@ -188,6 +185,9 @@ class FlakesChecker(pyflakes.checker.Checker):
yield (
message.lineno,
col,
(message.flake8_msg % message.message_args),
"{} {}".format(
message.flake8_code,
message.message % message.message_args,
),
message.__class__,
)

View file

@ -1,4 +1,5 @@
"""Tests of pyflakes monkey patches."""
import ast
import pyflakes
@ -13,3 +14,20 @@ def test_all_pyflakes_messages_have_flake8_codes_assigned():
if name[0].isupper() and obj.message
}
assert messages == set(pyflakes_shim.FLAKE8_PYFLAKES_CODES)
def test_undefined_local_code():
"""In pyflakes 2.1.0 this code's string formatting was changed."""
src = '''\
import sys
def f():
sys = sys
'''
tree = ast.parse(src)
checker = pyflakes_shim.FlakesChecker(tree, (), 't.py')
message_texts = [s for _, _, s, _ in checker.run()]
assert message_texts == [
"F823 local variable 'sys' defined in enclosing scope on line 1 referenced before assignment", # noqa: E501
"F841 local variable 'sys' is assigned to but never used",
]