psf/black

Docstrings reformatted to be too long

Closed this issue · 11 comments

Describe the bug When there's a docstring that's near the max line length with the """ on the next line, the new version of Black moves the """ to the first line, making the line too long.

To Reproduce Steps to reproduce the behavior:

  1. Create a file with this content:

    def my_func():
            """Remove punctuation and return cleaned string plus its length in tokens.
            """
            pass
    
  2. Reformat it

  3. Note that it now has the docstring on one line and that that line has 82 chars:

    def my_func():
            """Remove punctuation and return cleaned string plus its length in tokens."""
            pass
    

Expected behavior

The docstring shouldn't get wrapped.

Environment (please complete the following information):

  • Version: [e.g. master] 20.8b1

  • OS and Python version:
    Linux/Python 3.8.2

Does this bug also happen on master? To answer this, you have two options:

Yes: https://black.now.sh/?version=master&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AC3AIldAD2IimZxl1N_WlbvK5V-ga8lyrVzgK48mW1g5y7ZvjbGhEN7nG-8hLA9oQ3I4FUxivNyD3Gy0_4fbH0Mg7-k28ZpTZzLozFMvMFSuDJT_0ZQmFpNZw6AAvoHjtKEQjGQksHXW1WBInIzl_HhTgRlCs-04wcVCEBY4OInuM5XhKBPO4ZIAzTqLcpeAAAAADHge2pSp2OmAAGlAbgBAAByceBuscRn-wIAAAAABFla

Hello @mlissner.

The default line length limit is 88 so I don't see what is wrong with Black wrapping the docstring as it still fits. Do you happen to have a pyproject.toml configuration file?

Okay, now this bug makes sense. Thanks for clarifying!

Git bisect points to a4c11a7 as the first bad commit, from "Re-indent the contents of docstrings" (#1053).

ambv commented

Good catch, we'll fix it in the next release which is planned for the week of Sept 28th (likely Oct 1). If this is a deal breaker for you, I suggest pinning to 19.10b0 in the mean time.

Hi, is there an ETA for this?

Is there any update on this issue?
In my case it breaks flake8, which complains about the lines being too long.

@jensguballa no, there hasn't been any update on this issue. No one has stepped on and worked on a fix. Us maintainers have been working on other things (either we deem more important or want to work on) so this slipped under our radar. Sorry for not following through with our claim.

Some code examples we can add to tests with the black config/args you added would be amazing.

Do you by chance just not have flake8 configured to allow ~88 chars or not changing black's line length?

I am using black with its default configuration (88 chars), and I have set max-line-length = 88 in the flake section of setup.cfg. So I believe these should be consistent settings.

Here is a test script:

class _YamlBlockStyle(str):
    """Class used to indicate that a string shall be serialized using the block style.
    """

Line length is 86. After reformatting it is increased to 89:

class _YamlBlockStyle(str):
    """Class used to indicate that a string shall be serialized using the block style."""

This comment still has a functional example: #1632 (comment)

Even more info (from a duplicate of this issue): #2274