black icon indicating copy to clipboard operation
black copied to clipboard

Black reformats docstrings

Open berislavlopac opened this issue 3 years ago • 16 comments

Describe the bug

Black unexpectedly changes the indentation of docstrings.

To Reproduce Steps to reproduce the behavior:

  1. Take this file:
def something(foo, bar):
    """ My docstring.

        Some more docstring.

        Args:
            foo: something something
            bar: bla bla
    """
    print(foo, bar)
  1. Run Black on it with no arguments.

  2. The file now looks like this:

def something(foo, bar):
    """My docstring.

    Some more docstring.

    Args:
        foo: something something
        bar: bla bla
    """
    print(foo, bar)

Expected behavior

The file should not change.

Environment (please complete the following information):

  • Version: black, version 20.8b1
  • OS and Python version: Linux, Python 3.8.6

Does this bug also happen on master?

Yes: https://black.now.sh/?version=master&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AERAI9dAD2IimZxl1N_WlbvK5V_r8p8R-QE02W0lfkJhkx4HULNTeXtLhBSpJmQpwGtlUvdJejSQJkMO6dLzygF-JFcvh-GgHC_wXD7PkS9n_u3ZNIEjx21NLS3PZDa8hVIXSPbpnHxFjE9s9CpuDGtiWTGVnMmoNBDg1cWmglrQAzN90l7lG5DkXLLpb9UER5YwF4AAADqLgMjoNolCwABqwGSAgAAAeEjmbHEZ_sCAAAAAARZWg==

Additional context

I understand that Black is opinionated and is trying to enforce PEP 257 verbatim, but there should at least be a switch to disable docstring formatting.

berislavlopac avatar Oct 22 '20 16:10 berislavlopac

A few relevant comments in a closed issue about docstrings

Could we please have a switch to disable the docstring reformatting altogether? Removing the leading whitespace may break docstrings containing tabular text. For instance this PLY/Yacc grammar rule:

    def p_flow_stmt(self, p):
        """flow_stmt : break_stmt
                     | continue_stmt
                     | return_stmt
                     | raise_stmt
                     | yield_stmt
        """
        p[0] = p[1]

ends up like:

    def p_flow_stmt(self, p):
        """flow_stmt : break_stmt
        | continue_stmt
        | return_stmt
        | raise_stmt
        | yield_stmt
        """
        p[0] = p[1]

https://github.com/psf/black/issues/1635#issuecomment-682639306

laloch: ~I cannot reproduce this. Were the grammar rules using tab characters instead of spaces?~

Nevermind, I was on the wrong branch. Yeah, we'll deal with this for the next release.

https://github.com/psf/black/issues/1635#issuecomment-683327782

@laloch In the mean time, a workaround for Black's behavior is starting your docstring text on a newline:

    def p_flow_stmt(self, p):
        """
        flow_stmt : break_stmt
                  | continue_stmt
                  | return_stmt
                  | raise_stmt
                  | yield_stmt
        """
        p[0] = p[1]

https://github.com/psf/black/issues/1635#issuecomment-683329285

ichard26 avatar May 29 '21 15:05 ichard26

I am having a similar issue. When I am using the Click package for CLI, and employ the use of "\b" and "\f" for controlled formatting per https://click.palletsprojects.com/en/8.0.x/documentation/#preventing-rewrapping, then black reformats this docstring and mangles the CLI help-text.

jeremyschulman avatar Oct 21 '21 01:10 jeremyschulman

@jeremyschulman could you report an issue for that? I'd be open to specifically preserving \b and \f.

JelleZijlstra avatar Oct 22 '21 23:10 JelleZijlstra

@JelleZijlstra What is the problem with having an option to prevent formatting of docstrings completely?

berislav-harbr avatar Oct 22 '21 23:10 berislav-harbr

We prefer to minimize options that affect formatting. It's also a better user experience for Click users if Black doesn't mangle their docstring in the first place than if they have to hunt for an option.

JelleZijlstra avatar Oct 23 '21 00:10 JelleZijlstra

Oh I totally agree - it's better for everyone not to mangle their docstrings in the first place. 🙄

On Sat, 23 Oct 2021, 01:10 Jelle Zijlstra, @.***> wrote:

We prefer to minimize options that affect formatting. It's also a better user experience for Click users if Black doesn't mangle their docstring in the first place than if they have to hunt for an option.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/psf/black/issues/1779#issuecomment-950022706, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVG3A7NMTFRPLTV4HQ3FPTTUIH4PVANCNFSM4S3OTEPA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

berislav-harbr avatar Oct 23 '21 02:10 berislav-harbr

@JelleZijlstra - I have created issue #2565 per your request. Thank you for supporting black. I love it!

jeremyschulman avatar Oct 25 '21 12:10 jeremyschulman

Black broke the RST in some of our docstrings.

Example::

    pre-formated example command

There are other constructs that need to be indented correctly as well.

mixmastamyk avatar May 12 '22 01:05 mixmastamyk

@mixmastamyk could you be more specific? It seems to work fine on the playground.

felix-hilden avatar May 19 '22 09:05 felix-hilden

Probably an old version:

⏵ black --version
black, 22.1.0 (compiled: yes)
⏵ cat foo.py
def foo():
    '''Example::

        pre-formated example command
    '''
    pass
⏵ cat foo.py
def foo():
    """Example::

    pre-formated example command
    """
    pass

Nope, upgraded and this does the same:

⏵ black --version
black, 22.3.0 (compiled: yes)

If perhaps you are not familiar with RST, the indent on the second line is required to be indented to signify a preformated section. As RST is used heavily in Python projects I'm surprised this is the case. Maybe I've done something wrong. 🤔

mixmastamyk avatar May 19 '22 19:05 mixmastamyk

Oh, this is likely just a special case of the first line being involved. We leave it in but standardise the indentation in the general body, so the "block" is dedented. Is your style accepted by Sphinx?

felix-hilden avatar May 19 '22 19:05 felix-hilden

No, RST treats the dedented text as regular body text. But, I think I know what you are saying now. Another line in there would fix it.

mixmastamyk avatar May 19 '22 19:05 mixmastamyk

I guess this is tolerable, but think I'd prefer not having to outsmart the formatter.

mixmastamyk avatar May 19 '22 19:05 mixmastamyk

I tested it: Sphinx does not accept your format. It warns about "literal block expected; none found" and outputs ordinary text. So are you using some other tool?

felix-hilden avatar May 19 '22 19:05 felix-hilden

Black is what broke the .rst formatting. I wish it didn't do this, and didn't ask for it. But, seems easy to work around.

mixmastamyk avatar May 29 '22 00:05 mixmastamyk

I meant that the unedited example you described was not accepted by Sphinx, producing the error I provided. I did not format with black before building.

felix-hilden avatar May 30 '22 07:05 felix-hilden

In #3087 we discussed docstring formatting, and decided that it will keep happening. The specific issue Richard brought up is being tracked in #1698. So I'll close this issue for housekeeping.

felix-hilden avatar Aug 29 '22 15:08 felix-hilden