Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust logical line for FSTRING_MIDDLE brace escaping #1252

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,10 @@ def build_tokens_line(self):
if token_type == tokenize.STRING:
text = mute_string(text)
elif token_type == FSTRING_MIDDLE: # pragma: >=3.12 cover
text = 'x' * len(text)
# fstring tokens are "unescaped" braces -- re-escape!
brace_count = text.count('{') + text.count('}')
text = 'x' * (len(text) + brace_count)
end = (end[0], end[1] + brace_count)
if prev_row:
(start_row, start_col) = start
if prev_row != start_row: # different row
Expand Down
19 changes: 19 additions & 0 deletions tests/test_pycodestyle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import io
import sys
import tokenize

import pytest

from pycodestyle import Checker
from pycodestyle import expand_indent
from pycodestyle import mute_string

Expand Down Expand Up @@ -27,3 +32,17 @@ def test_expand_indent(s, expected):
)
def test_mute_string(s, expected):
assert mute_string(s) == expected


def test_fstring_logical_line():
src = '''\
f'hello {{ {thing} }} world'
'''
checker = Checker(lines=src.splitlines())
checker.tokens = list(tokenize.generate_tokens(io.StringIO(src).readline))
checker.build_tokens_line()

if sys.version_info >= (3, 12): # pragma: >3.12 cover
assert checker.logical_line == "f'xxxxxxxxx{thing}xxxxxxxxx'"
else:
assert checker.logical_line == "f'xxxxxxxxxxxxxxxxxxxxxxxxx'"
Loading