Skip to content

Fix f-string replacement braces following backslashes - #256

Closed
Sarbajit-Sengupta wants to merge 1 commit into
davidhalter:masterfrom
Sarbajit-Sengupta:fix/fstring-backslash-braces
Closed

Sarbajit-Sengupta wants to merge 1 commit into
davidhalter:masterfrom
Sarbajit-Sengupta:fix/fstring-backslash-braces

Conversation

@Sarbajit-Sengupta

Copy link
Copy Markdown

Fixes #207.

Parso's f-string literal scanner consumes a backslash followed by a brace as one escape. Python does not use backslashes to escape replacement-field braces. This rejects valid raw f-strings such as rf"\{value:0>3o}" and accepts an otherwise unmatched closing brace in rf"\}".

Keep the literal backslash in the string token and leave the following brace for the normal field parser, in both single-line and multiline strings. Regression cases cover raw prefix order/case, all four quote styles, one/two/three backslashes, source round trips, name references and rejection of unmatched closing braces. CPython's AST parser verifies the test inputs' validity.

Validation on Windows / Python 3.13.5: 36 new cases fail and 16 controls pass before the change; all 2,040 tests pass after it. Flake8 passes using CI's F401 exception. Zuban over the source and new test reports one existing diagnostic at unchanged parso/utils.py:95; its unrestricted Windows run also checks invalid-syntax fixtures despite the configured slash-based exclusion. No diagnostic was reported in either changed Python file. Other Python versions have not been run locally.

@davidhalter davidhalter closed this Nov 1, 2026
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.

KeyError: ReservedString(})

2 participants