Skip to content

Latest black changes ASTs #2150

@godlygeek

Description

@godlygeek

Describe the bug

The README says:

Also, as a temporary safety measure, Black will check that the reformatted code still produces a valid AST that is equivalent to the original. This slows it down. If you're feeling confident, use --fast.

But after #1740, docstrings beginning with a double quote have an extra space prepended to them. This produces a different AST than the file had before, but --safe doesn't complain about this.

Is Black no longer promising to produce identical ASTs?

To Reproduce

$ cat test.py
def foo():
    '''"something" is what this does.'''


print(foo.__doc__)
$ python test.py
"something" is what this does.
$ black --safe test.py
reformatted test.py
All done! ✨ 🍰 ✨
1 file reformatted.
$ cat test.py
def foo():
    """ "something" is what this does."""


print(foo.__doc__)
$ python test.py
 "something" is what this does.
$

Expected behavior:

I believe that black is never supposed to change the AST, and therefore rewriting the docstring to contain this extra space is not a valid transformation for black to perform.

Environment:

  • Version: black, version 21.4b0
  • OS and Python version: Linux, Python 3.8.8

Does this bug also happen on master?

Yes

Additional context

$ python -m ast test.py >ast1.out
$ black test.py
reformatted test.py
All done! ✨ 🍰 ✨
1 file reformatted.
$ python -m ast test.py >ast2.out
$ diff -U0 ast1.out ast2.out
--- ast1.out    2021-04-26 19:05:58.174110800 -0400
+++ ast2.out    2021-04-26 19:08:57.324110800 -0400
@@ -13 +13 @@
-               value=Constant(value='"something" is what this does.'))],
+               value=Constant(value=' "something" is what this does.'))],

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: docstringsHow we format docstringsR: rejectedThis will not be worked onT: styleWhat do we want Blackened code to look like?

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions