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
ilevkivskyi commented on Nov 8, 2018
But again, a permanent solution for this kind of issues is to put all constraints into the same bin.
ilevkivskyi commented on Nov 8, 2018
Actually this is not specific to unions against unions, unions against instance also fails:
with:
because Union[T, S] <: object infers T <: object and S <: object.
JukkaL commented on 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.