-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stubgen: include __all__ in output #16356
Changes from 4 commits
f03f12c
b545d45
59e4415
cc37d9e
f8d5c55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -587,20 +587,26 @@ __all__ = [] + ['f'] | |
def f(): ... | ||
def g(): ... | ||
[out] | ||
__all__ = ['f'] | ||
|
||
def f() -> None: ... | ||
|
||
[case testOmitDefsNotInAll_semanal] | ||
__all__ = ['f'] | ||
def f(): ... | ||
def g(): ... | ||
[out] | ||
__all__ = ['f'] | ||
|
||
def f() -> None: ... | ||
|
||
[case testOmitDefsNotInAll_inspect] | ||
__all__ = [] + ['f'] | ||
def f(): ... | ||
def g(): ... | ||
[out] | ||
__all__ = ['f'] | ||
|
||
def f(): ... | ||
|
||
[case testVarDefsNotInAll_import] | ||
|
@@ -610,6 +616,8 @@ x = 1 | |
y = 1 | ||
def g(): ... | ||
[out] | ||
__all__ = ['f', 'g'] | ||
|
||
def f() -> None: ... | ||
def g() -> None: ... | ||
|
||
|
@@ -620,6 +628,8 @@ x = 1 | |
y = 1 | ||
def g(): ... | ||
[out] | ||
__all__ = ['f', 'g'] | ||
|
||
def f(): ... | ||
def g(): ... | ||
|
||
|
@@ -628,6 +638,8 @@ __all__ = [] + ['f'] | |
def f(): ... | ||
class A: ... | ||
[out] | ||
__all__ = ['f'] | ||
|
||
def f() -> None: ... | ||
|
||
class A: ... | ||
|
@@ -637,6 +649,8 @@ __all__ = [] + ['f'] | |
def f(): ... | ||
class A: ... | ||
[out] | ||
__all__ = ['f'] | ||
|
||
def f(): ... | ||
|
||
class A: ... | ||
|
@@ -647,6 +661,8 @@ class A: | |
x = 1 | ||
def f(self): ... | ||
[out] | ||
__all__ = ['A'] | ||
|
||
class A: | ||
x: int | ||
def f(self) -> None: ... | ||
|
@@ -684,6 +700,8 @@ x = 1 | |
[out] | ||
from re import match as match, sub as sub | ||
|
||
__all__ = ['match', 'sub', 'x'] | ||
|
||
x: int | ||
|
||
[case testExportModule_import] | ||
|
@@ -694,6 +712,8 @@ y = 2 | |
[out] | ||
import re as re | ||
|
||
__all__ = ['re', 'x'] | ||
|
||
x: int | ||
|
||
[case testExportModule2_import] | ||
|
@@ -704,6 +724,8 @@ y = 2 | |
[out] | ||
import re as re | ||
|
||
__all__ = ['re', 'x'] | ||
|
||
x: int | ||
|
||
[case testExportModuleAs_import] | ||
|
@@ -714,6 +736,8 @@ y = 2 | |
[out] | ||
import re as rex | ||
|
||
__all__ = ['rex', 'x'] | ||
|
||
x: int | ||
|
||
[case testExportModuleInPackage_import] | ||
|
@@ -722,13 +746,17 @@ __all__ = ['p'] | |
[out] | ||
import urllib.parse as p | ||
|
||
__all__ = ['p'] | ||
|
||
[case testExportPackageOfAModule_import] | ||
import urllib.parse | ||
__all__ = ['urllib'] | ||
|
||
[out] | ||
import urllib as urllib | ||
|
||
__all__ = ['urllib'] | ||
Comment on lines
751
to
+758
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kinda cool that stubgen has sophisticated enough inference to do this. Didn't realise that it had this capability! |
||
|
||
[case testRelativeImportAll] | ||
from .x import * | ||
[out] | ||
|
@@ -741,6 +769,8 @@ x = 1 | |
class C: | ||
def g(self): ... | ||
[out] | ||
__all__ = ['f', 'x', 'C'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about this. Stubtest would emit an error here if it encountered this, since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Omitting it also feels wrong though, and should generate a stubtest error (not sure if it currently does). Maybe the best option is to just emit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stubtest doesn't complain if We're in a bit of a catch-22 here if the runtime
I think you're right that emitting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, and both stubtest and pyright are correct in those errors. Ultimately if |
||
|
||
def f() -> None: ... | ||
|
||
x: int | ||
|
@@ -758,6 +788,8 @@ x = 1 | |
class C: | ||
def g(self): ... | ||
[out] | ||
__all__ = ['f', 'x', 'C'] | ||
|
||
def f(): ... | ||
|
||
x: int | ||
|
@@ -2343,6 +2375,8 @@ else: | |
[out] | ||
import cookielib as cookielib | ||
|
||
__all__ = ['cookielib'] | ||
|
||
[case testCannotCalculateMRO_semanal] | ||
class X: pass | ||
|
||
|
@@ -2788,6 +2822,8 @@ class A: pass | |
# p/__init__.pyi | ||
from p.a import A | ||
|
||
__all__ = ['a'] | ||
|
||
a: A | ||
# p/a.pyi | ||
class A: ... | ||
|
@@ -2961,7 +2997,9 @@ __uri__ = '' | |
__version__ = '' | ||
|
||
[out] | ||
from m import __version__ as __version__ | ||
from m import __about__ as __about__, __author__ as __author__, __version__ as __version__ | ||
|
||
__all__ = ['__about__', '__author__', '__version__'] | ||
|
||
[case testAttrsClass_semanal] | ||
import attrs | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I prefer stubtest's heuristic here, which IIRC is something like:
__all__
:a. If a name is included in
__all__
, treat it as publicb. Else, treat the name as private
__all__
:a. If the name doesn't start with
_
, treat it as publicb. Else, if the name is a dunder, treat it as public unless it's one of the ones in the "excluded dunders" list
c. Else, treat it as private
The key difference is that (IIUC) the current algorithm you have here would treat
bar
as a public name in this situation, whereas stubtest would treat it as private:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we changed this algorithm in the way I'm suggesting, though, it would have an impact on existing tests such as
testIncludeClassNotInAll_inspect
. It would be a fairly big behavioural change. Probably best to leave it to another PR!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly we should change this, but that feels out of scope for this PR; the logic is mostly as it was before, except that names in
__all__
are now not treated as private. Your suggested change might be right, but it's better handled as an independent change.