Skip to content

Empty variadic tuple subtype not assignable to tuple[int, ...] #19110

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

Open
jorenham opened this issue May 18, 2025 · 3 comments
Open

Empty variadic tuple subtype not assignable to tuple[int, ...] #19110

jorenham opened this issue May 18, 2025 · 3 comments
Labels
bug mypy got something wrong topic-pep-646 PEP 646 (TypeVarTuple, Unpack)

Comments

@jorenham
Copy link
Contributor

class Shape[*Ts](tuple[*Ts]): ...
class Shape0(Shape[()]): ...
class Shape1(Shape[int]): ...

def rank(shape: tuple[int, ...]) -> int:
    return len(shape)

_ = rank(Shape())       # ✅
_ = rank(())            # ✅
_ = rank(Shape0())      # ❌  Argument 1 to "rank" has incompatible type "Shape0"; expected "tuple[int, ...]"
_ = rank(Shape1([1]))   # ✅

playground

Perhaps this is related to #19105, which also involves empty tuple subtypes.

@jorenham jorenham added the bug mypy got something wrong label May 18, 2025
@jorenham
Copy link
Contributor Author

jorenham commented May 18, 2025

I confirmed that #19100 does not solve this

@jorenham
Copy link
Contributor Author

I'm guessing that it's a consequence of this issue, but it might be worth noting that these variadic tuple subtypes ignore the constraints of typevars:

class Shape[*Ts](tuple[*Ts]): ...

class Shape0(Shape[()]): ...
class Shape1(Shape[int]): ...
class ShapeN(Shape[*tuple[int, ...]]): ...


def upcast[T: (tuple[()], tuple[int], tuple[int, ...])](shape: T) -> T:
    return shape

reveal_type(upcast(Shape0()))  # ❌ Shape0
reveal_type(upcast(Shape1()))  # ✅ tuple[builtins.int]
reveal_type(upcast(ShapeN()))  # ❌ ShapeN

full output:

main.py:11: error: Value of type variable "T" of "upcast" cannot be "Shape0"  [type-var]
main.py:11: note: Revealed type is "__main__.Shape0"
main.py:12: note: Revealed type is "tuple[builtins.int]"
main.py:13: error: Value of type variable "T" of "upcast" cannot be "ShapeN"  [type-var]
main.py:13: note: Revealed type is "__main__.ShapeN"
Found 2 errors in 1 file (checked 1 source file)

playground

For reference, pyright reveals these as tuple[()], tuple[int], tuple[int, ...] (playground).

Let me know in case I should raise this separately.

@sterliakov
Copy link
Collaborator

Wow, you opened a can of worms with your typevartuple experiments! This is definitely a bug.

@sterliakov sterliakov added the topic-pep-646 PEP 646 (TypeVarTuple, Unpack) label May 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-646 PEP 646 (TypeVarTuple, Unpack)
Projects
None yet
Development

No branches or pull requests

2 participants