GeezerVison is showing github.com — Empty variadic `tuple` subtype not assignable to `tuple[int, ...]` · Issue #19110 · python/mypy
Empty variadic tuple subtype not assignable to tuple[int, ...] #19110Descriptionclass 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. Activityjorenham commented on May 18, 2025 ContributorAuthor More actions I confirmed that #19100 does not solve this jorenham commented on May 19, 2025 ContributorAuthor More actions 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 commented on May 19, 2025 Wow, you opened a can of worms with your typevartuple experiments! This is definitely a bug. Reacted by Joren Hammudoglu
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|