Skip to content

Request: equivalent CLI option to ruff's --add-noqa to add type: ignore comments #16751

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

Open
jamesbraza opened this issue Jan 6, 2024 · 4 comments
Labels

Comments

@jamesbraza
Copy link
Contributor

Feature

ruff has an --add-noqa option to add noqa directives in all places: https://docs.astral.sh/ruff/linter/#inserting-necessary-suppression-comments

It would nice if mypy had something similar, like a --add-ignore CLI argument

Pitch

This would enable a new route to adopt mypy, mainly suppressing all type errors with type: ignore comments, and slowly removing them over time.

@jamesbraza
Copy link
Contributor Author

Turns out there are two third party projects for this:

I tried using both of them today (2/6/2024), and they sort of work, but they both have bugs (e.g. not in sync with mypy latest release, broken by pretty = true, don't add error codes to type ignores, can add duplicate codes).

Imo it would be great to have this capability directly supported within mypy

@emmatyping
Copy link
Member

I expect it is unlikely we could support this. Unlike most linters that work at the CST level, mypy works at the AST level, and any --add-noqa would cause significant whitespace and other formatting changes if we simply unparsed an AST with a type ignore introduced. I expect most projects would not accept that tradeoff.

Instead, perhaps we can suggest using the mypy configurataion to skip checking modules that have more than N errors. It is much better IMO to start with checking only one or two modules that get used all over a code base than sprinkle type ignore everywhere and then go back and remove them gradually. Unlike with noqa, type ignores in this case are likely to be real type safety concerns, rather than style issues.

If people are really compelled to this idea it could perhaps be implemented once there is an implementation of #7468, and as an extra step to suppressing the errors, modify the CST of affected files.

@JelleZijlstra
Copy link
Member

I don't think this would be particularly difficult to do; we don't need to look at the AST or CST at all, but can find the line in the source file where the error is emitted and add # type: ignore to it.

I believe Pyre provides a similar feature.

@hauntsaninja
Copy link
Collaborator

In particular, --output json should make it quite easy to build a basic script here

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

4 participants