-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 on docstring with tabs #1601
Comments
This is caused by the tab at the end of a line in the docstring: Here's a minimal example (initial tabs replaced with spaces): def thing():
"""
line ending in a tab
line ending with no whitespace
""" Error does not occur with a tab at the end of the docstring: def thing():
"""
line ending in a tab
""" And does not occur with a space at the end: def thing():
"""
line ending in a space
line ending with no whitespace
""" |
@hugovk thanks! |
You're welcome! This is still a bug in Black, so we can keep it open to be investigated :) |
We can probably fix this by loosening the constraints on docstrings when we do the AST comparison, similar to what I did in #1593. |
Updated (again), sorry for the noise. Confirming a duplicate like #1638 (which may be a different but related issue in black 20.8b1) |
Note that the bug also occurs when there's a literal tab in the middle of a line. It seems black converts that to spaces and errors out. def foo():
"""
a b # it's a literal tab between a and b
"""
pass $ black foo.py
error: cannot format foo.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: /tmp/blk_q5uiqxs8.log
Oh no! 💥 💔 💥
1 file failed to reformat. The contents of --- src
+++ dst
@@ -23,11 +23,11 @@
body=
Expr(
value=
Constant(
value=
- "a\tb # it's a literal tab between a and b", # str
+ "a b # it's a literal tab between a and b", # str
) # /Constant
) # /Expr
Pass(
) # /Pass
decorator_list= |
Note also the bug only occurs with a multiline docstring: def f(p):
"""p: Parameter
"""
pass This passes: def f(p):
"""p: Parameter"""
pass |
This appears to be caused by the intentional decision (introduced in #1053) to expand tabs to space (using `tabsize=8``, which is the expandtabs() default): Lines 6745 to 6747 in 6dddbd7
|
The `fix_docstring` function expanded all tabs, which caused a difference in the AST representation when those tabs were inline and not leading. This changes the function to only expand leading tabs so inline tabs are preserved. Fixes #1601.
The `fix_docstring` function expanded all tabs, which caused a difference in the AST representation when those tabs were inline and not leading. This changes the function to only expand leading tabs so inline tabs are preserved. Fixes psf#1601.
Describe the bug A clear and concise description of what the bug is.
To Reproduce Steps to reproduce the behavior:
Expected behavior Work
Environment (please complete the following information):
error: cannot format /home/user/cilantropy/dist_worker.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: /tmp/blk_rifw8eif.log Oh no! 💥 💔 💥 10 files left unchanged, 1 file failed to reformat.
Does this bug also happen on master? To answer this, you have two options:
pip install git+git://github.com/psf/black
Additional context:
Log:
and my code:
The text was updated successfully, but these errors were encountered: