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 crash with f-string docstrings #4019

Merged
merged 3 commits into from Nov 7, 2023
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
3 changes: 3 additions & 0 deletions CHANGES.md
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
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
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
@@ -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)}"""