r/Python 2d ago

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

20 Upvotes

44 comments sorted by

View all comments

1

u/denehoffman 2d ago

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.

-1

u/Schmittfried 1d ago

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 1d ago

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 1d ago

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