From b1c4dd96d769cb3ac3c8fc057e8dc60907b84c17 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Fri, 2 Aug 2024 21:51:39 +0530 Subject: [PATCH] fix: respect braces better in f-string parsing (#4422) --- CHANGES.md | 2 ++ src/blib2to3/pgen2/tokenize.py | 12 ++++++------ tests/data/cases/pep_701.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 48fe337392d..00c210ba86f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -39,6 +39,8 @@ - Fix bug with Black incorrectly parsing empty lines with a backslash (#4343) +- Fix bugs with Black's tokenizer not handling `\{` inside f-strings very well (#4422) + - Fix incorrect line numbers in the tokenizer for certain tokens within f-strings (#4423) diff --git a/src/blib2to3/pgen2/tokenize.py b/src/blib2to3/pgen2/tokenize.py index ecd017b3148..d15dc03d945 100644 --- a/src/blib2to3/pgen2/tokenize.py +++ b/src/blib2to3/pgen2/tokenize.py @@ -136,12 +136,12 @@ def _combinations(*l: str) -> Set[str]: ) # beginning of a single quoted f-string. must not end with `{{` or `\N{` -SingleLbrace = r"(?:\\N{|\\.|{{|[^'\\{])*(? Set[str]: _string_middle_double = r'(?:[^\n"\\]|\\.)*' # FSTRING_MIDDLE and LBRACE, must not end with a `{{` or `\N{` -_fstring_middle_single = r"(?:\\N{|\\[^{]|{{|[^\n'{\\])*(?