Skip to content
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

warn unused ignores flag does not respect TYPE_CHECKING #14365

Open
Dr-Irv opened this issue Dec 28, 2022 · 1 comment
Open

warn unused ignores flag does not respect TYPE_CHECKING #14365

Dr-Irv opened this issue Dec 28, 2022 · 1 comment
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code topic-type-ignore # type: ignore comments

Comments

@Dr-Irv
Copy link

Dr-Irv commented Dec 28, 2022

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 Dr-Irv added the bug mypy got something wrong label Dec 28, 2022
@AlexWaygood AlexWaygood added the topic-type-ignore # type: ignore comments label Dec 28, 2022
@AlexWaygood
Copy link
Member

AlexWaygood commented Dec 28, 2022

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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code topic-type-ignore # type: ignore comments
Projects
None yet
Development

No branches or pull requests

2 participants