The following file:
from typing import List
class A:
def __init__(self, myStr):
self.chars: List[int] = list(myStr)
def toString(self):
return "".join(self.chars)
typechecks (note chars should be List[str] not List[int]): ā Python python3 -m mypy temp.py => Success: no issues found in 1 source file, but the following one, where I declare the return type of toString, does:
from typing import List
class A:
def __init__(self, myStr):
self.chars: List[int] = list(myStr)
def toString(self) -> str:
return "".join(self.chars)
ā Python python3 -m mypy temp.py
temp.py:9: error: Argument 1 to "join" of "str" has incompatible type "List[int]"; expected "Iterable[str]"
Found 1 error in 1 file (checked 1 source file)
Could anyone explain mypy's behaviour in this case? Why do I need to add the return type to the function to force mypy to correctly diagnose the problem? (it already has all necessary information: that chars is List[int] and join accepts Iterable[str])