mirror of
https://github.com/PyCQA/flake8.git
synced 2026-03-29 10:36:53 +00:00
Merge pull request #1949 from stephenfin/issue-1948
Handle escaped braces in f-strings
This commit is contained in:
commit
939ea3d8d9
2 changed files with 42 additions and 1 deletions
|
|
@ -203,7 +203,13 @@ class FileProcessor:
|
|||
if token_type == tokenize.STRING:
|
||||
text = mutate_string(text)
|
||||
elif token_type == FSTRING_MIDDLE: # pragma: >=3.12 cover
|
||||
text = "x" * len(text)
|
||||
# A curly brace in an FSTRING_MIDDLE token must be an escaped
|
||||
# curly brace. Both 'text' and 'end' will account for the
|
||||
# escaped version of the token (i.e. a single brace) rather
|
||||
# than the raw double brace version, so we must counteract this
|
||||
brace_offset = text.count("{") + text.count("}")
|
||||
text = "x" * (len(text) + brace_offset)
|
||||
end = (end[0], end[1] + brace_offset)
|
||||
if previous_row:
|
||||
(start_row, start_column) = start
|
||||
if previous_row != start_row:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
"""Integration tests for plugin loading."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from flake8.main.cli import main
|
||||
|
|
@ -261,3 +263,36 @@ t.py:1:1: T001 "f'xxxxxxxxxxx'"
|
|||
"""
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected
|
||||
|
||||
|
||||
def test_escaping_of_fstrings_in_string_redacter(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
|
||||
|
||||
if sys.version_info >= (3, 12): # pragma: >=3.12 cover
|
||||
expected = """\
|
||||
t.py:1:1: T001 "f'xxx{hello}xxxx{world}xxx'"
|
||||
"""
|
||||
else: # pragma: <3.12 cover
|
||||
expected = """\
|
||||
t.py:1:1: T001 "f'xxxxxxxxxxxxxxxxxxxxxxxx'"
|
||||
"""
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue