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

Empty lines around class and method docstrings #130

Closed
mcflugen opened this issue Dec 8, 2022 · 3 comments · Fixed by #138
Closed

Empty lines around class and method docstrings #130

mcflugen opened this issue Dec 8, 2022 · 3 comments · Fixed by #138
Labels
C: convention Relates to docstring format convention P: bug PEP 257 violation or existing functionality that doesn't work as documented S: merged Closed with work merged to repository
Milestone

Comments

@mcflugen
Copy link
Contributor

mcflugen commented Dec 8, 2022

It would be nice if docformatter added/removed blank lines around the docstrings of classes and methods. According to the pep257 and numpy conventions, there should be no blank lines either before or after method docstrings and classes should have a blank line after their docstring but not before.

This issue is pretty much the same as #51 but applied to classes and methods.

Thus, the following,

class Foo:

    """Summary of Foo."""

    def foo(self):

        """Summary of foo."""

        pass

should be reformatted as,

class Foo:
    """Summary of Foo."""

    def foo(self):
        """Summary of foo."""
        pass
@github-actions github-actions bot added the fresh This is a new issue label Dec 8, 2022
@weibullguy weibullguy added P: enhancement Feature that is outside the scope of PEP 257 C: convention Relates to docstring format convention and removed fresh This is a new issue labels Dec 8, 2022
@mcflugen
Copy link
Contributor Author

mcflugen commented Dec 8, 2022

FYI, modifying the if statement in format.py with the following seems to fix blank lines preceding method and class docstrings,

                if (
                    len(modified_tokens) <= 2
                    or token_type not in {tokenize.NL, tokenize.NEWLINE}
                    or modified_tokens[-1][0]
                    not in {tokenize.NL, tokenize.NEWLINE}
                    or modified_tokens[-2][1] != ":"
                    and modified_tokens[-2][0] != tokenize.COMMENT
                    or not modified_tokens[-2][4].lstrip().startswith(("def", "class"))
                ):

@weibullguy weibullguy added the V: minor Bumps the minor version label Dec 16, 2022
@weibullguy weibullguy added this to the v1.6.0 milestone Dec 16, 2022
@weibullguy weibullguy added P: bug PEP 257 violation or existing functionality that doesn't work as documented V: patch Bumps the patch version and removed P: enhancement Feature that is outside the scope of PEP 257 V: minor Bumps the minor version labels Dec 18, 2022
@weibullguy weibullguy added the S: merged Closed with work merged to repository label Dec 29, 2022
@stefsmeets
Copy link

stefsmeets commented Jan 12, 2023

What is the intended behaviour in the case below?

class Foo:

    def foo(self):
        pass

In my interpretation, this is fine. But with the latest version of docformatter (1.5.1) this gets formatted as:

class Foo:
    def foo(self):
        pass

The result is that docformatter is now incompatible with the defaults for yapf (blank_line_before_nested_class_or_def=True). This causes issues in our autoformatting pipelines.

Edit: I see this is already addressed in #142 👍

@weibullguy weibullguy removed the V: patch Bumps the patch version label Jan 19, 2023
@bonastreyair
Copy link

bonastreyair commented Apr 4, 2023

what about this case? it removes all spaces and I think it shouldn't...

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"

When using it with black, it enforces to have an additional space between functions (as expected) and it returning to the first initial state... then failing all pre-commits
It did not happen in 1.5.1 but it does with latest 1.6.0 version that includes this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: convention Relates to docstring format convention P: bug PEP 257 violation or existing functionality that doesn't work as documented S: merged Closed with work merged to repository
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants