Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tdsmith opened this issue Aug 13, 2022 · 0 comments · Fixed by #13425
Closed
Labels
bug mypy got something wrong

Comments

@tdsmith
Copy link
Contributor

tdsmith commented Aug 13, 2022

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.

@tdsmith tdsmith added the bug mypy got something wrong label Aug 13, 2022
tdsmith added a commit to tdsmith/mypy that referenced this issue Aug 15, 2022
TypeVars aren't iterable, but their bounds might be!
Resolve a TypeVar to its bounds before trying to decide how to unpack
one of its instances.

Fixes python#13402.
sobolevn pushed a commit that referenced this issue Aug 19, 2022
* Allow unpacking from TypeVars by resolving bounds

TypeVars aren't iterable, but their bounds might be!
Resolve a TypeVar to its bounds before trying to decide how to unpack
one of its instances.

Fixes #13402.
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue Sep 10, 2022
* Allow unpacking from TypeVars by resolving bounds

TypeVars aren't iterable, but their bounds might be!
Resolve a TypeVar to its bounds before trying to decide how to unpack
one of its instances.

Fixes python#13402.
hauntsaninja added a commit that referenced this issue Sep 12, 2022
…13425) (#13644)

TypeVars aren't iterable, but their bounds might be! Resolve a TypeVar
to its bounds before trying to decide how to unpack one of its
instances.

Fixes #13402

Co-authored-by: Tim D. Smith <github@tim-smith.us>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant