Skip to content

Question about usage of union of Tuples #391

Closed

Description

@tacaswell

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

Activity

  1. ilevkivskyi commented on Feb 13, 2017

    @ilevkivskyi
    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).

  2. tacaswell commented on Feb 13, 2017

    @tacaswell
    Author

    Thanks! Sorry for the noise!

  3. ilevkivskyi commented on Feb 13, 2017

    @ilevkivskyi
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions