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

Add newline after module docstrings in preview style #8283

Merged
merged 3 commits into from
Oct 28, 2023

Conversation

konstin
Copy link
Member

@konstin konstin commented Oct 27, 2023

Change

"""Test docstring"""
a = 1

to

"""Test docstring"""

a = 1

in preview style, but don't touch the docstring otherwise.

Do we want to ask black to also format the content of module level docstrings? Seems inconsistent to me that we change function and class docstring indentation/contents but not module docstrings.

Fixes #7995

Change
```python
"""Test docstring"""
a = 1
```
to
```python
"""Test docstring"""

a = 1
```
in preview style, but don't touch the docstring otherwise.

Do we want to ask black to also format the content of module level docstrings? Seems inconsistent to me that we change function and class docstring indentation/contents but not module docstrings.

Fixes #7995
@konstin konstin added formatter Related to the formatter preview Related to preview mode features labels Oct 27, 2023
+++ Preview
@@ -1,4 +1,5 @@
0.1
+
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be fixed by #8216

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to understand, why / how is this happening?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, there's a bug in try_from_docstring that's fixed in that PR. I'm just gonna copy that fix in here -- seems odd to commit without it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my plan was to merge the other one first and then rebase, but this does too

@charliermarsh charliermarsh enabled auto-merge (squash) October 28, 2023 01:12
@charliermarsh charliermarsh merged commit af95cba into main Oct 28, 2023
14 checks passed
@charliermarsh charliermarsh deleted the module-level-docstrings-get-an-empty-line branch October 28, 2023 01:16
@github-actions
Copy link
Contributor

PR Check Results

Ecosystem

✅ ecosystem check detected no format changes.

Comment on lines +563 to +567
return match value {
Constant::Str(value) if !value.implicit_concatenated => Some(DocstringStmt(stmt)),
Constant::Bytes(value) if !value.implicit_concatenated => Some(DocstringStmt(stmt)),
_ => None,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think bytes can be used as docstrings, right?

$ cat src/play.py                                           
b"docstring"

$ python
Python 3.11.3 (main, Apr 19 2023, 22:45:48) [Clang 16.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from src import play
>>> play.__doc__
>>> 

$ cat src/play.py
"docstring"

$ python
Python 3.11.3 (main, Apr 19 2023, 22:45:48) [Clang 16.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from src import play
>>> play.__doc__
'docstring'
>>> 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making double check! psf/black#4002

konstin added a commit that referenced this pull request Oct 30, 2023
We previously incorrectly treated byte strings in docstring position as docstrings because black does so (#8283 (comment), psf/black#4002), even CPython doesn't recognize them:

```console
$ python3.12
Python 3.12.0 (main, Oct  6 2023, 17:57:44) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     b""" a"""
...
>>> print(str(f.__doc__))
None
```
konstin added a commit that referenced this pull request Oct 30, 2023
We previously incorrectly treated byte strings in docstring position as
docstrings because black does so
(#8283 (comment),
psf/black#4002), even CPython doesn't
recognize them:

```console
$ python3.12
Python 3.12.0 (main, Oct  6 2023, 17:57:44) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     b""" a"""
...
>>> print(str(f.__doc__))
None
```

<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->
@peterjc
Copy link

peterjc commented Mar 4, 2024

This conflicts with https://github.com/asottile/reorder-python-imports v3.12.0 which removes any blank lines between the module docstring and the import lines.

i.e. ruff format in v0.3.0 has the same issue reported in black as psf/black#4175 which is currently unresolved.

P.S. See also asottile/reorder-python-imports#366 - we can expect no fixes there 😞

Update: Consensus seems to be switch to isort instead.

@nikoethonai
Copy link

is there any config property which disables this behavior?

@MichaReiser
Copy link
Member

is there any config property which disables this behavior?

No, there's no such configuration option.

@kevalmorabia97
Copy link

I'd like to have an option to disable this behavior. I didn't have this when using Black 24 with preview features on.

@harupy
Copy link
Contributor

harupy commented Sep 17, 2024

@MichaReiser It looks like a newline is inserted when preview style is disabled. Is this intended?

https://play.ruff.rs/e23adff0-8541-4eab-957b-282e44aef53e

@harupy
Copy link
Contributor

harupy commented Sep 17, 2024

@MichaReiser Never mind. Just saw #9639.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter preview Related to preview mode features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add newline after module docstrings (module_docstring_newlines)
8 participants