Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when docstring is reindented and a line ends in a backslash #1610

Closed
JelleZijlstra opened this issue Aug 18, 2020 · 1 comment
Closed
Labels
C: invalid code Black destroyed a valid Python file T: bug Something isn't working

Comments

@JelleZijlstra
Copy link
Collaborator

This is another one from #1053

$ cat backslashdoc.py 
def xxx_xxxxxx_xxxxx_xx_xxxx():
    """xxxxxx x xxxx xx xx xxxxxxxx xxx xxxxx xx xxx xxxxxxx xxxxxx.

    xxxx_xx       = xx - (1 + xxxxxxxx_xxxx)
    xxxx_xx_xxxxx = xx - (1 + xxxxxxxx_xxxx) - (xxxxxx - 1) \
                  = xx - (xxxxxxxx_xxxx + xxxxxx)

   xxx xxxxx xxxxx xxxxxxxxxxxx xxxx xxx xxxxxxx xxxx xxxx xxxxxxx xx

    """
$ black backslashdoc.py 
error: cannot format backslashdoc.py: INTERNAL ERROR: Black produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_hx7_mm1z.log
Oh no! 💥 💔 💥
1 file failed to reformat.
$ cat  /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_hx7_mm1z.log
--- src
+++ dst
@@ -16,11 +16,11 @@
       body=
         Expr(
           value=
             Constant(
               value=
-                'xxxxxx x xxxx xx xx xxxxxxxx xxx xxxxx xx xxx xxxxxxx xxxxxx.\n\nxxxx_xx       = xx - (1 + xxxxxxxx_xxxx)\nxxxx_xx_xxxxx = xx - (1 + xxxxxxxx_xxxx) - (xxxxxx - 1)                   = xx - (xxxxxxxx_xxxx + xxxxxx)\n\nxxx xxxxx xxxxx xxxxxxxxxxxx xxxx xxx xxxxxxx xxxx xxxx xxxxxxx xx',  # str
+                'xxxxxx x xxxx xx xx xxxxxxxx xxx xxxxx xx xxx xxxxxxx xxxxxx.\n\nxxxx_xx       = xx - (1 + xxxxxxxx_xxxx)\nxxxx_xx_xxxxx = xx - (1 + xxxxxxxx_xxxx) - (xxxxxx - 1)                    = xx - (xxxxxxxx_xxxx + xxxxxx)\n\nxxx xxxxx xxxxx xxxxxxxxxxxx xxxx xxx xxxxxxx xxxx xxxx xxxxxxx xx',  # str
             )  # /Constant
         )  # /Expr
       decorator_list=
       name=
         'xxx_xxxxxx_xxxxx_xx_xxxx',  # str
@JelleZijlstra JelleZijlstra added T: bug Something isn't working C: invalid code Black destroyed a valid Python file labels Aug 18, 2020
@ichard26
Copy link
Collaborator

Fixed by #1623 as now Black ignores docstrings with backslash newline escapes.

black/src/black/__init__.py

Lines 2040 to 2042 in 9270a10

if is_docstring(leaf) and "\\\n" not in leaf.value:
# We're ignoring docstrings with backslash newline escapes because changing
# indentation of those changes the AST representation of the code.

(black-rHKUX7ap) R:\Programming\black>git fetch upstream
[snip]
(black-rHKUX7ap) R:\Programming\black>git merge upstream/master
[snip]
(black-rHKUX7ap) R:\Programming\black>cat temp.py
'cat' is not recognized as an internal or external command,
operable program or batch file.

(black-rHKUX7ap) R:\Programming\black>type temp.py
def xxx_xxxxxx_xxxxx_xx_xxxx():
    """xxxxxx x xxxx xx xx xxxxxxxx xxx xxxxx xx xxx xxxxxxx xxxxxx.

    xxxx_xx       = xx - (1 + xxxxxxxx_xxxx)
    xxxx_xx_xxxxx = xx - (1 + xxxxxxxx_xxxx) - (xxxxxx - 1) \
                  = xx - (xxxxxxxx_xxxx + xxxxxx)

   xxx xxxxx xxxxx xxxxxxxxxxxx xxxx xxx xxxxxxx xxxx xxxx xxxxxxx xx

    """
(black-rHKUX7ap) R:\Programming\black>black temp.py --check
would reformat temp.py
Oh no! 💥 💔 💥
1 file would be reformatted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: invalid code Black destroyed a valid Python file T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants