@@ -26,10 +26,9 @@ use crate::types::function::FunctionType;
2626use crate :: types:: generics:: { GenericContext , typing_self, walk_generic_context} ;
2727use crate :: types:: infer:: nearest_enclosing_class;
2828use crate :: types:: {
29- ApplyTypeMappingVisitor , BindingContext , BoundTypeVarInstance , ClassLiteral ,
30- FindLegacyTypeVarsVisitor , HasRelationToVisitor , IsEquivalentVisitor , KnownClass ,
31- MaterializationKind , NormalizedVisitor , TypeMapping , TypeRelation , VarianceInferable ,
32- todo_type,
29+ ApplyTypeMappingVisitor , BoundTypeVarInstance , ClassLiteral , FindLegacyTypeVarsVisitor ,
30+ HasRelationToVisitor , IsEquivalentVisitor , KnownClass , MaterializationKind , NormalizedVisitor ,
31+ TypeMapping , TypeRelation , VarianceInferable , todo_type,
3332} ;
3433use crate :: { Db , FxOrderSet } ;
3534use ruff_python_ast:: { self as ast, name:: Name } ;
@@ -415,9 +414,7 @@ impl<'db> Signature<'db> {
415414 let plain_return_ty = definition_expression_type ( db, definition, returns. as_ref ( ) )
416415 . apply_type_mapping (
417416 db,
418- & TypeMapping :: MarkTypeVarsInferable ( Some ( BindingContext :: Definition (
419- definition,
420- ) ) ) ,
417+ & TypeMapping :: MarkTypeVarsInferable ( Some ( definition. into ( ) ) ) ,
421418 ) ;
422419 if function_node. is_async && !is_generator {
423420 KnownClass :: CoroutineType
@@ -1256,8 +1253,18 @@ impl<'db> Parameters<'db> {
12561253 let class = nearest_enclosing_class ( db, index, scope_id) . unwrap ( ) ;
12571254
12581255 Some (
1259- typing_self ( db, scope_id, typevar_binding_context, class, & Type :: TypeVar )
1260- . expect ( "We should always find the surrounding class for an implicit self: Self annotation" ) ,
1256+ // It looks like unnecessary work here that we create the implicit Self
1257+ // annotation using non-inferable typevars and then immediately apply
1258+ // `MarkTypeVarsInferable` to it. However, this is currently necessary to
1259+ // ensure that implicit-Self and explicit Self annotations are both treated
1260+ // the same. Marking type vars inferable will cause reification of lazy
1261+ // typevar defaults/bounds/constraints; this needs to happen for both
1262+ // implicit and explicit Self so they remain the "same" typevar.
1263+ typing_self ( db, scope_id, typevar_binding_context, class, & Type :: NonInferableTypeVar )
1264+ . expect ( "We should always find the surrounding class for an implicit self: Self annotation" ) . apply_type_mapping (
1265+ db,
1266+ & TypeMapping :: MarkTypeVarsInferable ( typevar_binding_context. map ( Into :: into) ) ,
1267+ )
12611268 )
12621269 } else {
12631270 // For methods of non-generic classes that are not otherwise generic (e.g. return `Self` or
@@ -1680,9 +1687,7 @@ impl<'db> Parameter<'db> {
16801687 annotated_type : parameter. annotation ( ) . map ( |annotation| {
16811688 definition_expression_type ( db, definition, annotation) . apply_type_mapping (
16821689 db,
1683- & TypeMapping :: MarkTypeVarsInferable ( Some ( BindingContext :: Definition (
1684- definition,
1685- ) ) ) ,
1690+ & TypeMapping :: MarkTypeVarsInferable ( Some ( definition. into ( ) ) ) ,
16861691 )
16871692 } ) ,
16881693 kind,
0 commit comments