Skip to content

gh-134158: Fix PyREPL coloring of double braces in f/t-strings #134159

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

Merged
Merged
Changes from 2 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
10 changes: 9 additions & 1 deletion Lib/_pyrepl/utils.py
Original file line number Diff line number Diff line change
@@ -41,9 +41,17 @@ def from_re(cls, m: Match[str], group: int | str) -> Self:

@classmethod
def from_token(cls, token: TI, line_len: list[int]) -> Self:
end_offset = 0
if ((token.type is T.FSTRING_MIDDLE or token.type is T.TSTRING_MIDDLE)
and token.string.endswith(("{", "}"))):
# Double braces in f-string / t-string are translated into a single
# brace by the tokenizer, and are always at the end of the token:
# we must add 1 to the token end position to color the two braces.
end_offset = 1

return cls(
line_len[token.start[0] - 1] + token.start[1],
line_len[token.end[0] - 1] + token.end[1] - 1,
line_len[token.end[0] - 1] + token.end[1] - 1 + end_offset,
)


31 changes: 31 additions & 0 deletions Lib/test/test_pyrepl/test_reader.py
Original file line number Diff line number Diff line change
@@ -517,6 +517,37 @@ def unfinished_function():
self.assert_screen_equal(reader, code, clean=True)
self.assert_screen_equal(reader, expected)

def test_syntax_highlighting_literal_brace_in_fstring_or_tstring(self):
code = dedent(
"""\
f"{{"
f"}}"
f"a{{b"
f"a}}b"
f"a{{b}}c"
t"a{{b}}c"
f"{{{0}}}"
f"{ {0} }"
"""
)
expected = dedent(
"""\
{s}f"{z}{s}<<{z}{s}"{z}
{s}f"{z}{s}>>{z}{s}"{z}
{s}f"{z}{s}a<<{z}{s}b{z}{s}"{z}
{s}f"{z}{s}a>>{z}{s}b{z}{s}"{z}
{s}f"{z}{s}a<<{z}{s}b>>{z}{s}c{z}{s}"{z}
{s}t"{z}{s}a<<{z}{s}b>>{z}{s}c{z}{s}"{z}
{s}f"{z}{s}<<{z}{o}<{z}{n}0{z}{o}>{z}{s}>>{z}{s}"{z}
{s}f"{z}{o}<{z} {o}<{z}{n}0{z}{o}>{z} {o}>{z}{s}"{z}
"""
).format(**colors).replace("<", "{").replace(">", "}")
events = code_to_events(code)
reader, _ = handle_all_events(events)
self.assert_screen_equal(reader, code, clean=True)
self.maxDiff=None
self.assert_screen_equal(reader, expected)

def test_control_characters(self):
code = 'flag = "🏳️‍🌈"'
events = code_to_events(code)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix PyREPL coloring of double braces in f-strings and t-strings
Loading
Oops, something went wrong.