@@ -3214,6 +3214,22 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
32143214 setSymbolAccessed(fileInfo, outerScopeSymbol.symbol, node);
32153215 }
32163216 }
3217+
3218+ if (!codeFlowTypeResult.type && symbolWithScope.isBeyondExecutionScope) {
3219+ const outerScopeTypeResult = getCodeFlowTypeForCapturedVariable(
3220+ node,
3221+ symbolWithScope,
3222+ effectiveType
3223+ );
3224+
3225+ if (outerScopeTypeResult?.type) {
3226+ type = outerScopeTypeResult.type;
3227+ }
3228+
3229+ if (outerScopeTypeResult?.isIncomplete) {
3230+ isIncomplete = true;
3231+ }
3232+ }
32173233 }
32183234
32193235 // Detect, report, and fill in missing type arguments if appropriate.
@@ -3325,6 +3341,84 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
33253341 return { type, node, isIncomplete };
33263342 }
33273343
3344+ // Handles the case where a variable or parameter is defined in an outer
3345+ // scope and captured by an inner scope (either a function or a lambda).
3346+ function getCodeFlowTypeForCapturedVariable(
3347+ node: NameNode,
3348+ symbolWithScope: SymbolWithScope,
3349+ effectiveType: Type
3350+ ): FlowNodeTypeResult | undefined {
3351+ // This function applies only to variables and parameters, not to other
3352+ // types of symbols.
3353+ if (
3354+ !symbolWithScope.symbol
3355+ .getDeclarations()
3356+ .every((decl) => decl.type === DeclarationType.Variable || decl.type === DeclarationType.Parameter)
3357+ ) {
3358+ return undefined;
3359+ }
3360+
3361+ // If the symbol is a variable captured by an inner function
3362+ // or lambda, see if we can infer the type from the outer scope.
3363+ const scopeHierarchy = ScopeUtils.getScopeHierarchy(node, symbolWithScope.scope);
3364+
3365+ // Handle the case where all of the nested scopes are functions,
3366+ // lambdas and modules. Don't allow other types of scopes.
3367+ if (
3368+ scopeHierarchy &&
3369+ scopeHierarchy.length >= 2 &&
3370+ scopeHierarchy.every((s) => s.type === ScopeType.Function || s.type === ScopeType.Module)
3371+ ) {
3372+ // Find the parse node associated with the scope that is just inside of the
3373+ // scope that declares the captured variable.
3374+ const innerScopeNode = ScopeUtils.findTopNodeInScope(node, scopeHierarchy[scopeHierarchy.length - 2]);
3375+ if (
3376+ innerScopeNode &&
3377+ (innerScopeNode.nodeType === ParseNodeType.Function || innerScopeNode.nodeType === ParseNodeType.Lambda)
3378+ ) {
3379+ const innerScopeCodeFlowNode = AnalyzerNodeInfo.getFlowNode(innerScopeNode);
3380+ if (innerScopeCodeFlowNode) {
3381+ // See if any of the assignments of the symbol are reachable
3382+ // from this node. If so, we cannot apply any narrowing because
3383+ // the type could change after the capture.
3384+ if (
3385+ symbolWithScope.symbol.getDeclarations().every((decl) => {
3386+ // Parameter declarations always start life at the beginning
3387+ // of the execution scope, so they are always safe to narrow.
3388+ if (decl.type === DeclarationType.Parameter) {
3389+ return true;
3390+ }
3391+
3392+ const declCodeFlowNode = AnalyzerNodeInfo.getFlowNode(decl.node);
3393+ if (!declCodeFlowNode) {
3394+ return false;
3395+ }
3396+
3397+ // Functions and lambdas do not create a new flow node, so it's
3398+ // possible that they share the flow node of the declaration. In this
3399+ // case, the declaration must come before, so it's safe.
3400+ if (declCodeFlowNode === innerScopeCodeFlowNode) {
3401+ return true;
3402+ }
3403+
3404+ return !isFlowNodeReachable(declCodeFlowNode, innerScopeCodeFlowNode);
3405+ })
3406+ ) {
3407+ return getFlowTypeOfReference(
3408+ node,
3409+ symbolWithScope.symbol.id,
3410+ effectiveType,
3411+ /* isInitialTypeIncomplete */ false,
3412+ innerScopeNode
3413+ );
3414+ }
3415+ }
3416+ }
3417+ }
3418+
3419+ return undefined;
3420+ }
3421+
33283422 // Validates that a TypeVar is valid in this context. If so, it clones it
33293423 // and provides a scope ID defined by its containing scope (class, function
33303424 // or type alias). If not, it emits errors indicating why the TypeVar
@@ -14756,16 +14850,20 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
1475614850
1475714851 // Attempts to determine the type of the reference expression at the
1475814852 // point in the code. If the code flow analysis has nothing to say
14759- // about that expression, it return undefined.
14853+ // about that expression, it return undefined. Normally flow analysis
14854+ // starts from the reference node, but startNode can be specified to
14855+ // override this in a few special cases (functions and lambdas) to
14856+ // support analysis of captured variables.
1476014857 function getFlowTypeOfReference(
1476114858 reference: CodeFlowReferenceExpressionNode,
1476214859 targetSymbolId: number,
1476314860 initialType: Type | undefined,
14764- isInitialTypeIncomplete: boolean
14861+ isInitialTypeIncomplete: boolean,
14862+ startNode?: FunctionNode | LambdaNode
1476514863 ): FlowNodeTypeResult {
1476614864 // See if this execution scope requires code flow for this reference expression.
1476714865 const referenceKey = createKeyForReference(reference);
14768- const executionNode = ParseTreeUtils.getExecutionScopeNode(reference);
14866+ const executionNode = ParseTreeUtils.getExecutionScopeNode(startNode?.parent ?? reference);
1476914867 const codeFlowExpressions = AnalyzerNodeInfo.getCodeFlowExpressions(executionNode);
1477014868
1477114869 if (!codeFlowExpressions || !codeFlowExpressions.has(referenceKey)) {
@@ -14785,7 +14883,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
1478514883 analyzer = getCodeFlowAnalyzerForNode(executionNode.id);
1478614884 }
1478714885
14788- const flowNode = AnalyzerNodeInfo.getFlowNode(reference);
14886+ const flowNode = AnalyzerNodeInfo.getFlowNode(startNode ?? reference);
1478914887 if (flowNode === undefined) {
1479014888 return { type: undefined, usedOuterScopeAlias: false, isIncomplete: false };
1479114889 }
0 commit comments