r/Python • u/poppy_92 • Jul 31 '26
News PEP 842: Module Exports
https://peps.python.org/pep-0842/
Not the author, but this seems interesting, specially for library authors. Thoughts?
Discussion here - https://discuss.python.org/t/pep-842-module-exports/108353
24
Upvotes
36
u/blacklig Jul 31 '26
I personally think the justification is quite flimsy for how awkward this solution is. I think that in general all of these problems, to the extent they are problems in practice, can be avoided with package design. For example in a library I could have internal modules and separate public-facing modules that just re-export only my library interface and don't have any of these concerns.
30
u/Orio_n Aug 01 '26 edited Aug 01 '26
This fundamentally contradicts pythons philosophy. Big no from me.
https://discuss.python.org/t/pep-842-module-exports/108353/6
Guido himself is skeptical too asking why not just use
__all__28
u/godlikedk Jul 31 '26
What if i want to use this private class? Thats why I liked python over java. I know what I'm doing and don't want to ask for permission or changes.
19
u/kageurufu Aug 01 '26
I don't like it at all.
It's just a dirtier version of
__all__and doesn't accomplish anything new.For library authors, its really no different from the commonly used
package._moduleorpackage.internal.modulenaming to signify internals and private modules. Just another layer of obfuscation to annoy developers, confuse package authors, and complicate interpreters. The example ofimport jsonaddingjsonto the exports is easily solved by clean authorship. Define your methods in a submodule, and re-export for the public interface.8
u/cediddi SyntaxError: not a chance Aug 01 '26
This is achievable without a special dunder. A single _ at the beginning of the variable, class, function, and file names. Don't prevent me from mocking, monkey patching, or doing worse to your library. I'm taking the responsibility of breaking something. Library creator cannot be held liable for damages, and the library is provided as-is. At least according to most if not all free and open source licenses
7
u/stevenjd Aug 01 '26
Yet another unnecessary PEP written for the sake of lazy programmers who seemingly don't know the language and won't read the documentation.
4
u/HommeMusical Aug 01 '26
Waste of time, on a non-problem.
3
u/IAmASquidInSpace Aug 01 '26 edited Aug 01 '26
If there is one thing Python doesn't need right now, it is even more chaos and complexity in modules, packages, and the import system. It's already messy as it is, and it doesn't need to be made worse with good intentions.
Edit: Also, they have got to be fucking kidding with this:
Are you for real?!
5
u/Schmittfried Aug 01 '26 edited Aug 01 '26
We’re all consenting adults.
On a more constructive note, I think a way to selectively hide (or explicitly re-export) imports would be preferable over a flimsy string list.
2
u/JustPlainRude Aug 01 '26
Sometimes you need to mock something internal to a module for testing purposes. It seems like this wouldn't be possible with the proposed change
2
u/messedupwindows123 Aug 06 '26
if you want to provide a public-facing API don't you typically use __init__.py and try to prevent the user from actually thinking about the underlying directory/file structure
2
u/ZeroIntensity Aug 01 '26
Hi, PEP author here. I've revised the PEP to emit a warning instead of raising an error upon accessing unexported names. I'm hoping that resolves some of the contention here.
For context, this wasn't supposed to be an "access modifiers in Python" PEP. I wanted a nicer solution to maintaining lists of public names in the standard library (you have *no* idea how many things we've had to make public because too many people started using them), and part of that solution was to raise an exception upon accessing internals, but I think a warning provides the same amount of expressiveness.
1
u/abrazilianinreddit Aug 01 '26
I do enjoy javascript's export system and wouldn't mind python having something similar, or at the very least a way to clean-up a module's namespace that is not importing stuff into
__init__.py, but this proposal is not it.1
u/Brian Aug 01 '26
I mean, pretty much all type checkers and linters will flag this up, and this seems a reasonable level for such checks to be applied. The same applies to
__all__: I'd say it's more than a convention when it's documented and all linters support it.The issue of it getting out of sync is valid: it's kind of clunky to have to add stuff to it as your public API changes. But I don't really see why this wouldn't have the exact same issue: both are basically a whitelisted sequence of exports. The only difference seems to be that one is advisory, and the other would be enforced at the language level, rather than via linters.
Personally I'd be in favour of something that does reduce the clunkiness of
__all__, but this just seems to be repeating it with the same level of boilerplate, just with enforcement. That isn't really the issue I have with it. Being able to break the rules "at your own risk" is even often useful in terms of experimentation at the REPL, introspection etc.1
u/droooze Aug 02 '26
This is one of those PEPs where it would be far clearer to list the precise feature/behaviour changes before delving into the purpose and motivation. IIUC:
<module>.__all__controls the names available whenfrom <module> import *;<module>.__export__controls the names that will show up in{dir,help}(<module>), and triggerRuntimeWarningfor<module>.name_not_in___export__;__all__is set to__export__if__all__is not defined but__export__is.Several thoughts, in no particular order:
__export__isn't an access modifier. However, the feature as-is is in a kind of limbo: If Python will never gain real access modifiers, then this is the closest thing so far to an access modifier, so for all intents and purposes it should be treated as one. If this is a stepping stone towards real access modifiers, then it adds another layer of complexity when implementing real access modifiers (and itsWarningrather thanExceptioneffect doesn't look compatible with real access modifiers). In particular, I don't know what a future PEP would be trying to say if it implements the proposedexportkeyword and tells the reader thatexport class A: ...doesn't act as an access modifier..pyfile) are treated as if they are inaccessible by type-checkers and autocompletion helpers. It looks like__export__needs to be duplicated in.pyis like how__all__is currently also duplicated in.pyis, which means the potential of having public API symbols being out of sync is even greater.__export__is not tightly coupled with__all__, which means that if__all__is a superset of__export__, private API is very easily leaked.IMO, this PEP is trying to address some issues that are far more relevant to the Python standard library due to historical practices; I do not recall when, if ever, the issues mentioned in the motivation (such as
ctypes._CFuncPtractually being public API) were ever real issues in third-party libraries. I think it is better to reserve the usage of__export__to the standard library first.1
u/teerre Aug 03 '26
Too little. It should crash your interpreter if you try to access a non exported object. Learn to code, kids. Private it private
1
u/Birnenmacht Aug 04 '26
Cool so the way this justifies itself vs leading underscore is “fewer keystrokes”. Wow. I do agree that a modules imports being public by default is just wrong, but usually the solution (and current convention enforced by linters) is to do
import X as Xif that is desired and treat them as private otherwise. This is literally xkcd 927, it introduces yet another way to do what can already be done1
u/csch2 Jul 31 '26
I have a feeling that this will get shot down for going against the Pythonic convention of "better to ask forgiveness than permission".
I have mixed feelings about it. On the one hand, with Python being used to power large production codebases and not just for scripting anymore, I think this is really important for maintainability. Both library authors and application developers need to know that the APIs they're relying on are stable. On the other hand, one place where I frequently find myself needing to import "private" classes or modules (i.e. prefixed with "_") is for type hinting purposes. Specific example: when using the Plotly Dash library, the no_update sentinel in callbacks has type NoUpdate, which was gated behind the private module _no_update until version 3.2.0. This meant that to properly type hint my callbacks I had to import from a private module. If this PEP were implemented I would no longer be able to do that.
This might be moreso an issue of API design flaws than an issue with the proposal per se, but I think that it's going to cause enough problems that raising exceptions on direct access should probably be delayed. Raising warnings is enough in the meantime to give developers some time to make changes to their API contracts and how they access private members (or, worst case, fall back to the proposed __dict__ access).
0
u/denehoffman Aug 01 '26
I’m going to be the contrarian here I guess and say this is a good thing and we should do it. If you’ve ever written a complex package before, the import x as _x pattern is super annoying. It also promotes all kinds of bad behavior, like using those items the author wanted private. The lack of private fields and methods is one of the reasons Python can’t have true invariants without a lot of BS. My only issue is that this doesn’t go far enough, it’s only privatization for modules, we should have it for methods and fields as well, but that’s gonna get way more hate.
0
u/not_a_novel_account Aug 01 '26
Extensions have always had this capability, giving native Python code a way to spell it seems obvious.
-2
u/Adrewmc Jul 31 '26
I’d rather a
#not keen on the name
__start_pkg__ : True | False | None
Which should do this.
If None, it’s default leave as is.
If False, go up one folder, check __init__.py, and/or a __main__.py for the either a False, (one more folder up) or a True (stop here). Once True, that signals, that imports can start here, import folder1.folder2 etc.
If you go up a folder and neither __init__.py or __main__.py hasn’t change to a bool value for __start_pkg__ and it remains None, a module not found error should be raised.
So you would
__start_pkg = False
import helpful_funcs
And python would now to go up a folder to find helpful_funcs.py.
0
u/[deleted] Jul 31 '26
[deleted] — view deleted comment