Skip to content

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

Closed

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.

Activity

  1. added a commit that references this issue on Aug 15, 2022
    7df6183
  2. added a commit that references this issue on Aug 19, 2022
    7d95e2e
  3. added 2 commits that reference this issue on Sep 10, 2022
    64d3d04
    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

      No branches or pull requests

      Issue actions