mirror of
https://github.com/PyCQA/flake8.git
synced 2026-07-02 18:09:30 +00:00
Only honor noqa in comments
This commit is contained in:
parent
bb943328ef
commit
6e5c5cdc27
3 changed files with 36 additions and 1 deletions
|
|
@ -2,8 +2,10 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
|
import io
|
||||||
import linecache
|
import linecache
|
||||||
import logging
|
import logging
|
||||||
|
import tokenize
|
||||||
from re import Match
|
from re import Match
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
|
|
||||||
|
|
@ -16,7 +18,20 @@ LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
@functools.lru_cache(maxsize=512)
|
@functools.lru_cache(maxsize=512)
|
||||||
def _find_noqa(physical_line: str) -> Match[str] | None:
|
def _find_noqa(physical_line: str) -> Match[str] | None:
|
||||||
return defaults.NOQA_INLINE_REGEXP.search(physical_line)
|
comment_tokens = []
|
||||||
|
try:
|
||||||
|
tokens = tokenize.generate_tokens(io.StringIO(physical_line).readline)
|
||||||
|
for token in tokens:
|
||||||
|
if token.type == tokenize.COMMENT:
|
||||||
|
comment_tokens.append(token.string)
|
||||||
|
except tokenize.TokenError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
for comment in comment_tokens:
|
||||||
|
match = defaults.NOQA_INLINE_REGEXP.search(comment)
|
||||||
|
if match is not None:
|
||||||
|
return match
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class Violation(NamedTuple):
|
class Violation(NamedTuple):
|
||||||
|
|
|
||||||
|
|
@ -259,6 +259,25 @@ x = """
|
||||||
assert out == err == ""
|
assert out == err == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_noqa_in_string_is_not_ignored(tmpdir, capsys):
|
||||||
|
"""See https://github.com/pycqa/flake8/issues/1321."""
|
||||||
|
t_py_src = '''\
|
||||||
|
def f():
|
||||||
|
x = '# noqa'
|
||||||
|
'''
|
||||||
|
|
||||||
|
with tmpdir.as_cwd():
|
||||||
|
tmpdir.join("t.py").write(t_py_src)
|
||||||
|
assert cli.main(["t.py"]) == 1
|
||||||
|
|
||||||
|
expected = """\
|
||||||
|
t.py:2:5: F841 local variable 'x' is assigned to but never used
|
||||||
|
"""
|
||||||
|
out, err = capsys.readouterr()
|
||||||
|
assert out == expected
|
||||||
|
assert err == ""
|
||||||
|
|
||||||
|
|
||||||
def test_physical_line_file_not_ending_in_newline(tmpdir, capsys):
|
def test_physical_line_file_not_ending_in_newline(tmpdir, capsys):
|
||||||
"""See https://github.com/PyCQA/pycodestyle/issues/960."""
|
"""See https://github.com/PyCQA/pycodestyle/issues/960."""
|
||||||
t_py_src = "def f():\n\tpass"
|
t_py_src = "def f():\n\tpass"
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ from flake8.violation import Violation
|
||||||
("E111", "a = 1 # noqa - We do not care", True),
|
("E111", "a = 1 # noqa - We do not care", True),
|
||||||
("E111", "a = 1 # noqa: We do not care", True),
|
("E111", "a = 1 # noqa: We do not care", True),
|
||||||
("E111", "a = 1 # noqa:We do not care", True),
|
("E111", "a = 1 # noqa:We do not care", True),
|
||||||
|
("E111", "a = '# noqa'", False),
|
||||||
("ABC123", "a = 1 # noqa: ABC123", True),
|
("ABC123", "a = 1 # noqa: ABC123", True),
|
||||||
("E111", "a = 1 # noqa: ABC123", False),
|
("E111", "a = 1 # noqa: ABC123", False),
|
||||||
("ABC123", "a = 1 # noqa: ABC124", False),
|
("ABC123", "a = 1 # noqa: ABC124", False),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue