Being stricter with Any #10892
|
I would like this to report an error: from typing import Any, cast
def a(n: int):
pass
b: Any = 0 # or b: Any = ""
a(b)And require an explicit cast for example: a(cast(int, b))I found no mention of this among the configurations. Mypy not different either. Is this impossible currently to detect and enforce? |
Replies: 2 comments 2 replies
|
The Python type system is, by design, a gradual type system. It wouldn't be appropriate for a type checker to generate an error for If you want to generate a type error here, you should not use |
|
def a(n: int) -> None: ...
def foo(b: object) -> None:
a(b) # E: Argument of type "object" cannot be assigned to parameter "n" of type "int" in function "a"
foo(1) # ok
foo("") # okFor more on how |
objectmay have a meaning closer to what you're looking for:For more on how
objectvsAnydiffer, you can refer to