Skip to content

Commit b4877f1

Browse files
authored
[red-knot] Ensure a gradual type can always be assigned to itself (#15675)
1 parent 3235cd8 commit b4877f1

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

crates/red_knot_python_semantic/resources/mdtest/type_properties/is_assignable_to.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,12 @@ static_assert(not is_assignable_to(int, Not[int]))
263263
static_assert(not is_assignable_to(int, Not[Literal[1]]))
264264

265265
static_assert(not is_assignable_to(Intersection[Any, Parent], Unrelated))
266+
static_assert(is_assignable_to(Intersection[Unrelated, Any], Intersection[Unrelated, Any]))
267+
static_assert(is_assignable_to(Intersection[Unrelated, Any], Intersection[Unrelated, Not[Any]]))
266268

267269
# TODO: The following assertions should not fail (see https://github.com/astral-sh/ruff/issues/14899)
268270
# error: [static-assert-error]
269271
static_assert(is_assignable_to(Intersection[Any, int], int))
270-
271-
# error: [static-assert-error]
272-
static_assert(is_assignable_to(Intersection[Unrelated, Any], Intersection[Unrelated, Any]))
273-
# error: [static-assert-error]
274-
static_assert(is_assignable_to(Intersection[Unrelated, Any], Intersection[Unrelated, Not[Any]]))
275272
# error: [static-assert-error]
276273
static_assert(is_assignable_to(Intersection[Unrelated, Any], Not[tuple[Unrelated, Any]]))
277274
```

crates/red_knot_python_semantic/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ impl<'db> Type<'db> {
10081008
///
10091009
/// [assignable to]: https://typing.readthedocs.io/en/latest/spec/concepts.html#the-assignable-to-or-consistent-subtyping-relation
10101010
pub(crate) fn is_assignable_to(self, db: &'db dyn Db, target: Type<'db>) -> bool {
1011-
if self.is_equivalent_to(db, target) {
1011+
if self.is_gradual_equivalent_to(db, target) {
10121012
return true;
10131013
}
10141014
match (self, target) {

crates/red_knot_python_semantic/src/types/property_tests.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ mod stable {
461461
forall types s, t.
462462
s.is_fully_static(db) && s.is_gradual_equivalent_to(db, t) => s.is_equivalent_to(db, t)
463463
);
464+
465+
// `T` can be assigned to itself.
466+
type_property_test!(
467+
assignable_to_is_reflexive, db,
468+
forall types t. t.is_assignable_to(db, t)
469+
);
464470
}
465471

466472
/// This module contains property tests that currently lead to many false positives.
@@ -475,13 +481,6 @@ mod flaky {
475481

476482
use super::{intersection, union};
477483

478-
// Currently fails due to https://github.com/astral-sh/ruff/issues/14899
479-
// `T` can be assigned to itself.
480-
type_property_test!(
481-
assignable_to_is_reflexive, db,
482-
forall types t. t.is_assignable_to(db, t)
483-
);
484-
485484
// Negating `T` twice is equivalent to `T`.
486485
type_property_test!(
487486
double_negation_is_identity, db,

0 commit comments

Comments
 (0)