Skip to content

Commit

Permalink
Merge branch 'PyCQA:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloBroBro authored Aug 5, 2024
2 parents 86fcc6c + cf1542c commit b92b3d0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
15 changes: 15 additions & 0 deletions docs/source/release-notes/7.1.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
7.1.1 -- 2024-08-04
-------------------

You can view the `7.1.1 milestone`_ on GitHub for more details.

Bugs Fixed
~~~~~~~~~~

- Properly preserve escaped `{` and `}` in fstrings in logical lines in 3.12+.
(See also :issue:`1948`, :pull:`1949`).


.. all links
.. _7.1.1 milestone:
https://github.com/PyCQA/flake8/milestone/51
1 change: 1 addition & 0 deletions docs/source/release-notes/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ with the newest releases first.
.. toctree::
7.0.0
7.1.0
7.1.1

6.x Release Series
==================
Expand Down
2 changes: 1 addition & 1 deletion src/flake8/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
LOG = logging.getLogger(__name__)
LOG.addHandler(logging.NullHandler())

__version__ = "7.1.0"
__version__ = "7.1.1"
__version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit())

_VERBOSITY_TO_LOG_LEVEL = {
Expand Down
8 changes: 7 additions & 1 deletion src/flake8/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ def build_logical_line_tokens(self) -> _Logical: # noqa: C901
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:
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Integration tests for plugin loading."""
from __future__ import annotations

import sys

import pytest

from flake8.main.cli import main
Expand Down Expand Up @@ -261,3 +263,36 @@ def test_logical_line_plugin(tmpdir, capsys):
"""
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

0 comments on commit b92b3d0

Please sign in to comment.