python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.26k stars 2.79k forks source link

Feature request: option that prohibits any use of type:ignore, and other type-checking-suppression mechanisms #17078

Open wyattscarpenter opened 6 months ago

wyattscarpenter commented 6 months ago

Feature

PEP 484 lists several ways to ignore type checking https://peps.python.org/pep-0484/#compatibility-with-other-uses-of-function-annotations:

I would like an option that disallows all of these methods, eg --disallow-type-ignores

Pitch

My basic idea here is that I want to actually enforce typing and have it completely statically verified, when possible. If that isn't happening, I'd like to know! And possibly refactor my code to remove the ignores. — I would like mypy to be able to tell me whether or not my types are completely automatically enforced, without any ignores, if I use the right flag. This seems like an ergonomic way to enable that. (There are probably other ways to cheat the python type checker that could also be ruled out, with other flags.)

While useful, these type-checker-ignore features get around static type safety without ensuring dynamic type correctness, which is not ideal. Sometimes you have to use them—you know how it is—but it's not ideal. That means, in my opinion, it would be great to have a option for disallowing their use, to ensure mandatory type-safety in codebases where these workarounds are not in fact needed.

I don't think it would make sense to turn this option on by default, nor in strict mode. The ecosystem of typing just isn't thorough and unimpingable enough, in my experience. It could maybe make sense to go in some hypothetical ultrastrict mode, but I don't think most strict mode users would benefit from this option being turned on.

wyattscarpenter commented 6 months ago

I've also made a feature request in the ruff linter, here: https://github.com/astral-sh/ruff/issues/10672