Fixed bug that causes a false positive reportPrivateUsage error when a global or nonlocal binding is used within a class to access an outer-scoped variable. This addresses #7884.

This commit is contained in:
Eric Traut 2024-05-10 20:57:27 -07:00
parent e654729d96
commit e045e1c0a2
3 changed files with 15 additions and 1 deletions

View File

@ -1428,6 +1428,7 @@ export class Binder extends ParseTreeWalker {
range: convertTextRangeToRange(node.name, this._fileInfo.lines),
moduleName: this._fileInfo.moduleName,
isInExceptSuite: this._isInExceptSuite,
isExplicitBinding: this._currentScope.getBindingType(node.name.value) !== undefined,
};
symbol.addDeclaration(declaration);
}
@ -2322,6 +2323,7 @@ export class Binder extends ParseTreeWalker {
range: convertTextRangeToRange(node.target, this._fileInfo.lines),
moduleName: this._fileInfo.moduleName,
isInExceptSuite: this._isInExceptSuite,
isExplicitBinding: this._currentScope.getBindingType(node.target.value) !== undefined,
};
symbol.addDeclaration(declaration);
}
@ -2392,6 +2394,7 @@ export class Binder extends ParseTreeWalker {
range: convertTextRangeToRange(slotNameNode, this._fileInfo.lines),
moduleName: this._fileInfo.moduleName,
isInExceptSuite: this._isInExceptSuite,
isExplicitBinding: this._currentScope.getBindingType(slotName) !== undefined,
};
symbol.addDeclaration(declaration);
}
@ -2441,6 +2444,7 @@ export class Binder extends ParseTreeWalker {
range: convertTextRangeToRange(target, this._fileInfo.lines),
moduleName: this._fileInfo.moduleName,
isInExceptSuite: this._isInExceptSuite,
isExplicitBinding: this._currentScope.getBindingType(target.value) !== undefined,
};
symbol.addDeclaration(declaration);
}
@ -3607,6 +3611,7 @@ export class Binder extends ParseTreeWalker {
moduleName: this._fileInfo.moduleName,
isInExceptSuite: this._isInExceptSuite,
docString: this._getVariableDocString(target),
isExplicitBinding: this._currentScope.getBindingType(name.value) !== undefined,
};
symbolWithScope.symbol.addDeclaration(declaration);
}
@ -3748,6 +3753,7 @@ export class Binder extends ParseTreeWalker {
moduleName: this._fileInfo.moduleName,
isInExceptSuite: this._isInExceptSuite,
docString: this._getVariableDocString(target),
isExplicitBinding: this._currentScope.getBindingType(name.value) !== undefined,
};
symbolWithScope.symbol.addDeclaration(declaration);

View File

@ -4528,7 +4528,12 @@ export class Checker extends ParseTreeWalker {
return;
}
const declarations = this._evaluator.getDeclarationsForNameNode(node);
// Get the declarations for this name node, but filter out
// any variable declarations that are bound using nonlocal
// or global explicit bindings.
const declarations = this._evaluator
.getDeclarationsForNameNode(node)
?.filter((decl) => decl.type !== DeclarationType.Variable || !decl.isExplicitBinding);
let primaryDeclaration =
declarations && declarations.length > 0 ? declarations[declarations.length - 1] : undefined;

View File

@ -175,6 +175,9 @@ export interface VariableDeclaration extends DeclarationBase {
// If set, indicates an alternative node to use to determine the type of the variable.
alternativeTypeNode?: ExpressionNode;
// Is the declaration an assignment through an explicit nonlocal or global binding?
isExplicitBinding?: boolean;
}
// Alias declarations are used for imports. They are resolved