Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ static_assert(is_assignable_to(tuple[Any, ...], tuple[Any, Any]))
static_assert(is_assignable_to(tuple[Any, ...], tuple[int, ...]))
static_assert(is_assignable_to(tuple[Any, ...], tuple[int]))
static_assert(is_assignable_to(tuple[Any, ...], tuple[int, int]))
static_assert(is_assignable_to(tuple[Any, ...], tuple[int, *tuple[int, ...]]))
static_assert(is_assignable_to(tuple[Any, ...], tuple[*tuple[int, ...], int]))
static_assert(is_assignable_to(tuple[Any, ...], tuple[int, *tuple[int, ...], int]))
```

This also applies when `tuple[Any, ...]` is unpacked into a mixed tuple.
Expand All @@ -560,6 +563,10 @@ static_assert(is_assignable_to(tuple[*tuple[Any, ...], int], tuple[int, ...]))
static_assert(is_assignable_to(tuple[*tuple[Any, ...], int], tuple[int]))
static_assert(is_assignable_to(tuple[*tuple[Any, ...], int], tuple[int, int]))

# `*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]))

static_assert(is_assignable_to(tuple[int, *tuple[Any, ...], int], tuple[int, *tuple[Any, ...], int]))
static_assert(is_assignable_to(tuple[int, *tuple[Any, ...], int], tuple[Any, ...]))
static_assert(not is_assignable_to(tuple[int, *tuple[Any, ...], int], tuple[Any]))
Expand All @@ -580,6 +587,9 @@ static_assert(not is_assignable_to(tuple[int, ...], tuple[Any, Any]))
static_assert(is_assignable_to(tuple[int, ...], tuple[int, ...]))
static_assert(not is_assignable_to(tuple[int, ...], tuple[int]))
static_assert(not is_assignable_to(tuple[int, ...], tuple[int, int]))
static_assert(not is_assignable_to(tuple[int, ...], tuple[int, *tuple[int, ...]]))
static_assert(not is_assignable_to(tuple[int, ...], tuple[*tuple[int, ...], int]))
static_assert(not is_assignable_to(tuple[int, ...], tuple[int, *tuple[int, ...], int]))

static_assert(is_assignable_to(tuple[int, *tuple[int, ...]], tuple[int, *tuple[Any, ...]]))
static_assert(is_assignable_to(tuple[int, *tuple[int, ...]], tuple[Any, ...]))
Expand Down
36 changes: 30 additions & 6 deletions crates/ty_python_semantic/src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,22 @@ impl<'db> VariableLengthTuple<Type<'db>> {
relation_visitor,
disjointness_visitor,
),
EitherOrBoth::Right(_) => {
EitherOrBoth::Right(other_ty) => {
// The rhs has a required element that the lhs is not guaranteed to
// provide.
return ConstraintSet::from(false);
// provide, unless the lhs has a dynamic variable-length portion
// that can materialize to provide it (for assignability only),
// as in `tuple[Any, ...]` matching `tuple[int, int]`.
if !relation.is_assignability() || !self.variable.is_dynamic() {
return ConstraintSet::from(false);
}
self.variable.has_relation_to_impl(
db,
other_ty,
inferable,
relation,
relation_visitor,
disjointness_visitor,
)
}
};
if result
Expand Down Expand Up @@ -1033,10 +1045,22 @@ impl<'db> VariableLengthTuple<Type<'db>> {
relation_visitor,
disjointness_visitor,
),
EitherOrBoth::Right(_) => {
EitherOrBoth::Right(other_ty) => {
// The rhs has a required element that the lhs is not guaranteed to
// provide.
return ConstraintSet::from(false);
// provide, unless the lhs has a dynamic variable-length portion
// that can materialize to provide it (for assignability only),
// as in `tuple[Any, ...]` matching `tuple[int, int]`.
if !relation.is_assignability() || !self.variable.is_dynamic() {
return ConstraintSet::from(false);
}
self.variable.has_relation_to_impl(
db,
*other_ty,
inferable,
relation,
relation_visitor,
disjointness_visitor,
)
}
};
if result
Expand Down
Loading