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

Support warning on unused ignores without treating them as an error #12359

Open
jab opened this issue Mar 15, 2022 · 1 comment
Open

Support warning on unused ignores without treating them as an error #12359

jab opened this issue Mar 15, 2022 · 1 comment
Labels
feature topic-type-ignore # type: ignore comments

Comments

@jab
Copy link
Contributor

jab commented Mar 15, 2022

Feature

Mypy's --warn-unused-ignores option currently treats unused ignores as an error, rather than merely warning about them but still exiting zero.

Mypy should support an option where unused ignores are still warned about in the output, but are not treated as an error.

Current behavior:

cat test.py
x = 1 + 1  # type: ignoremypy test.py
Success: no issues found in 1 source filemypy --strict test.py  # exits nonzero
test.py:1: error: Unused "type: ignore" comment
Found 1 error in 1 file (checked 1 source file)mypy --strict --no-warn-unused-ignores test.py  # exits zero
Success: no issues found in 1 source file

The current warn-unused-ignores behavior, where you have to choose between either erroring or total silence when there's an unused ignore (and can't actually just get a warning), could even be considered a bug.

Pitch

To preserve backward compatibility, perhaps something like this would work:

mypy --unused-ignores=warning # emit a warning but don't exit nonzeromypy --unused-ignores=error # emit an error and exit nonzero, same as --warn-unused-ignores nowmypy --warn-unused-ignores # same as --unused-ignores=errormypy --unused-ignores=none # no output, exit zero, same as --no-warn-unused-ignores nowmypy --no-warn-unused-ignores # same as --unused-ignores=none

And the mypy -h usage output would just steer people toward using the new, less confusing options. Potentially using the old options could emit a message like "This option is deprecated, please use <new option> instead", if we may want to remove the confusing options at some point in the future.

@jab
Copy link
Contributor Author

jab commented Mar 15, 2022

Worth mentioning that mypy's current warn-unused-ignores behavior combines with #12358 (can only use type: ignore, not mypy: ignore) to make it harder to use mypy with other type checkers:

Suppose I am using two type checkers on the same codebase, and one of the type checkers is mypy. I have mypy's "warn-unused-ignores" option enabled, and I have the equivalent option enabled for the other type checker.

For a given line of code, mypy is emitting an error which is a false positive, and the other type checker is not emitting an error for that line.

If I add a type: ignore to that line (with a link to the false positive bug in mypy so I remember to remove the type: ignore once the bug is fixed), my codebase will type check with mypy successfully. But the other type checker will error with an "unused type ignore" error.

If I remove the type: ignore from that line, my codebase will type check with the other type checker successfully, but not with mypy, due to the false positive bug.

To play nicely with other type checkers in the light that different type checkers have different false positive bugs, all type checkers should support both a way to warn-but-not-error on unused ignores, as well as supporting an ignore syntax specific to only that type checker (e.g. "mypy: ignore").

@JelleZijlstra JelleZijlstra added the topic-type-ignore # type: ignore comments label Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-ignore # type: ignore comments
Projects
None yet
Development

No branches or pull requests

2 participants