Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy is still too eager about outer context #5874

Open
ilevkivskyi opened this issue Nov 8, 2018 · 3 comments
Open

Mypy is still too eager about outer context #5874

ilevkivskyi opened this issue Nov 8, 2018 · 3 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables topic-union-types

Comments

@ilevkivskyi
Copy link
Member

Consider this example:

T = TypeVar('T')
S = TypeVar('S')

def add(x: List[T], y: List[S]) -> List[Union[T, S]]: ...

a: List[int]
b: List[str]

e: List[Union[int, str]] = add(a, b)

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.

This blocks #5873

@ilevkivskyi ilevkivskyi added bug mypy got something wrong topic-type-variables priority-1-normal topic-union-types false-positive mypy gave an error on correct code labels Nov 8, 2018
@ilevkivskyi ilevkivskyi changed the title Mypy is still to eager about outer context Mypy is still too eager about outer context Nov 8, 2018
@ilevkivskyi
Copy link
Member Author

But again, a permanent solution for this kind of issues is to put all constraints into the same bin.

@ilevkivskyi
Copy link
Member Author

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.

@JukkaL
Copy link
Collaborator

JukkaL commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables topic-union-types
Projects
None yet
Development

No branches or pull requests

2 participants