Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix f-string comment parsing in Python 3.12+#3134

Open
Tttingsama wants to merge 2 commits into
pygments:masterfrom
Tttingsama:fix-f-string
Open

Fix f-string comment parsing in Python 3.12+#3134
Tttingsama wants to merge 2 commits into
pygments:masterfrom
Tttingsama:fix-f-string

Conversation

@Tttingsama

Copy link
Copy Markdown

PR Description

Summary

Fixes #2937 - Python f-string comment parsing for Python 3.12+

Problem

Python 3.12+ allows # comments inside f-string expressions, where comments extend to the end of line. However, Pygments incorrectly recognized } inside comments as the end of the f-string expression.
屏幕截图 2026-05-16 213639

Changes Made

Modified pygments/lexers/python.py in the expr-inside-fstring state:

'expr-inside-fstring': [
    (r'#[^\n]*', Comment.Single),     # Match # comment to end of line
    (r'\n', Whitespace),       # Return to parent state on newline
    # ... existing rules
],

Result

屏幕截图 2026-05-16 223619

Related Issues

#2937

@birkenfeld

Copy link
Copy Markdown
Member

Thanks for the PR! Can you add one or more testcases?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python lexer: Comments in f-strings are not parsed correctly

2 participants