Skip to content

Added missing check for access to a module member that is meant to be… - #2279

Closed
Eric Traut (erictraut) wants to merge 2 commits into
mainfrom
erictr/private_export
Closed

Eric Traut (erictraut) wants to merge 2 commits into
mainfrom
erictr/private_export

Conversation

@erictraut

Copy link
Copy Markdown
Collaborator

… 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.

@erictraut

Eric Traut (erictraut) commented Sep 9, 2021 •

Copy link
Copy Markdown
Collaborator Author

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 reportPrivateUsage diagnostic rule, which is off by default in basic type checking mode. Jake Bailey (@jakebailey), thoughts?

@jakebailey

Copy link
Copy Markdown
Member

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.
@@ -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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update comments?

node.module.nameParts[0].value !== node.alias.value)
) {
if (this._fileInfo.isStubFile) {
if (this._fileInfo.isStubFile || this._fileInfo.isInPyTypedPackage) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@heejaechang Heejae Chang (heejaechang) Sep 10, 2021 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ||

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

// Don't include private-named symbols in the builtin scope.
symbol.setIsExternallyHidden();
} else {
this._potentialPrivateSymbols.set(name, symbol);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now symbol in stub can affected by __ all __ when it was always hidden before.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is intended. See my comment above.

@erictraut
Eric Traut (erictraut) deleted the erictr/private_export branch December 19, 2021 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants