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
It fails on the last line with the following (I skip invariance notes):
error: Argument 1 to "add" has incompatible type "List[int]"; expected "List[Union[int, str]]"
error: Argument 2 to "add" has incompatible type "List[str]"; expected "List[Union[int, str]]"
If I remove the annotation for e, everything works fine.
When previously working on the external context issues, I found that I don't like how constrains for unions against unions are inferred (not that they are wrong, just counterintuitive). For example, Union[T, S] <: Union[str, int] infers T < : Union[str, int] and S <: Union[str, int], which is technically fine, but leads to errors like above.
Actually this is not specific to unions against unions, unions against instance also fails:
e: Sequence[object] =add(a, b)
with:
error: Argument 1 to "add" has incompatible type "List[int]"; expected "List[object]"
error: Argument 2 to "add" has incompatible type "List[str]"; expected "List[object]"
because Union[T, S] <: object infers T <: object and S <: object.
Solving both return type context and argument constraints in one go seems to be the only somewhat promising way to solve the issue that I can think of. This would be a big change however, and it could result in other difficulties.
My proposal is to revert the change to the signature of list.__add__ in typeshed.
Activity
[-]Mypy is still to eager about outer context[/-][+]Mypy is still too eager about outer context[/+]ilevkivskyi commentedon Nov 8, 2018
But again, a permanent solution for this kind of issues is to put all constraints into the same bin.
ilevkivskyi commentedon Nov 8, 2018
Actually this is not specific to unions against unions, unions against instance also fails:
with:
because
Union[T, S] <: objectinfersT <: object and S <: object.JukkaL commentedon Nov 29, 2018
Solving both return type context and argument constraints in one go seems to be the only somewhat promising way to solve the issue that I can think of. This would be a big change however, and it could result in other difficulties.
My proposal is to revert the change to the signature of
list.__add__in typeshed.