what I want
My goal is not to keep the code the way I want it to be and disable black package formatting.
The point I want to learn is that the difference between the two pieces of code is just one line of comment, and why black format results differently. I think the same code should be formatted to look the same as long as my configuration remains the same.
Examples 1
python code:
def long_long_long_long_long_long_long_long_long_long_long_long_func(arg: list): ...
long_long_long_long_long_long_long_long_long_long_long_name = (
long_long_long_long_long_long_long_long_long_long_long_long_func(
arg=[
"long_long_long_long_long_long_long_long_long_long_long_string",
"long_long_long_long_long_long_long_long_long_long_long_string",
]
)
)
after black, nothing changed. That’s good.
Examples 2
python code:
def long_long_long_long_long_long_long_long_long_long_long_long_func(arg: list): ...
long_long_long_long_long_long_long_long_long_long_long_name = (
long_long_long_long_long_long_long_long_long_long_long_long_func(
arg=[
"long_long_long_long_long_long_long_long_long_long_long_string",
# a comment
"long_long_long_long_long_long_long_long_long_long_long_string",
]
)
)
after black, I got below. This is obviously not what I want.
def long_long_long_long_long_long_long_long_long_long_long_long_func(arg: list): ...
long_long_long_long_long_long_long_long_long_long_long_name = long_long_long_long_long_long_long_long_long_long_long_long_func(
arg=[
"long_long_long_long_long_long_long_long_long_long_long_string",
# a comment
"long_long_long_long_long_long_long_long_long_long_long_string",
]
)
env
- black, 24.8.0 (compiled: yes)
- Python (CPython) 3.9.19
- macOS Sonoma 14.5
what I try
- I’ve simplified the sample code to the point where it can at least reproduce the problem.
- I looked in Black’s documentation for line breaks, maximum length, and there was no explanation for this
- I searched stackoverflow. Com and Google and found no instructions.
Why does black output a different format when code has comments?
what I want
My goal is not to keep the code the way I want it to be and disable black package formatting.
The point I want to learn is that the difference between the two pieces of code is just one line of comment, and why black format results differently. I think the same code should be formatted to look the same as long as my configuration remains the same.
Examples 1
python code:
after black, nothing changed. That’s good.
Examples 2
python code:
after black, I got below. This is obviously not what I want.
env
what I try
2
This issue occurs because Black attempts to adhere to its strict rules on line length and formatting, even if it sometimes splits lines differently from what you expect. In your case, the issue is that Black treats the comment as a signal to reformat the line differently than when there is no comment.
Currently, Black doesn’t provide a way to keep the parentheses indentation style you desire when comments are involved.
If you want to maintain the original formatting style, you can use Black’s
# fmt:off
and# fmt:on
directives.This is the code.
I hope this will help you a little.
1
Filed under: Kiến thức lập trình - @ 14:25
Thẻ: pythoncommentspython-black
« Algorithm to pick user ⇐ More Pages ⇒ Why is the [#OASchema] attribute not working as in one of examples? »