mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-11 15:24:18 +00:00
Merge pull request #1832 from PyCQA/fstring-middle
mute FSTRING_MIDDLE tokens
This commit is contained in:
commit
1f8437433e
2 changed files with 39 additions and 1 deletions
|
|
@ -5,6 +5,7 @@ import argparse
|
||||||
import ast
|
import ast
|
||||||
import contextlib
|
import contextlib
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
import tokenize
|
import tokenize
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
|
|
@ -178,7 +179,7 @@ class FileProcessor:
|
||||||
self.blank_lines = 0
|
self.blank_lines = 0
|
||||||
self.tokens = []
|
self.tokens = []
|
||||||
|
|
||||||
def build_logical_line_tokens(self) -> _Logical:
|
def build_logical_line_tokens(self) -> _Logical: # noqa: C901
|
||||||
"""Build the mapping, comments, and logical line lists."""
|
"""Build the mapping, comments, and logical line lists."""
|
||||||
logical = []
|
logical = []
|
||||||
comments = []
|
comments = []
|
||||||
|
|
@ -195,6 +196,11 @@ class FileProcessor:
|
||||||
continue
|
continue
|
||||||
if token_type == tokenize.STRING:
|
if token_type == tokenize.STRING:
|
||||||
text = mutate_string(text)
|
text = mutate_string(text)
|
||||||
|
elif (
|
||||||
|
sys.version_info >= (3, 12)
|
||||||
|
and token_type == tokenize.FSTRING_MIDDLE
|
||||||
|
):
|
||||||
|
text = "x" * len(text)
|
||||||
if previous_row:
|
if previous_row:
|
||||||
(start_row, start_column) = start
|
(start_row, start_column) = start
|
||||||
if previous_row != start_row:
|
if previous_row != start_row:
|
||||||
|
|
|
||||||
|
|
@ -197,3 +197,35 @@ t.py:3:1: T001 '"""\\n'
|
||||||
'''
|
'''
|
||||||
out, err = capsys.readouterr()
|
out, err = capsys.readouterr()
|
||||||
assert out == expected
|
assert out == expected
|
||||||
|
|
||||||
|
|
||||||
|
def yields_logical_line(logical_line):
|
||||||
|
yield 0, f"T001 {logical_line!r}"
|
||||||
|
|
||||||
|
|
||||||
|
def test_logical_line_plugin(tmpdir, capsys):
|
||||||
|
cfg_s = f"""\
|
||||||
|
[flake8]
|
||||||
|
extend-ignore = F
|
||||||
|
[flake8:local-plugins]
|
||||||
|
extension =
|
||||||
|
T = {yields_logical_line.__module__}:{yields_logical_line.__name__}
|
||||||
|
"""
|
||||||
|
|
||||||
|
cfg = tmpdir.join("tox.ini")
|
||||||
|
cfg.write(cfg_s)
|
||||||
|
|
||||||
|
src = """\
|
||||||
|
f'hello world'
|
||||||
|
"""
|
||||||
|
t_py = tmpdir.join("t.py")
|
||||||
|
t_py.write_binary(src.encode())
|
||||||
|
|
||||||
|
with tmpdir.as_cwd():
|
||||||
|
assert main(("t.py", "--config", str(cfg))) == 1
|
||||||
|
|
||||||
|
expected = """\
|
||||||
|
t.py:1:1: T001 "f'xxxxxxxxxxx'"
|
||||||
|
"""
|
||||||
|
out, err = capsys.readouterr()
|
||||||
|
assert out == expected
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue