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

Question about usage of union of Tuples #391

Closed
tacaswell opened this issue Feb 13, 2017 · 3 comments
Closed

Question about usage of union of Tuples #391

tacaswell opened this issue Feb 13, 2017 · 3 comments
Labels
resolution: duplicate The idea/problem was already reported

Comments

@tacaswell
Copy link

I just started trying to apply typing to a code base, so I may be greatly miss-understanding what I am doing, but here we go :)

We have a case where we get a tuple back from a user supplied function which must be (None, None), (some_not_none, None), or (some_not_none, other_not_none), that the second value can only be not None if the first value is also not None. (This is why I did not do Tuple[Optional[int], Optional[int]]).

A minimal example to reproduce the issues is

from typing import Tuple, Union

InputType = Union[Tuple[None, None],
                  Tuple[int, None],
                  Tuple[int, int]]


def f(a: InputType) -> None:
    b, c = a

which gives

(dd36) ✔ ~ 
17:32 $ mypy --version
mypy 0.471
(dd36) ✔ ~ 
17:32 $ mypy /tmp/test.py
/tmp/test.py:9: error: 'Union[Tuple[void, void], Tuple[builtins.int, void], Tuple[builtins.int, builtins.int]]' object is not iterable

@ilevkivskyi
Copy link
Member

@tacaswell Oh, this is a well known bug in mypy, see e.g. python/mypy#1855 and python/mypy#1575. There is even a PR for this python/mypy#2219 but I don't know when it will land.

Probably you could use your workaround with less precise type, or maybe use an overload in stub file, something like

@overload
def f(a: Tuple[None, None]) -> None: ...
@overload
def f(a: Tuple[int, None]) -> None: ...
@overload
def f(a: Tuple[int, int]) -> None: ...

might work (overloads will be allowed also outside stubs, i.e. in source code, see python/mypy#2603, but it is also not clear when this lands).

@ilevkivskyi ilevkivskyi added resolution: duplicate The idea/problem was already reported question labels Feb 13, 2017
@tacaswell
Copy link
Author

Thanks! Sorry for the noise!

@ilevkivskyi
Copy link
Member

Sorry for the noise!

No problem at all :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution: duplicate The idea/problem was already reported
Projects
None yet
Development

No branches or pull requests

2 participants