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

Add a way to ignore type: ignore, disabled error codes #13201

Open
copdips opened this issue Jul 20, 2022 · 7 comments
Open

Add a way to ignore type: ignore, disabled error codes #13201

copdips opened this issue Jul 20, 2022 · 7 comments
Labels

Comments

@copdips
Copy link

copdips commented Jul 20, 2022

Feature

My goal is to check all the ignored inline mypy errors/warnings

Pitch

People often added inline comment # type: ignore to ignore mypy error for a specific line, but it's difficult to trace for other developers, or for audit purpose. Now I would like to have something similar to flake8 --disable-noqa, which show all the mypy errors/warnings regardless of these inline ignores.

@JelleZijlstra
Copy link
Member

There is --warn-unused-ignores, which warns if a type ignore is unused, but there's no option to ignore them completely.

@sobolevn
Copy link
Member

type: ignore does not just change the error reporting, it also changes types of things. Most of the time type: ignore means that the thing behind it becomes Any.

And all parts of code matter for mypy (unlike flake8).

So, I don't think we need such a flag: un-silencing errors in this case can bring very confusing results.

@erictraut
Copy link

@sobolevn, what do you mean that "it also changes types of things"? My understanding is that mypy simply suppresses any errors on a line with a # type: ignore, and that the comment has no other effect in terms of type checking.

FWIW, pyright has an option that does what @copdips is requesting here (`"enableTypeIgnoreComments": false) to accommodate this use case.

@copdips
Copy link
Author

copdips commented Jul 21, 2022

@erictraut,
Thx for your trick with vscode, but in fact, I need the option during CICD time.
For e.g. with flake8, I run two commands:

flake8
flake8 --disable-noqa --exit-zero 

The first one is for the real CI check, and the second one is just for the review/audit.
That's why I asked if we have similar thing in mypy.

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 21, 2022

@erictraut is correct, a #type: ignore comment should only suppress errors on the line, so the proposed feature is a reasonable one.

If you know which code is type checked by mypy, it's easy enough to use some grep-like tool to find type ignores, for example something like this:

$ rg '# *type: *ignore\b'

This doesn't show the errors that are ignored, however.

As a quick hack, you could use sed to temporarily replace, say, # type: ignore with # xtype: ignore, and run mypy normally.

@hauntsaninja hauntsaninja changed the title Does mypy have similar thing to flake8 --disable-noqa Add a way to ignore type: ignore Aug 3, 2022
@hauntsaninja hauntsaninja changed the title Add a way to ignore type: ignore Add a way to ignore type: ignore, disabled error codes Aug 3, 2022
@ssbarnea
Copy link
Contributor

I am in a conundrum because the Unused "type: ignore" comment error seems impossible to ignore in a specific location.

I do have a file that might be missing:

try:
    from ._version import version as __version__  # type: ignore
except ImportError:  # pragma: no cover
    __version__ = "0.1.dev1"

Once mypy is configured to detect unused ignores (useful feature), I cannot make it really ignore this line. Keep in mind that there are two cases here, one where the file exists and one where the file does not exist.

I want to ensure mypy does not annoy me about this particular line only.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented May 21, 2023

@ssbarnea your issue is unrelated to the feature request here, I think it's more related to #15164 which should fix your kind of thing (will be included in next release). In particular, see this test case . For now, you can likely hack around it using __import__ or cast or if not TYPE_CHECKING (see some of the examples in #8823)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants