r/Python • • 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

44 comments sorted by

View all comments

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.

5

u/stevenjd Aug 01 '26 edited Aug 01 '26

the import x as _x pattern is super annoying.

Then don't do it. Callers should never treat imports in your module as public variables unless documented as public. Linters should flag the use of x as as error and let that be the end of it.

It also promotes all kinds of bad behavior, like using those items the author wanted private.

As an author, I don't get a say in what the caller does or doesn't do with my code, and that is exactly how it should be.

All I can do is say clearly that:

  • If you do this, you will break the code and then reject any bug reports from people who did this.
  • There are no guarantees for these so-called "private" objects, including imported modules. Use them at your own risk. I can and will change them without notice.

"Consenting adults" applies.

2

u/denehoffman Aug 01 '26 edited Aug 01 '26

Every time Python programmers talk about language guarantees, it’s always stuff like “the linter will handle it” and “the type checker will handle it” and then it slowly becomes “well you’ll get an error at runtime”. I’m sure there’s a way I could manually write behavior like this with enough hacky code, but the fact that lots of other languages have privatization should be an indication that it’s not intrinsically evil. It also ensures that users who have been using these private functions and types submit actual feature requests to promote those things to documented features rather than keeping them as little secret hacks for cool kids who read the source code.

Edit: it looks like in the current design, you aren’t prevented from accessing these module attributes. If you’ve ever really want to, you can add them to the export dict manually. The point is to make it an intentional thing you have to do to break the API rather than a thing a new user could do simply by importing something that is importable.

5

u/HommeMusical Aug 01 '26

import x as _x pattern is super annoying.

There's no good reason to do that. Use __all__, if you even care, but why do you care?!

like using those items the author wanted private.

That's not very Pythonic.

The lack of private fields and methods

Is by design and it's a feature that most people like.

is one of the reasons Python can’t have true invariants without a lot of BS.

C++ is just over there!

0

u/denehoffman Aug 01 '26

I agree that it will probably be rejected, I’m just saying that a lack of private fields, methods, and APIs is not really as cool as everyone thinks. But also if you look at the PEP, the actual proposed implementation is not really new syntax, you could almost do this in current pure python. Also, I think you can say it’s not pythonic all you want, that’s only because it’s not currently in Python. Lazy imports were also not pythonic and were actively discouraged by linters and now they’re new syntax.

3

u/HommeMusical Aug 01 '26

Lazy imports were also not pythoni

"Pythonic" doesn't mean "a thing that exists in Python" - it means adhering to the spirit of PEP 20, The Zen of Python.

Lazy imports solve several constant real-world problems with a single, perfectly clear word, lazy.

  • speeding up startup times
  • handling unavoidable circular references
  • optional dependencies

All previous code still works. Code using the variable lazy still works. It's simple, it's explicit, it's readable, elegant, and practical. It's Pythonic in almost every way.


But PEP 842 doesn't solve any real-world problem. "Someone might do this stupid thing, and I should prevent it."

We already have two ways to do this.

If you want to explicitly list what members you are exposing, use __all__, it really isn't that hard.

Or don't use __all__, but name things starting with _, and if they use one of them, or they re-use one of your imports, well, maybe they have a good reason to.


If you want to specify an API, you have type stubs.

If you just want to specify which symbols to export, you have __all__, though that isn't enforced.

Or mark variables with _, though that isn't enforced either. Most of us like it that it's not enforced.

Adding a fourth and more draconian way to do this, a little obscure, and just for imports, is a non-starter.

Adding an API with type stubs, that's really what you should be doing! Annoying fiddling with imports is picayune.

2

u/nekokattt Aug 02 '26

Do lazy imports also cover:

  • There should be one way to do things
  • Special cases are not special enough to break the rules
  • Explicit is better than implicit (i.e. try except around an import)

0

u/denehoffman Aug 02 '26

Lazy imports don’t actually solve a problem either, they’re a convenience to importing something later on in a program. The syntax doesn’t actually do anything you couldn’t do previously in Python, it just makes it easier to write. I personally like it. And pythonic can mean a lot of different things, but in terms of the Zen, and this was brought up when that PEP was discussed, adding a new keyword to do behavior that already exists is not quite Zen (there should be one—and preferably only one—obvious way to do it). You could even argue that the last line, “namespaces are one honking great idea”, means that namespaces are important and properly defining a module interface is equally important.

The PEP explicitly says you can opt out of that behavior. The problem it solves is also quite clear, `__all__` doesn’t apply to `__dir__`, and you shouldn’t implicitly export libraries through the wrong namespace (this is opinion of course, but I agree with it, it’s okay if you don’t!). And it’s hardly draconian, you admit it’s barely different from `__all__`! You can’t have it both ways. It’s just an exception-validated import scheme.

+1 for reminding me the word picayune exists, that’s a great one honestly!

I doubt this PEP will pass. Based on the discussion, the author seems to want to turn it into a warning message, which is somehow worse I think. So oh well, no big deal.

-1

u/Schmittfried Aug 01 '26

I agree, but a string list of names goes out of sync super fast. I think a more elegant solution would be a way to import modules without transitively exporting them.

import hidden numpy  # not visible to packages importing this one

1

u/denehoffman Aug 01 '26

I think new syntax is definitely not the way to go here. The @public decorator would be cool to have with some enforcement.

1

u/Schmittfried Aug 01 '26

As long as it’s not a string list, it’s opt-in and backwards-compatible I’m all for it.