I tested pyright/pylance (see below) and my understanding is that that type checker does not use __all__ to define public API, but it does model runtime behavior of star imports and __all__ correctly.
# ./base.py
__all__ = ["f"]
def f() -> None: pass
def g() -> None: pass
# ./hub.py
from .base import *
# ./__main__.py
from . import base
from . import hub
base.f # ok
base.g # ok
hub.f # ok
hub.g # unknown attribute
A lying base.pyi stub-file that incorrectly implements g and either has no __all__ or puts "g" into it breaks this behavior by overwriting the base.py, but predictably and as expected.
That’s an interesting statement. Let me expand on that: “public” is first and foremost intent (“should a 3rd party make use of this”). There is little the runtime does differently for public vs. internal names. In fact, the only difference is wildcard imports in absence of __all__. The spec says that wildcard imports import all public names, and in fact leading underscores are correctly excluded.
Considering imported names, the runtime does not record whether a name is imported or declared in the module. Therefore, wildcard imports cannot special-handle imported names (i.e. considering them internal).
In other words, the only place where the runtime does something differently (wildcard imports) has technically no information in imported names, which means that imported names are public is not a free choice, but a technical limitation.
So technically yes this statement is true:
But given the limited behavior impact and technical limitations, I would give that statement much less weight that what one may deduce from reading it in isolation.
Your reasoning is completely correct, but I dispute your conclusion. If we’re planning on documenting and/or clarifying the current behaviour in the language spec, then runtime behaviour takes priority (I’d even be willing to argue it’s all that matters).
Typing behaviour, and the typing spec, is a separate matter. Although it seems to me that having consistent behaviour is better, that’s not the current reality so we need to decide whether to document how things are now, or make a change. Given that the runtime can’t change, making typing behave like runtime is the only possible change here.
We could, of course, document all of this in a different document (or section of the docs), essentially a style and usage guide. But even then, what works in the runtime is pretty important. Even if we are only documenting how people can express their intent independent of the language semantics (or lack of them), being clear about what’s the language behaviour and what’s purely convention (which may or may not be respected by 3rd party tools) is still important.
If the plan is to introduce new language constructs/features, all bets are off. We can decide what semantics we want, as well as how it’s indicated in the language. But that means tackling the whole question of what people want “public” to mean - which brings us back to the problem that people have widely differing opinions on that matter.
Honestly, I wish typing had never added that rule that from x import a as a means something specific. It feels like a very dangerous precedent to set that two ways of writing the same thing (as far as the runtime is concerned) can mean significantly different things to external tools.
I would welcome a discussion on whether and how we could make a change. Even if typing is optional and a separate topic, both worlds overlap in practice if people use typing. IMHO we should not burden developers with handling conflicting statements.
Is that set in stone? All our options have disadvantages (keeping the inconsistency, changing runtime, changing typing). Can we weigh the respective costs and benefits before excluding options?
As a user reading this I find all this discussion surrounding the “private”, “public” terminology confusing and there seems to be little consensus on what they mean in practice. These public/private terms already carry too many connotations within and outside Python. Meanwhile the actual runtime behavior of _underscore and __all__ is clear. Perhaps we should refer to them by what they actually do rather than their connotations:
__all__is the explicit wildcard-exported symbols
In the absence of __all__, the implicit wildcard-exported symbols are defined as the names in the global namespace, that don’t begin with an underscore
The sequence of wildcard-exported symbols are evaluated at the time at the time of star-import, and might or might not be statically determinable. This is because __all__ and the module globals can be populated by any means, including global statements inside functions, dynamically modifying __dict__/globals(), and so on. The runtime makes no distinction of how the module globals are populated.
Anything not in the global namespace, such as a method in a class namespace, is irrelevant.
Type checkers and style guides can of course tighten things up to encourage readable code and discourage surprises; IDEs and type-checkers don’t seem to support dynamically determining wildcard-exported symbols well, and a dynamically-varying list of exported symbols will be quite surprising to most users. But I don’t think it’s the remit of the language reference (much less the runtime) to legislate on these issues, their job is to define minimum behavior which they already do well.
I don’t think there’s been a serious case arguing that the existing runtime behavior is causing the confusion. Changing the runtime behavior will break a lot of existing code for no good benefit, and that’s an automatic no-go.
It’s the conflating terminology that we use to describe the runtime behavior in the language reference, in the typing spec, the expected API and so on that’s really the trouble. As a general rule, we can always expect the typing spec and style guides to be tighter than what the runtime does or what language reference states, but that they are using the same terminology to describe different things is unfortunate.
Has anyone considered changing that? import X could be compiled to the current IMPORT_NAME + STORE_NAME, plus an additional bit that does something like __imported_names__.append('X').
Fleshing out the details (how to initialize __imported_names__ to [] before the first import, how to ignore import statements inside function or class scopes, how to make imports of submodules also add to the __imported_names__ of their parent package) are left as an exercise for the reader.
This might already be too far into “proposing solutions” territory as opposed to “determining status quo” territory, but anyway, one question I’d have about that scheme is: what happens with
There would have to be an override mechanism of some kind (e.g. from somewhere export thing PEP?), not just for this, but also to support the hub module pattern. But I agree that discussing possible implementation issues is a digression. My point was to disagree with
We could make wildcard imports special-case imported names, if we wanted to. (It would not be backward-compatible, and I’m not entirely sure that doing this would be worth the pain.)
Sorry, what I meant was that the runtime has no way of distinguishing from x import a and from x import a as a. So the typing behaviour simply isn’t expressible at runtime.
We could of course change the semantics of from x import a as a, but that’s a much bigger change and would likely involve formalising some new runtime mechanism for marking a name as “public”.
We’re getting beyond “determining the status quo” though, so I suggest we defer any discussion of harmonising the runtime and typing behaviours to a thread (or probably multiple threads, given how this debate is currently going ) that’s focused on a specific proposal.
I think this is correct, but I think it is important to give also the big picture. So it should explicitly state additionally that the typing spec is orthogonal to the language spec and that it differs regarding the import → private interpretation and the corresponding typing idiom import x as x.
There’s also the REPL’s completion, only suggesting names without underscore prefixes unless an underscore has already been typed. (It ignores __all__.)
Explicit language semantics take precedence over conventions, though conventions may still inform the intended meaning. In this specific case, it is essentially saying, “I am intentionally exposing this underscored member.” If that happens by mistake, tests should help catch it.
I have a question. Is the behaviour of dir and help, for example as in the REPL, part of the scope of this thread?
I ask because I use the REPL a lot to explore new to me modules and to remind myself about familiar modules. This request for help LSP for Python? in this forum is related. (LSP is Language Server Protocol - I had to look it up.)
If the object is a module object, the list contains the names of the module’s attributes.
There is also a note:
Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases.
So basically, dir takes no account of “public” vs “private”, except in terms of any undocumented behaviour it might have around choosing “interesting” names.
The help function says nothing more than “a help page is printed on the console”. So again, no documented behaviour that isn relevant to this discussion.
Any discussion about what dir or help “should” do is out of scope here - this thread is for confirming the status quo as documented, not for designing improvements.
It’s not outside the realm of possibility that dir() could grow a keyword-only argument that would enable more specific meaning to “interesting”, e.g. dir(public=True) or some such.