Skip to content

Commit f7b458a

Browse files
committed
fix regression, add more tests
1 parent 463b5dc commit f7b458a

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

crates/ty_python_semantic/resources/mdtest/type_properties/is_equivalent_to.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ static_assert(is_equivalent_to(Any, Any | Intersection[Any, str]))
138138
static_assert(is_equivalent_to(Any, Intersection[str, Any] | Any))
139139
static_assert(is_equivalent_to(Any, Any | Intersection[Any, Not[None]]))
140140
static_assert(is_equivalent_to(Any, Intersection[Not[None], Any] | Any))
141+
142+
static_assert(is_equivalent_to(Any, Unknown | Intersection[Unknown, str]))
143+
static_assert(is_equivalent_to(Any, Intersection[str, Unknown] | Unknown))
144+
static_assert(is_equivalent_to(Any, Unknown | Intersection[Unknown, Not[None]]))
145+
static_assert(is_equivalent_to(Any, Intersection[Not[None], Unknown] | Unknown))
141146
```
142147

143148
## Tuples

crates/ty_python_semantic/resources/mdtest/union_types.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,28 @@ def _(c: BC, d: BD):
306306
reveal_type(c) # revealed: Literal[b""]
307307
reveal_type(d) # revealed: Literal[b""]
308308
```
309+
310+
## Unions of tuples
311+
312+
A union of a fixed-length tuple and a variable-length tuple must be collapsed to the variable-length
313+
element, never to the fixed-length element (`tuple[()] | tuple[Any, ...]` -> `tuple[Any, ...]`, not
314+
`tuple[()]`).
315+
316+
```py
317+
from typing import Any
318+
319+
def f(
320+
a: tuple[()] | tuple[int, ...],
321+
b: tuple[int, ...] | tuple[()],
322+
c: tuple[int] | tuple[str, ...],
323+
d: tuple[str, ...] | tuple[int],
324+
e: tuple[()] | tuple[Any, ...],
325+
f: tuple[Any, ...] | tuple[()],
326+
):
327+
reveal_type(a) # revealed: tuple[int, ...]
328+
reveal_type(b) # revealed: tuple[int, ...]
329+
reveal_type(c) # revealed: tuple[int] | tuple[str, ...]
330+
reveal_type(d) # revealed: tuple[str, ...] | tuple[int]
331+
reveal_type(e) # revealed: tuple[Any, ...]
332+
reveal_type(f) # revealed: tuple[Any, ...]
333+
```

crates/ty_python_semantic/src/types/tuple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ impl<'db> VariableLengthTuple<Type<'db>> {
757757
// (or any other dynamic type), then the `...` is the _gradual choice_ of all
758758
// possible lengths. This means that `tuple[Any, ...]` can match any tuple of any
759759
// length.
760-
if relation == TypeRelation::Subtyping || !matches!(self.variable, Type::Dynamic(_))
760+
if !relation.is_assignability() || !matches!(self.variable, Type::Dynamic(_))
761761
{
762762
return ConstraintSet::from(false);
763763
}

0 commit comments

Comments
 (0)