From d34d9ccc27319031707b6a25e851097eb46c7cd4 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Tue, 11 Jun 2024 13:27:02 -0700 Subject: [PATCH] Fixed regression in the handling of certain `__init__` methods with an explicit type annotation for `self`. --- packages/pyright-internal/src/analyzer/typeEvaluator.ts | 9 ++++++++- packages/pyright-internal/src/analyzer/types.ts | 5 +---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator.ts b/packages/pyright-internal/src/analyzer/typeEvaluator.ts index f66c58f2c..1789b7cc0 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator.ts @@ -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 { diff --git a/packages/pyright-internal/src/analyzer/types.ts b/packages/pyright-internal/src/analyzer/types.ts index c86c99e24..9086f0e23 100644 --- a/packages/pyright-internal/src/analyzer/types.ts +++ b/packages/pyright-internal/src/analyzer/types.ts @@ -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; }