mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Add a way to ignore `type: ignore`, disabled error codes

Open copdips opened this issue 3 years ago • 5 comments

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.

copdips avatar Jul 20 '22 13:07 copdips

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

JelleZijlstra avatar Jul 20 '22 13:07 JelleZijlstra

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.

sobolevn avatar Jul 21 '22 07:07 sobolevn

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

erictraut avatar Jul 21 '22 07:07 erictraut

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

copdips avatar Jul 21 '22 09:07 copdips

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

JukkaL avatar Jul 21 '22 11:07 JukkaL

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.

ssbarnea avatar May 21 '23 07:05 ssbarnea

@ssbarnea your issue is unrelated to the feature request here, I think it's more related to https://github.com/python/mypy/pull/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)

hauntsaninja avatar May 21 '23 20:05 hauntsaninja