Skip to content

PyREPL doesn't correctly color double braces in f-strings / t-strings #134158

Closed
@loic-simon

Description

@loic-simon

Bug report

Bug description:

Syntax highlighting in PyREPL (added in #131507) does not correctly color the double-braces used for escaping braces in f-strings or t-strings:

Image

After investigation, this is caused by Python tokenizer producing only 1 brace in this case:

>>> from tokenize import tokenize
>>> from io import BytesIO
>>> from pprint import pp
>>> 
>>> pp(list(tokenize(BytesIO(b'f"a{{b"').readline)))
[TokenInfo(type=68 (ENCODING), string='utf-8', start=(0, 0), end=(0, 0), line=''),
TokenInfo(type=59 (FSTRING_START), string='f"', start=(1, 0), end=(1, 2), line='f"a{{b"'),
TokenInfo(type=60 (FSTRING_MIDDLE), string='a{', start=(1, 2), end=(1, 4), line='f"a{{b"'),
TokenInfo(type=60 (FSTRING_MIDDLE), string='b', start=(1, 5), end=(1, 6), line='f"a{{b"'),
TokenInfo(type=61 (FSTRING_END), string='"', start=(1, 6), end=(1, 7), line='f"a{{b"'),
TokenInfo(type=4 (NEWLINE), string='', start=(1, 7), end=(1, 8), line='f"a{{b"'),
TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')]

So PyREPL think there is only one brace in the original string, and doesn't color the second one.

I have a fix for this issue, PR on the way!

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

Windows, macOS

Linked PRs

Activity

added
stdlibPython modules in the Lib dir
3.14bugs and security fixes
topic-replRelated to the interactive shell
3.15new features, bugs and security fixes
on May 17, 2025
added a commit that references this issue on May 19, 2025

gh-134158: Fix PyREPL coloring of double braces in f/t-strings (gh-13…

71ea6a6
added a commit that references this issue on May 19, 2025

pythongh-134158: Fix PyREPL coloring of double braces in f/t-strings (p…

added a commit that references this issue on May 19, 2025

[3.14] gh-134158: Fix PyREPL coloring of double braces in f/t-strings (

a3c3d6f
hugovk

hugovk commented on May 19, 2025

@hugovk
Member

Thank you for the report and the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesstdlibPython modules in the Lib dirtopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      PyREPL doesn't correctly color double braces in f-strings / t-strings · Issue #134158 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant