mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-10 14:54:17 +00:00
Fix reporting of UndefinedLocal pyflakes error
This commit is contained in:
parent
79148a02ee
commit
8df38c92b9
2 changed files with 23 additions and 5 deletions
|
|
@ -57,10 +57,7 @@ def patch_pyflakes():
|
||||||
"""Add error codes to Pyflakes messages."""
|
"""Add error codes to Pyflakes messages."""
|
||||||
for name, obj in vars(pyflakes.messages).items():
|
for name, obj in vars(pyflakes.messages).items():
|
||||||
if name[0].isupper() and obj.message:
|
if name[0].isupper() and obj.message:
|
||||||
obj.flake8_msg = "%s %s" % (
|
obj.flake8_code = FLAKE8_PYFLAKES_CODES.get(name, "F999")
|
||||||
FLAKE8_PYFLAKES_CODES.get(name, "F999"),
|
|
||||||
obj.message,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
patch_pyflakes()
|
patch_pyflakes()
|
||||||
|
|
@ -188,6 +185,9 @@ class FlakesChecker(pyflakes.checker.Checker):
|
||||||
yield (
|
yield (
|
||||||
message.lineno,
|
message.lineno,
|
||||||
col,
|
col,
|
||||||
(message.flake8_msg % message.message_args),
|
"{} {}".format(
|
||||||
|
message.flake8_code,
|
||||||
|
message.message % message.message_args,
|
||||||
|
),
|
||||||
message.__class__,
|
message.__class__,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
"""Tests of pyflakes monkey patches."""
|
"""Tests of pyflakes monkey patches."""
|
||||||
|
import ast
|
||||||
|
|
||||||
import pyflakes
|
import pyflakes
|
||||||
|
|
||||||
|
|
@ -13,3 +14,20 @@ def test_all_pyflakes_messages_have_flake8_codes_assigned():
|
||||||
if name[0].isupper() and obj.message
|
if name[0].isupper() and obj.message
|
||||||
}
|
}
|
||||||
assert messages == set(pyflakes_shim.FLAKE8_PYFLAKES_CODES)
|
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",
|
||||||
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue