unpythonic icon indicating copy to clipboard operation
unpythonic copied to clipboard

Fix shadowed submodule names

Open Technologicat opened this issue 6 years ago • 3 comments
trafficstars

Some constructs such as unpythonic.env.env and unpythonic.llist.llist have the same name as the submodule they live in. This hides the submodule from the dict of the top-level namespace of the unpythonic module. This behavior should be considered a bug.

To improve discoverability (e.g. groups of functionality; submodule top-level docstrings), the offending modules should be renamed to avoid becoming shadowed.

It's better to rename the modules than the symbols, since the symbols are certainly used by client code, but the submodule names necessarily aren't. It's not wrong to import directly from a submodule of unpythonic, if that feels more readable at the use site, but it's not mandatory, either, since the top-level __init__ imports all public symbols into the top-level unpythonic namespace. (This behavior is part of the public API, and is not going away.)

This is a breaking change, so we'll wait until the next major version before we do it.

CAUTION: Renaming the modules will break unpickling for instances that were pickled using the old version. [1] says that pickle can save and restore class instances transparently, however the class definition must be importable and live in the same module as when the object was stored. (emphasis mine)

Technologicat avatar Oct 29 '19 13:10 Technologicat

This is also an issue because of The submodules are added to the package namespace trap. So if anything, anywhere, imports one of the submodules, the name in the top-level unpythonic namespace will reset to point to the submodule.

Technologicat avatar Oct 31 '19 13:10 Technologicat

The trap is also documented in the Python reference, §5.4.2.

Also on SO.

Technologicat avatar Nov 12 '19 23:11 Technologicat

New names, list in progress:

  • unpythonic.itunpythonic.itertools, in analogy with standard itertools
  • unpythonic.fununpythonic.functools, similarly
  • unpythonic.llistunpythonic.linkedlists

Maybe:

  • unpythonic.ecunpythonic.escapes
  • unpythonic.fupunpythonic.fpupdate, in analogy with unpythonic.fploop

Technologicat avatar Dec 04 '19 22:12 Technologicat