Skip to content

Commit ecbd9e8

Browse files
Fix crash with f-string docstrings (#4019)
Python does not consider f-strings to be docstrings, so we probably shouldn't be formatting them as such Fixes #4018 Co-authored-by: Alex Waygood <[email protected]>
1 parent e808e61 commit ecbd9e8

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
- Fix crash on formatting code like `await (a ** b)` (#3994)
1515

16+
- No longer treat leading f-strings as docstrings. This matches Python's behaviour and
17+
fixes a crash (#4019)
18+
1619
### Preview style
1720

1821
- Multiline dictionaries and lists that are the sole argument to a function are now

src/black/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def is_docstring(leaf: Leaf) -> bool:
529529
return False
530530

531531
prefix = get_string_prefix(leaf.value)
532-
if "b" in prefix or "B" in prefix:
532+
if set(prefix).intersection("bBfF"):
533533
return False
534534

535535
if prev_siblings_are(

tests/data/cases/docstring_preview.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def docstring_almost_at_line_limit():
5858

5959

6060
def docstring_almost_at_line_limit_with_prefix():
61-
f"""long docstring................................................................"""
61+
f"""long docstring................................................................
62+
"""
6263

6364

6465
def mulitline_docstring_almost_at_line_limit():

tests/data/cases/f_docstring.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def foo(e):
2+
f""" {'.'.join(e)}"""
3+
4+
def bar(e):
5+
f"{'.'.join(e)}"
6+
7+
def baz(e):
8+
F""" {'.'.join(e)}"""
9+
10+
# output
11+
def foo(e):
12+
f""" {'.'.join(e)}"""
13+
14+
15+
def bar(e):
16+
f"{'.'.join(e)}"
17+
18+
19+
def baz(e):
20+
f""" {'.'.join(e)}"""

0 commit comments

Comments
 (0)