You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Are you reporting a bug, or opening a feature request?
bug (or intended behavior?)
Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
fromtypingimportcast, List, Union, TypeVarA=TypeVar('A')
B=TypeVar('B')
defconcat(xs: List[A], ys: List[B]) ->List[Union[A, B]]:
returncast(List[Union[A, B]], xs) +cast(List[Union[A, B]], ys)
deftest() ->List[Union[int, str]]:
xs= [1, 2, 3]
ys= ['A', 'B']
zs=concat(xs, ys)
reveal_type(xs) # builtins.list[builtins.int*]reveal_type(ys) # builtins.list[builtins.str*]reveal_type(zs) # builtins.list[Union[builtins.int*, builtins.str*]]#return zs # OKreturnconcat(xs, ys) # NG# union_fragile.py:17: error: Argument 1 to "concat" has incompatible type "List[int]"; expected "List[Union[int, str]]"# union_fragile.py:17: note: "List" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance# union_fragile.py:17: note: Consider using "Sequence" instead, which is covariant# union_fragile.py:17: error: Argument 2 to "concat" has incompatible type "List[str]"; expected "List[Union[int, str]]"
What is the actual behavior/output?
$ mypy union_fragile.py
mypy union_fragile.py
union_fragile.py:13: note: Revealed type is 'builtins.list[builtins.int*]'
union_fragile.py:14: note: Revealed type is 'builtins.list[builtins.str*]'
union_fragile.py:15: note: Revealed type is 'builtins.list[Union[builtins.int*, builtins.str*]]'
union_fragile.py:17: error: Argument 1 to "concat" has incompatible type "List[int]"; expected "List[Union[int, str]]"
union_fragile.py:17: note: "List" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance
union_fragile.py:17: note: Consider using "Sequence" instead, which is covariant
union_fragile.py:17: error: Argument 2 to "concat" has incompatible type "List[str]"; expected "List[Union[int, str]]"
Found 2 errors in 1 file (checked 1 source file)
What is the behavior/output you expect?
I expect this to be accepted.
Because if I replace return concat(xs, ys) with return zs, it is accepted.
What are the versions of mypy and Python you are using?
Do you see the same issue after installing mypy from Git master?
(I'm not familiar with mypy internals and the following is just my guess.)
It seems that mypy first compared List[Union[A, B]] (the return type of concat) with expected type List[Union[int, str]] and derived an instantiation A = B = Union[int, str]. But, since List is non-injective type constructor, there are other possible instantiation including:
A=int, B=str,
A=str, B=int,
A=Union[int, str], B=int,
..
Therefore, committing to a particular instantiation (A = B = Union[int, str] in this case) is not possible at the moment.
bug (or intended behavior?)
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
I expect this to be accepted.
Because if I replace
return concat(xs, ys)
withreturn zs
, it is accepted.Do you see the same issue after installing mypy from Git master?
I see the same issue after install installing mypy from Git master.
I did not provide any flags.
The text was updated successfully, but these errors were encountered: