Skip to content
Closed
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
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Simon Ruggier (@sruggier)
Tim Gates (@timgates42) <tim.gates@iress.com>
Batuhan Taskaya (@isidentical) <isidentical@gmail.com>
Jocelyn Boullier (@Kazy) <jocelyn@boullier.bzh>
Rupayon Haldar (@rupayon123)


Note: (@user) means a github user name.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Changelog
Unreleased
++++++++++

- Accept backslashes before braces in f-string literal text.

0.8.7 (2026-05-02)
++++++++++++++++++

Expand Down
5 changes: 3 additions & 2 deletions parso/python/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ def _get_token_collection(version_info):
unicode_character_name = r'[A-Za-z0-9\-]+(?: [A-Za-z0-9\-]+)*'
fstring_string_single_line = _compile(
r'(?:\{\{|\}\}|\\N\{' + unicode_character_name
+ r'\}|\\(?:\r\n?|\n)|\\[^\r\nN]|[^{}\r\n\\])+'
+ r'\}|\\(?:\r\n?|\n)|\\[^\r\nN{}]|\\(?=[{}])|[^{}\r\n\\])+'
)
fstring_string_multi_line = _compile(
r'(?:\{\{|\}\}|\\N\{' + unicode_character_name + r'\}|\\[^N]|[^{}\\])+'
r'(?:\{\{|\}\}|\\N\{' + unicode_character_name
+ r'\}|\\[^N{}]|\\(?=[{}])|[^{}\\])+'
)
fstring_format_spec_single_line = _compile(r'(?:\\(?:\r\n?|\n)|[^{}\r\n])+')
fstring_format_spec_multi_line = _compile(r'[^{}]+')
Expand Down
22 changes: 22 additions & 0 deletions test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,35 @@ def test_valid(code, grammar):
assert fstring.get_code() == code


@pytest.mark.parametrize('prefix', ['f', 'rf'])
@pytest.mark.parametrize('quote', ['"', '"""'])
@pytest.mark.parametrize('text', [r'\{value}', r'\{value:03o}', r'\{{value}}'])
def test_backslash_before_brace(prefix, quote, text, each_version):
code = prefix + quote + text + quote
grammar = load_grammar(version=each_version)
module = grammar.parse(code, error_recovery=False)
assert module.get_code() == code
assert not list(grammar.iter_errors(module))


def test_backslash_before_brace_in_call(grammar):
code = r'stream.write(b_(rf"\{ord_(c):0>3o}"))'
module = grammar.parse(code, error_recovery=False)
assert module.get_code() == code
assert not list(grammar.iter_errors(module))


@pytest.mark.parametrize(
'code', [
# an f-string can't contain unmatched curly braces
'f"}"',
'f"{"',
'f"""}"""',
'f"""{"""',
r'f"\}"',
r'rf"\}"',
r'f"""\}"""',
r'rf"""\}"""',

# invalid conversion characters
'f"{1!{a}}"',
Expand Down
Loading