Fixed regression in the handling of certain __init__ methods with an explicit type annotation for self.

This commit is contained in:
Eric Traut 2024-06-11 13:27:02 -07:00
parent add2193101
commit d34d9ccc27
2 changed files with 9 additions and 5 deletions

View File

@ -27006,7 +27006,14 @@ export function createTypeEvaluator(
const specializedFunction = applySolvedTypeVars(memberType, typeVarContext) as FunctionType;
return FunctionType.clone(specializedFunction, stripFirstParam, baseType, getTypeVarScopeId(baseType));
// If this is a constructor method, provide the base type's TypeVar scope ID
// so any TypeVars in this type can be solved.
let baseTypeTypeVarScopeId: TypeVarScopeId | undefined;
if (FunctionType.isConstructorMethod(specializedFunction) || specializedFunction.details.name === '__init__') {
baseTypeTypeVarScopeId = getTypeVarScopeId(baseType);
}
return FunctionType.clone(specializedFunction, stripFirstParam, baseType, baseTypeTypeVarScopeId);
}
function isFinalVariable(symbol: Symbol): boolean {

View File

@ -1658,10 +1658,7 @@ export namespace FunctionType {
}
newFunction.inferredReturnType = type.inferredReturnType;
if (newFunction.preBoundFlags & FunctionTypeFlags.ConstructorMethod) {
newFunction.boundTypeVarScopeId = boundTypeVarScopeId ?? type.boundTypeVarScopeId;
}
newFunction.boundTypeVarScopeId = boundTypeVarScopeId ?? type.boundTypeVarScopeId;
return newFunction;
}