From 455de7703ea2c4144f8c1bb2c352974ac9b2403f Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 27 Apr 2024 01:24:20 -0700 Subject: [PATCH] Fix another f-string regression (#4339) --- CHANGES.md | 3 +++ src/blib2to3/pgen2/tokenize.py | 8 ++++---- tests/data/cases/pep_701.py | 6 ++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f56f40f..e3df420 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,9 @@ +- Fix regression where Black failed to parse a multiline f-string containing another + multiline string (#4339) + ### Performance diff --git a/src/blib2to3/pgen2/tokenize.py b/src/blib2to3/pgen2/tokenize.py index f66087b..b86b91e 100644 --- a/src/blib2to3/pgen2/tokenize.py +++ b/src/blib2to3/pgen2/tokenize.py @@ -622,10 +622,10 @@ def generate_tokens( spos = strstart epos = (lnum, end) tokenline = contline + line - if ( - fstring_state.current() == STATE_NOT_FSTRING - and not is_fstring_start(token) - ): + if fstring_state.current() in ( + STATE_NOT_FSTRING, + STATE_IN_BRACES, + ) and not is_fstring_start(token): yield (STRING, token, spos, epos, tokenline) endprog_stack.pop() parenlev = parenlev_stack.pop() diff --git a/tests/data/cases/pep_701.py b/tests/data/cases/pep_701.py index 9e157bd..d72d91c 100644 --- a/tests/data/cases/pep_701.py +++ b/tests/data/cases/pep_701.py @@ -125,6 +125,9 @@ f'{{\\"kind\\":\\"ConfigMap\\",\\"metadata\\":{{\\"annotations\\":{{}},\\"name\\":\\"cluster-info\\",\\"namespace\\":\\"amazon-cloudwatch\\"}}}}' +f"""{''' +'''}""" + # output x = f"foo" @@ -252,3 +255,6 @@ f"{1:{2}d}" f'{{\\"kind\\":\\"ConfigMap\\",\\"metadata\\":{{\\"annotations\\":{{}},\\"name\\":\\"cluster-info\\",\\"namespace\\":\\"amazon-cloudwatch\\"}}}}' + +f"""{''' +'''}"""