Skip to content

List[Tuple[Union[...], Union[...]]] fails to type check #3624

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

Closed
rowillia opened this issue Jun 28, 2017 · 3 comments
Closed

List[Tuple[Union[...], Union[...]]] fails to type check #3624

rowillia opened this issue Jun 28, 2017 · 3 comments

Comments

@rowillia
Copy link
Contributor

Repro:

from typing import Any, Iterable, Union, Text, Tuple, List

KeyType = Union[Text, bytes, float, int]

def foo(a: KeyType, b: KeyType, c: Tuple[KeyType, KeyType], x: Iterable[Tuple[KeyType, KeyType]]) -> None:
    pass


def bar(x: List[Tuple[KeyType, KeyType]]) -> None:
    pass

x: List[Tuple[int, str]] = [(1, 'hello')]
foo(x[0][0], x[0][1], x[0], x)
bar(x)  # error: Argument 1 to "bar" has incompatible type List[Tuple[int, str]]; expected List[Tuple[Union[str, bytes, float, int], Union[str, bytes, float, int]]]

What's odd is that every element of x in the above example type checks correctly with foo, and it even works if we declare the parameter as a Iterable, but fails as soon as x is a List

@JelleZijlstra
Copy link
Member

That's because of invariance. List is invariant in its type argument.

@ilevkivskyi
Copy link
Member

Yes, I don't think this is a bug. There are already two issues for giving a better error message in such situations: #1115 and #3352.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 28, 2017

UsingMapping instead of List may work, since the prior is covariant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants