mypy icon indicating copy to clipboard operation
mypy copied to clipboard

warn unused ignores flag does not respect TYPE_CHECKING

Open Dr-Irv opened this issue 3 years ago • 1 comments

Bug Report

With pandas-stubs, we are using pyright and mypy. We have code that we want to test with pyright, but not mypy because of a mypy bug. We are using the always-true option, but the issue here can be shown using TYPE_CHECKING.

If you include the flag --warn-unused-ignores, then the warnings will occur on code that should not be checked if looking at constants like TYPE_CHECKING or those set by always-true.

To Reproduce

from typing import TYPE_CHECKING

if not TYPE_CHECKING:
    x: int = "abc" # type: ignore[assignment]                                                     

Using mypy --no-incremental --warn-unused-ignores ignoretst.py, you get the error:

ignoretst.py:4: error: Unused "type: ignore" comment

Expected Behavior

No warning reported.

Actual Behavior

See above - even though the code should NOT be type checked, you are getting a warning.

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: --no-incremental --warn-unused-ignores
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9

Dr-Irv avatar Dec 28 '22 17:12 Dr-Irv

The underlying problem is the same as #8823: mypy reports that type: ignore comments are "unused" if they appear in code blocks that it detects are unreachable. (To an extent, that's a logical conclusion: you "obviously" can't be "using" the type: ignore comment if it's in a section of code that you can't ever get to!)

AlexWaygood avatar Dec 28 '22 18:12 AlexWaygood