Skip to content

Inconsistent type inference with Union[List[int], T] context #3506

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

Open
JukkaL opened this issue Jun 7, 2017 · 0 comments
Open

Inconsistent type inference with Union[List[int], T] context #3506

JukkaL opened this issue Jun 7, 2017 · 0 comments
Labels
bug mypy got something wrong topic-union-types

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 7, 2017

Mypy behaves unexpectedly when caller provides a list expression that has non-int items to a function that accepts Union[List[int], T]:

from typing import *
T = TypeVar('T')
def f(a: Union[List[int], T]) -> T: pass
f([1])  # OK
f([''])  # E: List item 0 has incompatible type "str"   <<--- unexpected
a = ['']
f(a)  # OK

It would be better if mypy would always recognize that List[str] is a valid argument for f. However, if given a [] argument the inferred return type should be List[int].

This was originally discussed at #3501 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-union-types
Projects
None yet
Development

No branches or pull requests

3 participants