Skip to content

error: 'Union[Any, Tuple[Any, Any]]' object is not iterable #1855

Closed

Description

@gvanrossum

Code:

from typing import *
Pair = Tuple[Any, Any]
def f(a: Union[Any, Pair]):
    x, y = a  # E: 'Union[Any, Tuple[Any, Any]]' object is not iterable

If the type of argument is either just Any or just Pair the code passes. But with the union it gives the error.

UPDATE: I don't think the unreduced union is at fault -- an example with two different Tuple types of length 2 also fails. It's more that the unpack operation isn't tried for each variant of the union.

Activity

  1. gnprice commented on Jul 14, 2016

    @gnprice
    Collaborator

    We've seen this a couple of times internally.

  2. added this to the milestone on Jul 14, 2016
  3. elazarg commented on Sep 18, 2016

    @elazarg
    Contributor

    Could be related to #2128 ?

  4. gvanrossum commented on Sep 18, 2016

    @gvanrossum
    MemberAuthor
  5. elazarg commented on Sep 18, 2016

    @elazarg
    Contributor

    Turns out it's not directly the same cause: the union case was not handled and not tested.

    I believe it's the same root cause but in a deeper sense: the matching of multi-assignment and function calls define is typechecking and should be handled through the subtype module. Many subtype-related decisions are scattered through the checker (such as itemwise matching, and the iteration over union elements that I just added).

    But even if it's not moved to subtype.py, I think it'll help to have a single matcher, common to function calls, multi assignment, multi-iteration (what's the terminology for that?) etc. If it was the case, my fix would have probably fixed #2128 too (I did not study it yet, though; It's just a guess).

  6. removed this from the milestone on Mar 29, 2017
  7. self-assigned this
    on May 13, 2017
  8. nickpresta commented on May 23, 2017

    @nickpresta

    EDIT: I'm running mypy 0.511 -- I'm trying with the latest master now.

    We're seeing this problem manifest itself with our code like this (no OSS code to link):

    class User(NamedTuple):
        id: UUID
        # more attributes
        
    
    class UserService:
        def get_or_create(self, user_id: UUID, user_data: Optional[dict] = None) -> Tuple[User, bool]:
            return User(id=uuid4()), False
    
    
    class Authentication:
        def authenticate(self, request: Request) -> Tuple[User, None]:
            user, _ = UserService().get_or_create(user_id=uuid4())  # ERROR HERE
            return user, None

    The error I get is:

    error: 'Union[Any, Tuple[Tuple[uuid.UUID, datetime.datetime, datetime.datetime, datetime.datetime, builtins.bool, builtins.str, builtins.str, builtins.str, fallback=users.domain.User], builtins.bool]]' object is not iterable
    

    The Tuple[uuid.UUID, datetime.datetime, ...] bit is our User namedtuple.

    As soon as I do something like:

    class Authentication:
        def authenticate(self, request: Request) -> Tuple[User, None]:
            some_id = uuid4()
            user = UserService().get_or_create(user_id=some_id)[0]  # explicitly unpack first element
            return user, None

    The error then goes away.

    Right now we're working around it with the explicit index but any other insight would be much appreciated.

    Thanks

  9. gvanrossum commented on May 23, 2017

    @gvanrossum
    MemberAuthor

    Does it repro with the latest master?

  10. nickpresta commented on May 23, 2017

    @nickpresta

    @gvanrossum I checked out master and tried to reproduce with mypy 0.520-dev and I don't get the error anymore.

    Thanks.

    We will wait for 0.520 to be published to PyPI.

  11. gvanrossum commented on May 23, 2017

    @gvanrossum
    MemberAuthor

    OK, then per our policy I will close this issue. (We close issues when they are fixed in master.)

  12. ilevkivskyi commented on May 23, 2017

    @ilevkivskyi
    Member

    But the original issue is not fixed, I just checked and I still see the same error:
    'Union[Any, Tuple[Any, Any]]' object is not iterable

  13. gvanrossum commented on May 23, 2017

    @gvanrossum
    MemberAuthor

    Whoops, sorry.

  14. added a commit that references this issue on Oct 11, 2017
    77ce5e2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions