Closed
Description
If you can come up with a better name for this issue, please let me know, this one is poor. The naming difficulty also lead to difficulty searching to see if this already existed, so apologies in advance if this is a duplicate issue.
When running this code through the typechecker:
from typing import Union, Dict, List
def g() -> List[Union[str, int]]:
x = ['a']
x += [1]
return x
def f() -> Dict[str, Union[str, int]]:
d = {'a': 'a'}
d['b'] = 1
return d
I get this output:
test.py: note: In function "g":
test.py:4: error: List item 0 has incompatible type "int"
test.py:5: error: Incompatible return value type (got List[str], expected List[Union[str, int]])
test.py: note: In function "f":
test.py:9: error: Incompatible types in assignment (expression has type "int", target has type "str")
test.py:10: error: Incompatible return value type (got Dict[str, str], expected Dict[str, Union[str, int]])
With both these functions, mypy is assuming the type on assignment, then having type errors due to conflicts with the explicitly declared return type.
Surely there should be a way to correctly infer the original type based on the return type?