Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug which causes f-expressions to be split #1809

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3611,13 +3611,14 @@ class StringSplitter(CustomSplitMapMixin, BaseStringSplitter):
MIN_SUBSTR_SIZE = 6
# Matches an "f-expression" (e.g. {var}) that might be found in an f-string.
RE_FEXPR = r"""
(?<!\{)\{
(?<!\{) (?:\{\{)* \{ (?!\{)
(?:
[^\{\}]
| \{\{
| \}\}
| (?R)
)+?
(?<!\})(?:\}\})*\}(?!\})
(?<!\}) \} (?:\}\})* (?!\})
"""

def do_splitter_match(self, line: Line) -> TMatchResult:
Expand Down
14 changes: 14 additions & 0 deletions tests/data/long_strings__regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ def xxxxxxx_xxxxxx(xxxx):
),
}

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

# output


Expand Down Expand Up @@ -830,3 +834,13 @@ def xxxxxxx_xxxxxx(xxxx):
r"(?<!([0-9]\ ))(?<=(^|\ ))([A-Z]+(\ )?|[0-9](\ )|[a-z](\\\ )){4,7}([A-Z]|[0-9]|[a-z])($|\b)(?!(\ ?([0-9]\ )|(\.)))"
),
}

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