Skip to content

Commit

Permalink
Fixed regression in the handling of certain __init__ methods with a…
Browse files Browse the repository at this point in the history
…n explicit type annotation for `self`.
  • Loading branch information
erictraut committed Jun 11, 2024
1 parent add2193 commit d34d9cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 8 additions & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions packages/pyright-internal/src/analyzer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d34d9cc

Please sign in to comment.