Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DetachHead opened this issue Feb 2, 2024 · 2 comments
Open

Comments

@DetachHead
Copy link

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 DetachHead added the bug label Feb 2, 2024
@mhils mhils added enhancement and removed bug labels Feb 2, 2024
@mhils
Copy link
Member

mhils commented Feb 2, 2024

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__.

@DetachHead
Copy link
Author

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"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants