PyCQA / pydocstyle

docstring style checker
http://pydocstyle.org
MIT License
1.11k stars 189 forks source link

D202: Incompatibility with black 23 #627

Open adamjstewart opened 1 year ago

adamjstewart commented 1 year ago

Black 23.1.0 introduces a new formatting change that requires an empty line before functions: https://github.com/psf/black/pull/3302. In some rare cases, this seems to counteract pydocstyle requirements.

Steps to reproduce

  1. Create the following file:

    """Foo."""
    
    def foo():
       """Foo."""
       # Closure
       def bar():
           """Bar."""
           pass
  2. Run pydocstyle, see that it passes
  3. Run black, see that it inserts a newline before the comment

    --- foo.py   2023-02-01 22:01:42.684243 +0000
    +++ foo.py   2023-02-01 22:02:23.613704 +0000
    @@ -1,9 +1,10 @@
    """Foo."""
    
    def foo():
        """Foo."""
    +
        # Closure
        def bar():
            """Bar."""
            pass
  4. Run pydocstyle again, see that it fails:
    foo.py:5 in public function `foo`:
           D202: No blank lines allowed after function docstring (found 1)

It's unclear to me whether black or pydocstyle is in the wrong here. Since black is a bigger project, I thought I'd start here and see whether pydocstyle should conform to this new standard set by black or whether this is a bug I should report to black.

adamjstewart commented 1 year ago

Note that the issue disappears if the comment is removed. Black still reformats the function, but pydocstyle is okay with it with or without the newline. This leads me to believe this is a problem with pydocstyle.

paravoid commented 1 year ago

I'm facing the same issue. AIUI this was previously raised with #361 and addressed with #426.

The fix had this:

re(r"\s+(?:(?:class|def)\s|@)").match(after)

...and I think the comment seems to just confuse the regexp parser.