Skip to content

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

Closed
@tdsmith

Description

@tdsmith

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions