Skip to content

Instances of type variables bounded by an iterable type support iteration but not unpacking #13402

@tdsmith

Description

@tdsmith
Contributor

Bug Report

Instances of type variables bounded by an iterable type support iteration but not unpacking.

To Reproduce

from typing import TypeVar

class A:
    pass

ATuple = TypeVar("ATuple", bound=tuple[A, ...])

def f(aa: ATuple) -> None:
    for a in aa:               # this is ok
        print(a)

f((A(),))

def g(aa: ATuple) -> None:
    (a,) = aa                  # "ATuple" object is not iterable
    print(a)

g((A(),))

Bounding the TypeVar by list[A] has the same behaviour as bounding it by tuple[A, ...].

Expected Behavior

No error -- an ATuple should support both iteration and unpacking.

Actual Behavior

f() checks clean.

In g(), unpacking fails with:

error: "ATuple" object is not iterable
error: Cannot determine type of "a"

Your Environment

This reproduces in all versions in the playground.

Activity

added a commit that references this issue on Aug 15, 2022
7df6183
added a commit that references this issue on Aug 19, 2022
7d95e2e
added a commit that references this issue on Sep 10, 2022
64d3d04
added a commit that references this issue on Sep 12, 2022
70bc348
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

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @tdsmith

      Issue actions

        Instances of type variables bounded by an iterable type support iteration but not unpacking · Issue #13402 · python/mypy