[ty] Allow tuple[Any, ...] to assign to tuple[int, *tuple[int, ...]]#21803
[ty] Allow tuple[Any, ...] to assign to tuple[int, *tuple[int, ...]]#21803charliermarsh merged 2 commits intomainfrom
tuple[Any, ...] to assign to tuple[int, *tuple[int, ...]]#21803Conversation
Diagnostic diff on typing conformance testsChanges were detected when running ty on typing conformance tests--- old-output.txt 2025-12-05 19:01:00.800057224 +0000
+++ new-output.txt 2025-12-05 19:01:04.523089702 +0000
@@ -934,7 +934,6 @@
tuples_type_compat.py:32:10: error[invalid-assignment] Object of type `tuple[int, *tuple[int, ...]]` is not assignable to `tuple[int]`
tuples_type_compat.py:33:10: error[invalid-assignment] Object of type `tuple[int, ...]` is not assignable to `tuple[int]`
tuples_type_compat.py:43:22: error[invalid-assignment] Object of type `tuple[int, ...]` is not assignable to `tuple[int]`
-tuples_type_compat.py:47:40: error[invalid-assignment] Object of type `tuple[Any, ...]` is not assignable to `tuple[int, *tuple[str, ...]]`
tuples_type_compat.py:62:26: error[invalid-assignment] Object of type `tuple[int, ...]` is not assignable to `tuple[int, int]`
tuples_type_compat.py:75:9: error[type-assertion-failure] Type `tuple[int]` does not match asserted type `tuple[int] | tuple[str, str] | tuple[int, *tuple[str, ...], int]`
tuples_type_compat.py:80:9: error[type-assertion-failure] Type `tuple[str, str] | tuple[int, int]` does not match asserted type `tuple[int] | tuple[str, str] | tuple[int, *tuple[str, ...], int]`
@@ -1031,4 +1030,4 @@
typeddicts_usage.py:28:17: error[missing-typed-dict-key] Missing required key 'name' in TypedDict `Movie` constructor
typeddicts_usage.py:28:18: error[invalid-key] Unknown key "title" for TypedDict `Movie`: Unknown key "title"
typeddicts_usage.py:40:24: error[invalid-type-form] The special form `typing.TypedDict` is not allowed in type expressions
-Found 1033 diagnostics
+Found 1032 diagnostics
|
|
|
The conformance change is correct. |
|
Confused by the change in |
|
Those two are known unstable diagnostics currently, not related to this PR. |
|
Oh let's go, thank you. |
AlexWaygood
left a comment
There was a problem hiding this comment.
This PR regresses some cases. This assertion passes on main, but fails on your PR branch:
from typing import Any
from ty_extensions import static_assert, is_assignable_to
static_assert(not is_assignable_to(tuple[*tuple[Any, ...], Any], tuple[*tuple[Any, ...], Any, Any]))The spec carves out a very specific special case here for tuple[Any, ...] in particular. The special case does not apply to "mixed" tuples that have Any as their variable-length element but also have suffixes and prefixes
|
Oh hang on, maybe I'm wrong. The bit at the top of the "tuples" section of the spec suggests that this is a very specific special case. But lower down, the spec also says
|
There was a problem hiding this comment.
Okay, this looks great to me. Thank you!!
The only thing I think would be great to add would be to add the test case that I initially thought this PR was regressing 😆 I.e., let's add this assertion:
from typing import Any
from ty_extensions import static_assert, is_assignable_to
# `*tuple[Any, ...]` can materialize to a tuple of any length as a special case,
# so this passes:
static_assert(is_assignable_to(tuple[*tuple[Any, ...], Any], tuple[*tuple[Any, ...], Any, Any]))It looks like we already link to the relevant parts of the spec in that mdtest
* origin/main: [ty] Allow `tuple[Any, ...]` to assign to `tuple[int, *tuple[int, ...]]` (#21803) [ty] Support renaming import aliases (#21792) [ty] Add redeclaration LSP tests (#21812) [ty] more detailed description of "Size limit on unions of literals" in mdtest (#21804) [ty] Complete support for `ParamSpec` (#21445) [ty] Update benchmark dependencies (#21815)
* origin/main: [ty] Add test case for fixed panic (#21832) [ty] Avoid double-analyzing tuple in `Final` subscript (#21828) [flake8-bandit] Fix false positive when using non-standard `CSafeLoader` path (S506). (#21830) Add minimal-size build profile (#21826) [ty] Allow `tuple[Any, ...]` to assign to `tuple[int, *tuple[int, ...]]` (#21803) [ty] Support renaming import aliases (#21792) [ty] Add redeclaration LSP tests (#21812) [ty] more detailed description of "Size limit on unions of literals" in mdtest (#21804) [ty] Complete support for `ParamSpec` (#21445) [ty] Update benchmark dependencies (#21815)
Summary
Closes astral-sh/ty#1750.