-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
casts and # type: ignore comments are unpleasant things, and have the potential to mask future type errors. Some particular instances of these in a program may become obsolete over time, either due to type changes in the rest of the program, or due to mypy becoming smarter (see #1447 for some instances of the latter). It would be nice if mypy could point out such instances in the modules it type checks. For cast(T, expr) this could mean checking whether expr already has inferred type T, and for # type: ignore, which works by silencing error messages reported on the line of the comment, this could mean checking whether any such error messages were actually generated (and then discarded).
One issue is how to avoid false positives in a situation where a cast or # type: ignore comment is needed when type checking under certain configurations, but not others (for example, dependent on the python version or the version of a dependency). A special case of this issue is type checking generic functions with type variables with value restrictions, which are type checked once for each combination of legal values; a cast might be needed when specializing AnyStr to bytes but not str, for example. This case could be handled entirely by mypy with some extra care, but the general issue remains.