diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index ec022df292..c93194ac68 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -52,7 +52,7 @@ use crate::types::function::{ DataclassTransformerParams, FunctionSpans, FunctionType, KnownFunction, }; use crate::types::generics::{ - GenericContext, PartialSpecialization, Specialization, bind_typevar, get_self_type, + GenericContext, PartialSpecialization, Specialization, bind_typevar, typing_self, walk_generic_context, }; pub use crate::types::ide_support::{ @@ -5717,7 +5717,7 @@ impl<'db> Type<'db> { ], }); }; - let self_type = get_self_type(db, scope_id, typevar_binding_context, class); + let self_type = typing_self(db, scope_id, typevar_binding_context, class); Ok(self_type.map(Type::NonInferableTypeVar).unwrap_or(*self)) } SpecialFormType::TypeAlias => Ok(Type::Dynamic(DynamicType::TodoTypeAlias)), diff --git a/crates/ty_python_semantic/src/types/generics.rs b/crates/ty_python_semantic/src/types/generics.rs index 2192c18672..ecf0a5dd53 100644 --- a/crates/ty_python_semantic/src/types/generics.rs +++ b/crates/ty_python_semantic/src/types/generics.rs @@ -101,7 +101,8 @@ pub(crate) fn bind_typevar<'db>( }) } -pub(crate) fn get_self_type<'db>( +/// Create a `typing.Self` type variable for a given class. +pub(crate) fn typing_self<'db>( db: &'db dyn Db, scope_id: ScopeId, typevar_binding_context: Option>, @@ -110,11 +111,10 @@ pub(crate) fn get_self_type<'db>( let module = parsed_module(db, scope_id.file(db)).load(db); let index = semantic_index(db, scope_id.file(db)); - let class_definition = class.definition(db); let typevar = TypeVarInstance::new( db, ast::name::Name::new_static("Self"), - Some(class_definition), + Some(class.definition(db)), Some( TypeVarBoundOrConstraints::UpperBound(Type::instance( db, @@ -129,6 +129,7 @@ pub(crate) fn get_self_type<'db>( None, TypeVarKind::TypingSelf, ); + bind_typevar( db, &module, diff --git a/crates/ty_python_semantic/src/types/signatures.rs b/crates/ty_python_semantic/src/types/signatures.rs index e60a7e320c..7a63387ec0 100644 --- a/crates/ty_python_semantic/src/types/signatures.rs +++ b/crates/ty_python_semantic/src/types/signatures.rs @@ -22,7 +22,7 @@ use super::{ use crate::semantic_index::definition::Definition; use crate::types::constraints::{ConstraintSet, IteratorConstraintsExtension}; use crate::types::function::FunctionType; -use crate::types::generics::{GenericContext, get_self_type, walk_generic_context}; +use crate::types::generics::{GenericContext, typing_self, walk_generic_context}; use crate::types::infer::nearest_enclosing_class; use crate::types::{ ApplyTypeMappingVisitor, BindingContext, BoundTypeVarInstance, ClassType, @@ -1293,7 +1293,7 @@ impl<'db> Parameters<'db> { let index = semantic_index(db, scope_id.file(db)); let class = nearest_enclosing_class(db, index, scope_id).unwrap(); Type::NonInferableTypeVar( - get_self_type(db, scope_id, typevar_binding_context, class).unwrap(), + typing_self(db, scope_id, typevar_binding_context, class).unwrap(), ) .apply_type_mapping( db,