PEP 843 is ready for discussion. It adds a small statement, from x export y [as z], that does what from x import y [as z] does and also appends the name to __all__ in the same statement.
# spam/init.py
from ._internal.core export PublicAPI
from ._internal.widgets export Widget as PublicWidget
The problem it targets is narrow: it simplifies “hub modules”. Hub modules are modules whose job is gathering names from internal submodules and re-exposing them. Today that means writing every name twice, once in the import and again as a string in __all__, with nothing keeping the two in sync but a careful reviewer. This statement folds them into one.
Full text: PEP 843 – Export Statement for DRY Re-exports | peps.python.org
PR: https://github.com/python/peps/pull/5078
Relationship to PEP 842 and PEP 844
This PEP sits alongside PEPs 842 and 844. In short, PEP 843 just focuses on re-exports. Here’s a quick comparison:
| PEP 842 | PEP 843 | PEP 844 | |
|---|---|---|---|
| Mechanism | New syntax (five forms of the export keyword) |
New syntax (one form of the export keyword) |
New builtins; no grammar change |
| Scope | Definitions and re-exports | Re-exports | Definitions |
| Export list | A new __export__ list, and __all__ |
__all__ |
__all__ |
| Runtime enforcement | Yes: ExportError on access to non-exported attributes |
None | None |
Open issue
One question the PEP leaves open: should export * require the source module to define its own __all__, or fall back to import *'s behavior? Feedback on that specifically is welcome.
Thanks
This went from an idea to a PEP because of a lot of people’s time. Peter Bierma, PEP 842’s author, is sponsoring this PEP despite it being a rival proposal to his own, and pushed on getting the header fields, CODEOWNERS entry, and process details right. Barry Warsaw, Jelle Zijlstra, Hugo van Kemenade, Guido van Rossum, and Éric Araujo reviewed the draft, catching many errors. And in the earlier Ideas thread, Brénainn Woodsend pushed back on whether NumPy actually needs __all__ at all, Paul Moore was skeptical of how common this pattern really is, and James Webber and Xitop objected to the word “export” itself.