Skip to content

Commit db82569

Browse files
bbugyi200noxan
authored andcommitted
Fix bug which causes f-expressions to be split (psf#1809)
Closes psf#1807.
1 parent ee758f0 commit db82569

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/black/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3611,13 +3611,14 @@ class StringSplitter(CustomSplitMapMixin, BaseStringSplitter):
36113611
MIN_SUBSTR_SIZE = 6
36123612
# Matches an "f-expression" (e.g. {var}) that might be found in an f-string.
36133613
RE_FEXPR = r"""
3614-
(?<!\{)\{
3614+
(?<!\{) (?:\{\{)* \{ (?!\{)
36153615
(?:
36163616
[^\{\}]
36173617
| \{\{
36183618
| \}\}
3619+
| (?R)
36193620
)+?
3620-
(?<!\})(?:\}\})*\}(?!\})
3621+
(?<!\}) \} (?:\}\})* (?!\})
36213622
"""
36223623

36233624
def do_splitter_match(self, line: Line) -> TMatchResult:

tests/data/long_strings__regression.py

+14
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ def xxxxxxx_xxxxxx(xxxx):
371371
),
372372
}
373373

374+
# We do NOT split on f-string expressions.
375+
print(f"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam. {[f'{i}' for i in range(10)]}")
376+
x = f"This is a long string which contains an f-expr that should not split {{{[i for i in range(5)]}}}."
377+
374378
# output
375379

376380

@@ -830,3 +834,13 @@ def xxxxxxx_xxxxxx(xxxx):
830834
r"(?<!([0-9]\ ))(?<=(^|\ ))([A-Z]+(\ )?|[0-9](\ )|[a-z](\\\ )){4,7}([A-Z]|[0-9]|[a-z])($|\b)(?!(\ ?([0-9]\ )|(\.)))"
831835
),
832836
}
837+
838+
# We do NOT split on f-string expressions.
839+
print(
840+
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam."
841+
f" {[f'{i}' for i in range(10)]}"
842+
)
843+
x = (
844+
"This is a long string which contains an f-expr that should not split"
845+
f" {{{[i for i in range(5)]}}}."
846+
)

0 commit comments

Comments
 (0)