Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Allow blank lines after multi-line function docstrings #361

Closed
anntzer opened this issue Apr 29, 2019 · 4 comments
Closed

Allow blank lines after multi-line function docstrings #361

anntzer opened this issue Apr 29, 2019 · 4 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Apr 29, 2019

Currently, pydocstyle warns by default when a function docstring is followed by a blank line (D202). However, a careful reading of PEP257 indicates that this rule should only apply to one-line docstrings, not to multiline docstrings: the sole sentence regarding this, "There's no blank line either before or after the docstring.", occurs in the "One-line docstrings" section, immediately after the "The closing quotes are on the same line as the opening quotes. This looks better for one-liners." sentence which clearly only applies to one-line docstrings. Allowing an empty line after the function docstring also makes sense if e.g. the function body contains empty lines: in something like

def func():
    """
    Some docstring.

    Some more stuff.
    """
    do_this()
    do_that()

    do_this_again()
    do_that_again()

if there's no empty line after the docstring, I personally feel the docstring "binds" too closely to the first part of the body at the expense of the second part.

@cdeil
Copy link

cdeil commented Jun 4, 2019

In our project we are using black and pydocstyle.

There is the case of inner functions following docstrings where black and pydocstyle` disagree, one of the few problems that come up when using both tools.

Example: https://github.com/gammapy/gammapy/blob/a9ca5c02b191a0f03212163c106e35f45f05850c/gammapy/stats/significance.py#L111-L125

pydocstyle tells me to remove the empty line after the docstring:

gammapy/stats/significance.py:112 in public function `significance_to_probability_normal_limit`:
        D202: No blank lines allowed after function docstring (found 1)

And then I run black, it will add the empty line back, because it puts empty lines around inner functions:
https://github.com/python/black#empty-lines

To me, the black style seems reasonable, so I'm bringing this up here instead of with black.

Would you be willing to change D202 and make it so that this formatting is allowed and will not raise a D202?

def spam():
    """Spam."""

    def ham():
        return "ham"

    return "spam"

lewisacidic added a commit to lewisacidic/pydocstyle that referenced this issue Aug 5, 2019
Changes D202: "No blank lines allowed after function docstring" to
allow space below function docstrings with inner functions.

i.e. allows:

```python

def outer():
    """Valid docstring."""

    def inner():
        pass

    return pass
```

See comment from @cdeil in PyCQA#361.

feat(checker): add test case to test_definitions

docs(changelog): add change to release notes
Nurdok pushed a commit that referenced this issue Nov 30, 2019
* fix(checker): allow whitespace before inner functions and class

Changes D202: ("No blank lines allowed after function docstring") to
allow space below function docstrings with inner functions.

i.e. allows:

```python
def outer():
    """Valid docstring."""

    def inner():
        pass

    return pass
```

See comment from @cdeil in #361.

* fix(checker): use raw string for all regex

Avoid DeprecationWarning ("invalid escape sequence").
@ala-ableton
Copy link

AFAICS, this issue is fixed by #426 and could be closed.

@Nurdok Nurdok closed this as completed Dec 13, 2019
@anntzer
Copy link
Contributor Author

anntzer commented Dec 13, 2019

Actually I think this is not fully resolved: as noted in the original message, I think that a careful reading of PEP257 indicates that the prohibition for a blank link after the docstring only applies to single-line docstrings (the point literally appears in the "one-line docstrings" section).

@divypandya
Copy link

In our project we are using black and pydocstyle.

There is the case of inner functions following docstrings where black and pydocstyle` disagree, one of the few problems that come up when using both tools.

Example: https://github.com/gammapy/gammapy/blob/a9ca5c02b191a0f03212163c106e35f45f05850c/gammapy/stats/significance.py#L111-L125

pydocstyle tells me to remove the empty line after the docstring:

gammapy/stats/significance.py:112 in public function `significance_to_probability_normal_limit`:
        D202: No blank lines allowed after function docstring (found 1)

And then I run black, it will add the empty line back, because it puts empty lines around inner functions:
https://github.com/python/black#empty-lines

To me, the black style seems reasonable, so I'm bringing this up here instead of with black.

Would you be willing to change D202 and make it so that this formatting is allowed and will not raise a D202?

def spam():
    """Spam."""

    def ham():
        return "ham"

    return "spam"

Faced the same problem with inner functions. I added a comment just after the docstring of outer functions and left a new line before the inner function, worked for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants