I tested the following snippet with 4 common type checkers for Python and, to my surprise, none of them complained:
from typing import Any
def length(s: str) -> int:
return len(s)
def any_length(o: Any) -> int:
return length(o)
if __name__ == "__main__":
print(any_length(1234))
It's easy to predict that running this code will result in an exception:
TypeError: object of type 'int' has no len()
mypy:
Success: no issues found in 1 source file
pytype:
Success: no errors found
pyright:
0 errors, 0 warnings, 0 informations
Completed in 0.435sec
pyre:
ƛ No type errors found
I would expect at least a warning saying that Any
is not a subtype of str
and therefore application of length: str -> int
to an object of type Any
is unsafe. Is there something about these particular types that makes it difficult for type checkers to consider this simple case? The problem of determining whether a concrete type is a subtype of another doesn't seem undecidable, but maybe I'm wrong here?