Skip to content

Black 22.1.0 strips some comments #2873

Closed

Description

@henryiii

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.py

The 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.

Activity

  1. added
    C: invalid codeBlack destroyed a valid Python file
    F: commentsThe syntactic kind. Not in the language grammar, always on our minds. Best bugs.
    on Feb 7, 2022
  2. ichard26 commented on Feb 7, 2022

    @ichard26
    Collaborator

    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.

  3. henryiii commented on Feb 7, 2022

    @henryiii
    ContributorAuthor

    Simpler example:

    return np.divide(
        where=view.sum_of_weights_of_weight_long**2 > view.sum_of_weights_squared,  # type: ignore
    )

    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: ignore comments are ignored because they can never be moved to the next line. Though that's also true for # noqa:*, # pylint disable=*, etc.

  4. ichard26 commented on Feb 7, 2022

    @ichard26
    Collaborator

    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.

  5. self-assigned this
    on Feb 8, 2022
  6. linked a pull request that will close this issueCopy over comments when hugging power ops #2874on Feb 8, 2022
  7. added a commit that references this issue on Jul 14, 2022
    18c17be
  8. added a commit that references this issue on Aug 13, 2022
    afde7e5
  9. added a commit that references this issue on Oct 6, 2022
    81d333f
  10. added a commit that references this issue on Oct 27, 2022
    e5a4a0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C: invalid codeBlack destroyed a valid Python fileF: commentsThe syntactic kind. Not in the language grammar, always on our minds. Best bugs.T: bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions