Repository navigation
Added missing check for access to a module member that is meant to be… - #2279
Eric Traut (erictraut) wants to merge 2 commits into
Conversation
|
I've fixed the bug for case C2, and that case now generates an error too. I think the changes in this PR are correct, but I worry that they're going to generate a lot of new errors for pyright/pylance users who have type checking enabled. I needed to make a few hundred changes to my team's code base to fix local type stubs that were not correctly re-exporting imported symbols. This is going to expose a lot of similar bugs in public type stubs and py.typed libraries. I wonder if we should move these new errors to a new diagnostic rule so it can be disabled separately from other general type errors. Or I could report them using the existing |
|
I'd be locking them behind the private usage check (you can use it, but a stricter mode may yell at you); I was actually thinking it was that way already. |
… private to that module — e.g. a private-name type alias, a private-named function in a ".py" file within a py.typed library, or a non-re-exported import within a type stub.
6072bef to
6cbe7be
Compare
| @@ -252,14 +256,18 @@ export class Binder extends ParseTreeWalker { | |||
|
|
|||
| // Use the __all__ list to determine whether any potential private | |||
| // symbols should be made externally invisible. | |||
There was a problem hiding this comment.
update comments?
| node.module.nameParts[0].value !== node.alias.value) | ||
| ) { | ||
| if (this._fileInfo.isStubFile) { | ||
| if (this._fileInfo.isStubFile || this._fileInfo.isInPyTypedPackage) { |
There was a problem hiding this comment.
so, now even if import is not written as "import x as x", if x is in __ all __ , it is not marked as hidden?
previously, stub didn't care about __ all __ to mark symbol as hidden or not. it only cared about 'import x as x" form.
There was a problem hiding this comment.
Yeah, the challenge I ran into is that there are symbols in some stubs that begin with _ that are meant to be exported. The __all__ mechanism was the only way I could think of supporting it. This came up frequently in the hand-rolled stubs that I maintain for my team's code base, but some of these _ symbols show up even in typeshed stubs. For example, consider the function os._exit.
There was a problem hiding this comment.
this will impact auto-import behavior. but I guess that's fine. (since auto import decide what to include in auto import using the symbol. externallyHidden flag.) I think we can ignore the impact on auto import.
There was a problem hiding this comment.
Yeah, I consider the current behavior for auto-import to be a bug. It suggests completions that will result in runtime exceptions. This change addresses that bug.
There was a problem hiding this comment.
I just posted a question in typeshed about symbols within a type stub that begin with an underscore. https://github.com/python/typeshed/issues/6018
| this._currentScope.type === ScopeType.Builtin | ||
| ) { | ||
| if ( | ||
| !importSymbolNode.alias || |
There was a problem hiding this comment.
same here.
| // Don't include private-named symbols in the builtin scope. | ||
| symbol.setIsExternallyHidden(); | ||
| } else { | ||
| this._potentialPrivateSymbols.set(name, symbol); |
There was a problem hiding this comment.
now symbol in stub can affected by __ all __ when it was always hidden before.
There was a problem hiding this comment.
Yes, this is intended. See my comment above.
… private to that module — e.g. a private-name type alias, a private-named function in a ".py" file within a py.typed library, or a non-re-exported import within a type stub.