PyCQA / docformatter

Formats docstrings to follow PEP 257
https://pypi.python.org/pypi/docformatter
MIT License
526 stars 61 forks source link

v1.6.0-rc2 - Emtpy Lines After Docstring and Before Nested Function Definition Are Removed #161

Closed rmartin16 closed 1 year ago

rmartin16 commented 1 year ago

138 started removing empty lines after the method docstring including if the first code in the method if a function definition. I'm not sure if there should be exception for this situation...but at least black wants an empty line before a nested function definition.

Example:

 def test_no_git(tracking_create_command, monkeypatch):
     """If Git is not installed, an error is raised."""
-
     def monkeypatch_verify_git(*a, **kw):
         raise BriefcaseCommandError("Briefcase requires git, but it is not installed")
weibullguy commented 1 year ago

Per PEP-257, there is no blank line either before or after a docstring. The current docformatter behavior is correct. This is an enhancement that can be added to #94.

bonastreyair commented 1 year ago

I've added another example here: https://github.com/PyCQA/docformatter/issues/130#issuecomment-1495579567

Given:

class Foo:
    @abstractmethod
    def bar(self):
        """This is a description."""

    @abstractmethod
    def baz(self):
        """This is a second description."""

def new_function():
    """Description."""
    return "bar"

I get this:

class Foo:
    @abstractmethod
    def bar(self):
        """This is a description."""
    @abstractmethod
    def baz(self):
        """This is a second description."""
def new_function():
    """Description."""
    return "bar"
weibullguy commented 1 year ago

@bonastreyair per PEP-257, there is no blank line either before or after a docstring. Add a pass or any other Python statement to your stub functions and the blank lines won't be removed.

Kurt-von-Laven commented 1 year ago

docformatter v1.6.0 isn't correctly removing the blank line after a docstring in this scenario; it's incorrectly removing the docstring between method definitions. PEP 257 states:

Insert a blank line after all docstrings (one-line or multi-line) that document a class – generally speaking, the class’s methods are separated from each other by a single blank line, and the docstring needs to be offset from the first method by a blank line.

PEP 8 states:

Method definitions inside a class are surrounded by a single blank line.

docformatter shouldn't render an opinion on the use of the optional pass keyword.

drasmuss commented 1 year ago

Note that this also applies to top level functions, not just class methods, e.g.

def foo():
    """Description."""

def bar():
     """Description."""

becomes

def foo():
    """Description."""
def bar():
     """Description."""

This is a pretty clear violation of pep8 (https://peps.python.org/pep-0008/#blank-lines)

Surround top-level function and class definitions with two blank lines.

I can't imagine that pep 257 was intended to override those basic pep8 blank line principles.

weibullguy commented 1 year ago

@bonastreyair tag v1.6.1-rc1 should have the fix you need if you're interested in giving it a try before I officially release v1.6.1.

@Kurt-von-Laven and @drasmuss you're correct, I didn't pay close enough attention to what @bonastreyair actually reported. Stub functions should be left as-is by docformatter in v1.6.1.

jonyscathe commented 1 year ago

v1.6.1-rc1 fixes this issue for me

bonastreyair commented 1 year ago

@weibullguy it works for mee too yup! thanks!