Skip to content Skip to sidebar Skip to footer

How To Make Mypy Complain About Assigning An Any To An Int

mypy --strict dutifully complains about the following code: from typing import Any, Dict def main() -> None: my_str: str = 'hello' my_int: int = my_str if __name__ ==

Solution 1:

And in my understanding an Any is just the Union of all possible types.

That's not correct. Any is an escape hatch, an annotation for variables that you want the type checker to ignore. It certainly is not a union.

From the mypy documentation on Any:

A value with the Any type is dynamically typed. Mypy doesn’t know anything about the possible runtime types of such value. Any operations are permitted on the value, and the operations are only checked at runtime. You can use Any as an “escape hatch” when you can’t use a more precise type for some reason.

(Bold emphasise mine)

It explicitly covers your case:

Any is compatible with every other type, and vice versa. You can freely assign a value of type Any to a variable with a more precise type:

a: Any = None
s: str = ''
a = 2     # OK (assign "int"to"Any")
s = a     # OK (assign "Any"to"str")

Declared (and inferred) types are ignored (or erased) at runtime. They are basically treated as comments, and thus the above code does not generate a runtime error, even though s gets an int value when the program is run, while the declared type of s is actually str!

So the correct approach is to not use Any if you want the type checker to keep tracking how the value is used. Use a Union[], as you did in your third example, or re-think your data structures to allow for better type hinting. For example, rather than use a dictionary with a union value type, consider using a named tuple or dataclass with explicit fields and a specific type for each field.

Solution 2:

For those that read about this later, the actual solution is to use the "disallow any" family of command line flags, as describes in this answer.

Post a Comment for "How To Make Mypy Complain About Assigning An Any To An Int"