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

Latest black (25.1.0) adds a newline after docstring and before pass in an exception class #4571

Closed
simonw opened this issue Jan 31, 2025 · 2 comments
Labels
T: bug Something isn't working

Comments

@simonw
Copy link

simonw commented Jan 31, 2025

This file:

class ModelError(Exception):
    "Models can raise this error, which will be displayed to the user"
    pass

When run through the latest Black is reformatted to look like this:

class ModelError(Exception):
    "Models can raise this error, which will be displayed to the user"

    pass

Here's a way to see that happen using uvx. With the previous Black version:

echo 'class ModelError(Exception):
    "Models can raise this error, which will be displayed to the user"
    pass' | uvx --with 'black==24.10.0' black -

This outputs:

class ModelError(Exception):
    "Models can raise this error, which will be displayed to the user"
    pass
All done! ✨ 🍰 ✨
1 file left unchanged.

But if you bump to 25.1.0 this happens:

echo 'class ModelError(Exception):
    "Models can raise this error, which will be displayed to the user"
    pass' | uvx --with 'black==25.1.0' black - 

Output:

class ModelError(Exception):
    "Models can raise this error, which will be displayed to the user"

    pass
reformatted -

All done! ✨ 🍰 ✨
1 file reformatted.
@JelleZijlstra
Copy link
Collaborator

This is correct and expected. Black 25 releases our new stable style for 2025, and one of the thing it changes is a bugfix where we were previously failing to recognize strings like this one as docstrings. We previously already behaved this way for triple-quoted docstrings; now the behavior is consistent.

$ echo 'class ModelError(Exception):
    """Models can raise this error, which will be displayed to the user"""
    pass' | uvx --with 'black==25.1.0' black - --diff
--- STDIN	2025-01-31 20:34:18.575891+00:00
+++ STDOUT	2025-01-31 20:34:18.576843+00:00
@@ -1,3 +1,4 @@
 class ModelError(Exception):
     """Models can raise this error, which will be displayed to the user"""
+
     pass
would reformat -

All done! ✨ 🍰 ✨
1 file would be reformatted.
$ echo 'class ModelError(Exception):
    """Models can raise this error, which will be displayed to the user"""
    pass' | uvx --with 'black==24.10.0' black - --diff
--- STDIN	2025-01-31 20:34:23.106368+00:00
+++ STDOUT	2025-01-31 20:34:23.110604+00:00
@@ -1,3 +1,4 @@
 class ModelError(Exception):
     """Models can raise this error, which will be displayed to the user"""
+
     pass
would reformat -

All done! ✨ 🍰 ✨
1 file would be reformatted.

If you don't want the blank line, I'd recommend removing the pass; it's not needed.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2025
simonw added a commit to simonw/llm that referenced this issue Jan 31, 2025
@simonw
Copy link
Author

simonw commented Jan 31, 2025

Thanks for the explanation, that makes sense - I'll re-train myself not to use pass when i have an exception with a docstring like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants