diff --git a/crates/ty_python_semantic/src/types/generics.rs b/crates/ty_python_semantic/src/types/generics.rs index 12f40c6e21..f3d71e2dd4 100644 --- a/crates/ty_python_semantic/src/types/generics.rs +++ b/crates/ty_python_semantic/src/types/generics.rs @@ -17,8 +17,8 @@ use crate::types::tuple::{TupleSpec, TupleType, walk_tuple_type}; use crate::types::{ ApplyTypeMappingVisitor, BoundTypeVarInstance, FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind, NormalizedVisitor, - Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarVariance, - UnionType, binding_type, declaration_type, + Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind, + TypeVarVariance, UnionType, binding_type, declaration_type, }; use crate::{Db, FxOrderSet}; @@ -401,6 +401,17 @@ impl<'db> GenericContext<'db> { fn heap_size((variables,): &(FxOrderSet>,)) -> usize { ruff_memory_usage::order_set_heap_size(variables) } + + /// Returns a version of this generic context with the `Self` typevar removed. + pub(crate) fn bind_self(self, db: &'db dyn Db) -> Self { + Self::from_typevar_instances( + db, + self.variables(db) + .iter() + .filter(|typevar| typevar.typevar(db).kind(db) != TypeVarKind::TypingSelf) + .copied(), + ) + } } #[derive(Debug, Copy, Clone, PartialEq, Eq)] diff --git a/crates/ty_python_semantic/src/types/signatures.rs b/crates/ty_python_semantic/src/types/signatures.rs index 1159b0556d..999dea6227 100644 --- a/crates/ty_python_semantic/src/types/signatures.rs +++ b/crates/ty_python_semantic/src/types/signatures.rs @@ -522,7 +522,7 @@ impl<'db> Signature<'db> { return_ty.map(|ty| ty.apply_type_mapping(db, &TypeMapping::BindSelf(self_type))); } Self { - generic_context: self.generic_context, + generic_context: self.generic_context.map(|context| context.bind_self(db)), inherited_generic_context: self.inherited_generic_context, definition: self.definition, parameters,