Changed re-export logic for type stub and py.typed modules to honor the clarification that was recently added to PEP 484. Previously, any import that used an "as" clause was considered to be re-exported. Now, symbols are re-exported only if the "as" clause is redundant (i.e. it is of the form import A as A or from A import X as X).

This commit is contained in:
Eric Traut 2020-11-01 08:54:09 -08:00
parent 2fcdf253ea
commit 5e5e55449e
2 changed files with 11 additions and 5 deletions

View File

@ -32,7 +32,7 @@ If a “py.typed” module is present, a type checker will treat all modules wit
Each module exposes a set of symbols. Some of these symbols are considered “private” — implementation details that are not part of the librarys interface. Type checkers like pyright use the following rules to determine which symbols are visible outside of the package.
* Symbols whose names begin with an underscore (but are not dunder names) are considered private.
* Imported symbols are considered private by default. If they use the “import ... as ...”, “from ... import ... as ...” or “from ... import *” forms, they are not private (unless their names begin with an underscore).
* Imported symbols are considered private by default. If they use the “import A as A” (a redundant module alias), “from A import X as X” (a redundant symbol alias) or “from A import *” forms, they are not private (unless their names begin with an underscore).
* A module can expose an `__all__` symbol at the module level that provides a list of names that are considered part of the interface. This overrides all other rules above, allowing imported symbols or symbols whose names begin with an underscore to be included in the interface.
* Local variables within a function (including nested functions) are always considered private.

View File

@ -1303,11 +1303,17 @@ export class Binder extends ParseTreeWalker {
}
const symbol = this._bindNameToScope(this._currentScope, symbolName);
if (symbol && !node.alias) {
if (
symbol &&
(!node.alias ||
node.module.nameParts.length !== 1 ||
node.module.nameParts[0].value !== node.alias.value)
) {
if (this._fileInfo.isStubFile) {
// PEP 484 indicates that imported symbols should not be
// considered "reexported" from a type stub file unless
// they are imported using the "as" form.
// they are imported using the "as" form and the aliased
// name is entirely redundant.
symbol.setIsExternallyHidden();
} else if (this._fileInfo.isInPyTypedPackage && this._currentScope.type === ScopeType.Module) {
this._potentialPrivateSymbols.set(symbolName, symbol);
@ -1469,11 +1475,11 @@ export class Binder extends ParseTreeWalker {
const symbol = this._bindNameToScope(this._currentScope, nameNode.value);
if (symbol) {
if (!importSymbolNode.alias) {
if (!importSymbolNode.alias || importSymbolNode.alias.value !== importSymbolNode.name.value) {
if (this._fileInfo.isStubFile) {
// PEP 484 indicates that imported symbols should not be
// considered "reexported" from a type stub file unless
// they are imported using the "as" form.
// they are imported using the "as" form using a redundant form.
symbol.setIsExternallyHidden();
} else if (this._fileInfo.isInPyTypedPackage && this._currentScope.type === ScopeType.Module) {
this._potentialPrivateSymbols.set(nameNode.value, symbol);