black icon indicating copy to clipboard operation
black copied to clipboard

Docstrings reformatted to be too long

Open mlissner opened this issue 6 years ago • 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

mlissner avatar Aug 26 '20 18:08 mlissner

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?

ichard26 avatar Aug 26 '20 18:08 ichard26

Sorry, should have mentioned we have it set to 79. I think this link has that config and you can still see it:

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

mlissner avatar Aug 26 '20 19:08 mlissner

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).

ichard26 avatar Aug 26 '20 19:08 ichard26

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.

ambv avatar Aug 26 '20 23:08 ambv

Hi, is there an ETA for this?

gimperiale avatar Nov 16 '20 11:11 gimperiale

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

jensguballa avatar May 06 '21 18:05 jensguballa

@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.

ichard26 avatar May 06 '21 18:05 ichard26

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?

cooperlees avatar May 06 '21 18:05 cooperlees

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."""

jensguballa avatar May 06 '21 19:05 jensguballa

This comment still has a functional example: https://github.com/psf/black/issues/1632#issuecomment-681069972

mlissner avatar May 06 '21 19:05 mlissner

Even more info (from a duplicate of this issue): https://github.com/psf/black/issues/2274

ichard26 avatar May 30 '21 02:05 ichard26