Skip to content

Commit

Permalink
Fixed a bug that resulted in a false positive error when a captured v…
Browse files Browse the repository at this point in the history
…ariable within an inner scope (function or lambda) is imported from another module using an import statement that comes after the function or lambda. This addresses #5618. (#5623)

Co-authored-by: Eric Traut <[email protected]>
  • Loading branch information
erictraut and msfterictraut authored Aug 1, 2023
1 parent 66281f5 commit 76ea0f8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/analyzer/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,8 @@ export class Binder extends ParseTreeWalker {
const dataclassesSymbolsOfInterest = ['InitVar'];
const importInfo = AnalyzerNodeInfo.getImportInfo(node.module);

AnalyzerNodeInfo.setFlowNode(node, this._currentFlowNode!);

let resolvedPath = '';
if (importInfo && importInfo.isImportFound && !importInfo.isNativeLib) {
resolvedPath = importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1];
Expand Down Expand Up @@ -1874,6 +1876,8 @@ export class Binder extends ParseTreeWalker {
const importedName = importSymbolNode.name.value;
const nameNode = importSymbolNode.alias || importSymbolNode.name;

AnalyzerNodeInfo.setFlowNode(importSymbolNode, this._currentFlowNode!);

const symbol = this._bindNameToScope(this._currentScope, nameNode);

if (symbol) {
Expand Down Expand Up @@ -2487,6 +2491,8 @@ export class Binder extends ParseTreeWalker {
) {
const firstNamePartValue = node.module.nameParts[0].value;

AnalyzerNodeInfo.setFlowNode(node, this._currentFlowNode!);

// See if there's already a matching alias declaration for this import.
// if so, we'll update it rather than creating a new one. This is required
// to handle cases where multiple import statements target the same
Expand Down
5 changes: 0 additions & 5 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4452,11 +4452,6 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
return true;
}

// Assume alias declarations are also always safe to narrow.
if (decl.type === DeclarationType.Alias) {
return true;
}

const declCodeFlowNode = AnalyzerNodeInfo.getFlowNode(decl.node);
if (!declCodeFlowNode) {
return false;
Expand Down

0 comments on commit 76ea0f8

Please sign in to comment.