mirror of
https://github.com/microsoft/pyright.git
synced 2024-08-17 03:40:44 +03:00
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:
parent
2fcdf253ea
commit
5e5e55449e
@ -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 library’s 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.
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user