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

[spec] Clarification: Are symbols not listed in __all__ ever considered public? #1829

Open
randolf-scholz opened this issue Aug 1, 2024 · 1 comment
Labels
topic: documentation Documentation-related issues and PRs

Comments

@randolf-scholz
Copy link

According to https://typing.readthedocs.io/en/latest/guides/libraries.html#library-interface-public-and-private-symbols:

A module can expose an __all__ symbol at the module level that provides a list of names that are considered part of the interface. This overrides all other rules above, allowing imported symbols or symbols whose names begin with an underscore to be included in the interface.

Does this mean that if __all__ is present: ① A symbol is public if and only if it is listed in __all__ or ② If a symbol is listed in __all__, it is public, but things not listed in __all__ can still be considered public as well?

For example:

# module.py
__all__ = ["identity"]

from typing import TypeVar

T = TypeVar("T")

def identity(x: T) -> T:
    return x

Are TypeVar and T considered public members of module.py? If so, what is the suggested way to exclude them? T could be renamed to _T, but is one supposed to do from typing import TypeVar as _TypeVar, if one wants TypeVar to not be considered a public member of module?

@randolf-scholz randolf-scholz added the topic: documentation Documentation-related issues and PRs label Aug 1, 2024
@erictraut
Copy link
Collaborator

First, the document you're citing applies to type stubs and "py.typed" libraries. It doesn't define rules for local modules or modules in libraries that are not "py.typed".

The __all__ list defines the symbols that are imported via a wildcard import. All such symbols are treated as public in a type stub or a "py.typed" module. Symbols that are not listed within __all__ are not imported via a wildcard import, and they may be considered public or private according to the other rules listed in the document.

In the example you provided, assuming that module.py is part of a "py.typed" package, T and identity would be considered public. TypeVar is private because it does not use a redundant form of import (i.e. from typing import TypeVar as TypeVar).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: documentation Documentation-related issues and PRs
Projects
None yet
Development

No branches or pull requests

2 participants