diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index c93194ac68..100f30b194 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -5359,7 +5359,7 @@ impl<'db> Type<'db> { // It is important that identity_specialization specializes the class with // _inferable_ typevars, so that our specialization inference logic will // try to find a specialization for them. - Type::from(class.identity_specialization(db, Type::TypeVar)), + Type::from(class.identity_specialization(db, &Type::TypeVar)), ), _ => (None, None, self), }, @@ -5717,8 +5717,15 @@ impl<'db> Type<'db> { ], }); }; - let self_type = typing_self(db, scope_id, typevar_binding_context, class); - Ok(self_type.map(Type::NonInferableTypeVar).unwrap_or(*self)) + + Ok(typing_self( + db, + scope_id, + typevar_binding_context, + class, + &Type::NonInferableTypeVar, + ) + .unwrap_or(*self)) } SpecialFormType::TypeAlias => Ok(Type::Dynamic(DynamicType::TodoTypeAlias)), SpecialFormType::TypedDict => Err(InvalidTypeExpressionError { diff --git a/crates/ty_python_semantic/src/types/class.rs b/crates/ty_python_semantic/src/types/class.rs index 23d0136190..65d4d3d9f5 100644 --- a/crates/ty_python_semantic/src/types/class.rs +++ b/crates/ty_python_semantic/src/types/class.rs @@ -1532,7 +1532,7 @@ impl<'db> ClassLiteral<'db> { pub(crate) fn identity_specialization( self, db: &'db dyn Db, - typevar_to_type: impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>, + typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>, ) -> ClassType<'db> { self.apply_specialization(db, |generic_context| { generic_context.identity_specialization(db, typevar_to_type) diff --git a/crates/ty_python_semantic/src/types/generics.rs b/crates/ty_python_semantic/src/types/generics.rs index ecf0a5dd53..f61ba1e999 100644 --- a/crates/ty_python_semantic/src/types/generics.rs +++ b/crates/ty_python_semantic/src/types/generics.rs @@ -107,7 +107,8 @@ pub(crate) fn typing_self<'db>( scope_id: ScopeId, typevar_binding_context: Option>, class: ClassLiteral<'db>, -) -> Option> { + typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>, +) -> Option> { let module = parsed_module(db, scope_id.file(db)).load(db); let index = semantic_index(db, scope_id.file(db)); @@ -118,7 +119,7 @@ pub(crate) fn typing_self<'db>( Some( TypeVarBoundOrConstraints::UpperBound(Type::instance( db, - class.identity_specialization(db, Type::NonInferableTypeVar), + class.identity_specialization(db, typevar_to_type), )) .into(), ), @@ -138,6 +139,7 @@ pub(crate) fn typing_self<'db>( typevar_binding_context, typevar, ) + .map(typevar_to_type) } /// A list of formal type variables for a generic function, class, or type alias. @@ -333,7 +335,7 @@ impl<'db> GenericContext<'db> { pub(crate) fn identity_specialization( self, db: &'db dyn Db, - typevar_to_type: impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>, + typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>, ) -> Specialization<'db> { let types = self .variables(db) diff --git a/crates/ty_python_semantic/src/types/signatures.rs b/crates/ty_python_semantic/src/types/signatures.rs index 55bc2e3c9c..8e391bbd0a 100644 --- a/crates/ty_python_semantic/src/types/signatures.rs +++ b/crates/ty_python_semantic/src/types/signatures.rs @@ -1255,15 +1255,8 @@ impl<'db> Parameters<'db> { let class = nearest_enclosing_class(db, index, scope_id).unwrap(); Some( - Type::NonInferableTypeVar( - typing_self(db, scope_id, typevar_binding_context, class).unwrap(), - ) - .apply_type_mapping( - db, - &TypeMapping::MarkTypeVarsInferable(Some(BindingContext::Definition( - definition, - ))), - ), + typing_self(db, scope_id, typevar_binding_context, class, &Type::TypeVar) + .unwrap(), ) } else { // For methods of non-generic classes that are not otherwise generic (e.g. return `Self` or