Skip to content

Commit

Permalink
Fix crash with f-string docstrings (#4019)
Browse files Browse the repository at this point in the history
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 <Alex.Waygood@Gmail.com>
  • Loading branch information
hauntsaninja and AlexWaygood authored Nov 7, 2023
1 parent e808e61 commit ecbd9e8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

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

- No longer treat leading f-strings as docstrings. This matches Python's behaviour and
fixes a crash (#4019)

### Preview style

- Multiline dictionaries and lists that are the sole argument to a function are now
Expand Down
2 changes: 1 addition & 1 deletion src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ def is_docstring(leaf: Leaf) -> bool:
return False

prefix = get_string_prefix(leaf.value)
if "b" in prefix or "B" in prefix:
if set(prefix).intersection("bBfF"):
return False

if prev_siblings_are(
Expand Down
3 changes: 2 additions & 1 deletion tests/data/cases/docstring_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def docstring_almost_at_line_limit():


def docstring_almost_at_line_limit_with_prefix():
f"""long docstring................................................................"""
f"""long docstring................................................................
"""


def mulitline_docstring_almost_at_line_limit():
Expand Down
20 changes: 20 additions & 0 deletions tests/data/cases/f_docstring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def foo(e):
f""" {'.'.join(e)}"""

def bar(e):
f"{'.'.join(e)}"

def baz(e):
F""" {'.'.join(e)}"""

# output
def foo(e):
f""" {'.'.join(e)}"""


def bar(e):
f"{'.'.join(e)}"


def baz(e):
f""" {'.'.join(e)}"""

0 comments on commit ecbd9e8

Please sign in to comment.