[red-knot] Ensure a gradual type can always be assigned to itself (#15675)

This commit is contained in:
Alex Waygood 2025-01-22 16:01:13 +00:00 committed by GitHub
parent 3235cd8019
commit b4877f1661
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 13 deletions

View file

@ -1008,7 +1008,7 @@ impl<'db> Type<'db> {
///
/// [assignable to]: https://typing.readthedocs.io/en/latest/spec/concepts.html#the-assignable-to-or-consistent-subtyping-relation
pub(crate) fn is_assignable_to(self, db: &'db dyn Db, target: Type<'db>) -> bool {
if self.is_equivalent_to(db, target) {
if self.is_gradual_equivalent_to(db, target) {
return true;
}
match (self, target) {

View file

@ -461,6 +461,12 @@ mod stable {
forall types s, t.
s.is_fully_static(db) && s.is_gradual_equivalent_to(db, t) => s.is_equivalent_to(db, t)
);
// `T` can be assigned to itself.
type_property_test!(
assignable_to_is_reflexive, db,
forall types t. t.is_assignable_to(db, t)
);
}
/// This module contains property tests that currently lead to many false positives.
@ -475,13 +481,6 @@ mod flaky {
use super::{intersection, union};
// Currently fails due to https://github.com/astral-sh/ruff/issues/14899
// `T` can be assigned to itself.
type_property_test!(
assignable_to_is_reflexive, db,
forall types t. t.is_assignable_to(db, t)
);
// Negating `T` twice is equivalent to `T`.
type_property_test!(
double_negation_is_identity, db,