pdoc icon indicating copy to clipboard operation
pdoc copied to clipboard

explicit re-export from a private module should make the member public

Open DetachHead opened this issue 1 year ago • 2 comments

Problem Description

in python typing, an import alias with the same name is considered an explicit re-export:

# foo/_private_module.py
foo = 1
# foo/public_module.py
from _private_module import foo as foo # explicitly re-exporting foo in a public module

see https://peps.python.org/pep-0484/#stub-files:

Modules and variables imported into the stub are not considered exported from the stub unless the import uses the import ... as ... form or the equivalent from ... import ... as ... form. (UPDATE: To clarify, the intention here is that only names imported using the form X as X will be exported, i.e. the name before and after as must be the same.)

however pdoc does not recognize this and does not include the name in the docs, when it is intended to be public.

Steps to reproduce the behavior:

  1. create a private and public module with the code above
  2. run pdoc foo

System Information

pdoc: 14.4.0
Python: 3.12.0
Platform: Windows-10-10.0.19045-SP0

DetachHead avatar Feb 02 '24 23:02 DetachHead

This feels more like a hack than anything else, but given that it's in PEP 484 I'd be happy to merge a PR that adds support for it. Fwiw, a strict reading of the spec seems to imply this only applies to stub files of I'm not mistaken...

As a workaround, I'd recommend to just declare __all__.

mhils avatar Feb 02 '24 23:02 mhils

This feels more like a hack than anything else

tell me about it, that's python typing for you lol

a strict reading of the spec seems to imply this only applies to stub files of I'm not mistaken...

yeah i don't know why but for some reason most of the documentation and discussions around python typing seems to imply that you'd only ever use types in stubs and not actual source code. but the two main type checkers support this explicit re-export "syntax" outside of stubs (mypy and pyright) so i think it's safe to assume this behavior is intended to work in regular source code too

As a workaround, I'd recommend to just declare __all__.

thanks. though i'm not really a fan of __all__ since it would mean i have to add everything else in my module to it, and i prefer declaring whether something is public/private on its definition rather than in a separate section of the module. so the workaround i went with was to just import it and assign it to another variable:

# foo/public_module.py
from _private_module import foo as _foo

foo = _foo
"""the downside is that i have to move the docstring here tho"""

DetachHead avatar Feb 03 '24 00:02 DetachHead