Repository navigation
Black 22.1.0 strips some comments #2873
Copy link
Copy link
Closed
Labels
C: invalid codeBlack destroyed a valid Python fileBlack destroyed a valid Python fileF: commentsThe syntactic kind. Not in the language grammar, always on our minds. Best bugs.The syntactic kind. Not in the language grammar, always on our minds. Best bugs.T: bugSomething isn't workingSomething isn't working
Description
Describe the bug
Black will now occasionally strip comments (including type: ignore comments).
To Reproduce
For example, take this code:
if hasattr(view, "sum_of_weights"):
return np.divide( # type: ignore[no-any-return]
view.variance, # type: ignore[union-attr]
view.sum_of_weights, # type: ignore[union-attr]
out=np.full(view.sum_of_weights.shape, np.nan), # type: ignore[union-attr]
where=view.sum_of_weights > 1, # type: ignore[union-attr]
where=view.sum_of_weights**2 > view.sum_of_weights_squared, # type: ignore[union-attr]
)And run it with these arguments:
$ black file.pyThe resulting Python code is:
if hasattr(view, "sum_of_weights"):
return np.divide( # type: ignore[no-any-return]
view.variance, # type: ignore[union-attr]
view.sum_of_weights, # type: ignore[union-attr]
out=np.full(view.sum_of_weights.shape, np.nan), # type: ignore[union-attr]
where=view.sum_of_weights > 1, # type: ignore[union-attr]
where=view.sum_of_weights**2 > view.sum_of_weights_squared,
)Expected behavior
Not to strip a type ignore comment!!!
(Personally, I think all trailing comments should be ignored from all line length calculations, etc, since they are often turning off linting or type checking for the line they are on. But it clearly shouldn't just vanish!)
Environment
- Black's version: main and 22.1.0
- OS and Python version: Any (macOS, pre-commit.ci's linux, and whatever the web app runs on)
Additional context
Pretty self contained. :) I had noticed a similar error on upgrading to 22.1.0, but it went away due to other changes, and I never worked it back down, but I'm hoping it was the same one.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
C: invalid codeBlack destroyed a valid Python fileBlack destroyed a valid Python fileF: commentsThe syntactic kind. Not in the language grammar, always on our minds. Best bugs.The syntactic kind. Not in the language grammar, always on our minds. Best bugs.T: bugSomething isn't workingSomething isn't working
Activity
ichard26 commentedon Feb 7, 2022
Hmm, is there a reason why we ignore type comments in the AST @JelleZijlstra? this would be an excellent reason to stop doing this as this is problematic.
henryiii commentedon Feb 7, 2022
Simpler example:
I think it has to be in a function call, and it has to have a
**in the same line, and has to be longer than the line length. Changing**to*causes this stripping not to happen.I think
type: ignorecomments are ignored because they can never be moved to the next line. Though that's also true for# noqa:*,# pylint disable=*, etc.ichard26 commentedon Feb 7, 2022
Ah this is probably my fault, I suspect the power op hugging code I added in GH-2726 fails to copy over the Leaf prefixes which is where comments are stored. I'm currently not at home though so I won't be able to fix until later.
Is this worth a hotfix release? The damage is bad but it could not be common enough to warrant a hotfix.
Copy over comments when hugging power ops (#2874)
Copy over comments when hugging power ops (psf#2874)
Bump from upstream (#3)
Upstream force (#8)