Skip to content

Commit c65bd20

Browse files
committed
cleanup
1 parent e7528bc commit c65bd20

File tree

4 files changed

+17
-24
lines changed

4 files changed

+17
-24
lines changed

crates/ty_python_semantic/resources/mdtest/protocols.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ from ty_extensions import is_equivalent_to
985985
static_assert(is_equivalent_to(UniversalSet, object))
986986
```
987987

988-
and that therefore `Any` is a subtype of `UniversalSet` (in general, `Any` can _only_ ever be a
988+
and that therefore `Any` is a subtype of `UniversalSet` (in general, `Any` can *only* ever be a
989989
subtype of `object` and types that are equivalent to `object`):
990990

991991
```py

crates/ty_python_semantic/src/types.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,9 +1613,10 @@ impl<'db> Type<'db> {
16131613
callable.has_relation_to_impl(db, target, relation, visitor)
16141614
}),
16151615

1616-
(Type::ProtocolInstance(left), Type::ProtocolInstance(right)) => {
1617-
left.has_relation_to_impl(db, right, relation, visitor)
1618-
}
1616+
(Type::ProtocolInstance(left), Type::ProtocolInstance(right)) => left
1617+
.interface(db)
1618+
.extends_interface_of(db, right.interface(db), relation, visitor),
1619+
16191620
// A protocol instance can never be a subtype of a nominal type, with the *sole* exception of `object`.
16201621
(Type::ProtocolInstance(_), _) => C::unsatisfiable(db),
16211622
(_, Type::ProtocolInstance(protocol)) => {

crates/ty_python_semantic/src/types/instance.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,12 @@ impl<'db> ProtocolInstanceType<'db> {
482482
}
483483
}
484484

485+
/// Return `true` if this protocol is a supertype of `object`.
486+
///
487+
/// This indicates that the protocol represents the same set of possible runtime objects
488+
/// as `object` (since `object` is the universal set of *all* possible runtime objects!).
489+
/// Such a protocol is therefore an equivalent type to `object`, which would in fact be
490+
/// normalised to `object`.
485491
pub(super) fn is_equivalent_to_object(self, db: &'db dyn Db) -> bool {
486492
#[salsa::tracked(cycle_fn=recover, cycle_initial=initial, heap_size=ruff_memory_usage::heap_size)]
487493
fn inner<'db>(db: &'db dyn Db, protocol: ProtocolInstanceType<'db>, _: ()) -> bool {
@@ -539,22 +545,6 @@ impl<'db> ProtocolInstanceType<'db> {
539545
}
540546
}
541547

542-
/// Return `true` if this protocol type has the given type relation to the protocol `other`.
543-
///
544-
/// TODO: consider the types of the members as well as their existence
545-
pub(super) fn has_relation_to_impl<C: Constraints<'db>>(
546-
self,
547-
db: &'db dyn Db,
548-
other: Self,
549-
_relation: TypeRelation,
550-
visitor: &HasRelationToVisitor<'db, C>,
551-
) -> C {
552-
other
553-
.inner
554-
.interface(db)
555-
.is_sub_interface_of(db, self.inner.interface(db), visitor)
556-
}
557-
558548
/// Return `true` if this protocol type is equivalent to the protocol `other`.
559549
///
560550
/// TODO: consider the types of the members as well as their existence

crates/ty_python_semantic/src/types/protocol_class.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,21 @@ impl<'db> ProtocolInterface<'db> {
230230
.unwrap_or_else(|| Type::object(db).member(db, name))
231231
}
232232

233-
/// Return `true` if if all members on `self` are also members of `other`.
233+
/// Return `true` if `self` extends the interface of `other`, i.e.,
234+
/// all members on `other` are also members of `self`.
234235
///
235236
/// TODO: this method should consider the types of the members as well as their names.
236-
pub(super) fn is_sub_interface_of<C: Constraints<'db>>(
237+
pub(super) fn extends_interface_of<C: Constraints<'db>>(
237238
self,
238239
db: &'db dyn Db,
239240
other: Self,
241+
_relation: TypeRelation,
240242
_visitor: &HasRelationToVisitor<'db, C>,
241243
) -> C {
242244
// TODO: This could just return a bool as written, but this form is what will be needed to
243245
// combine the constraints when we do assignability checks on each member.
244-
self.inner(db).keys().when_all(db, |member_name| {
245-
C::from_bool(db, other.inner(db).contains_key(member_name))
246+
other.inner(db).keys().when_all(db, |member_name| {
247+
C::from_bool(db, self.inner(db).contains_key(member_name))
246248
})
247249
}
248250

0 commit comments

Comments
 (0)