Skip to content

TYP: Redundant imports to define the public API #43664

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

Closed
twoertwein opened this issue Sep 19, 2021 · 0 comments · Fixed by #43695
Closed

TYP: Redundant imports to define the public API #43664

twoertwein opened this issue Sep 19, 2021 · 0 comments · Fixed by #43695
Labels
Typing type annotations, mypy/pyright type checking
Milestone

Comments

@twoertwein
Copy link
Member

twoertwein commented Sep 19, 2021

The following is not standardized in a PEP but is generally agreed on by multiple type checkers and is mentioned in the scope of stub files in PEP484. See pytorch/pytorch#47027 (comment) for more pointers. Pytorch (in init.py) and numpy (in init.pyi) are two of the projects that already try to use redundant imports.

Is your feature request related to a problem?

When pandas becomes a py.typed library (#28142), static type checks need to know what is part of the public API and what isn't. PEP 484 outlines that imported modules are only part of the public API, if they follow the pattern of "import X as X" (for stub files). Pyright implemented this not only for stub files but also for "normal" python code (see https://github.com/microsoft/pyright/blob/main/docs/typed-libraries.md#library-interface).
Edit: mypy also complains about that when implicit_reexport=false

Describe the solution you'd like

Use redundant imports to signal what is part of the public API.

While this is not standardized for normal python code, pandas has/will have many .pyi files for its cython code (which is subject to PEP 484). It is probably more consistent to use redundant imports in the entire codebase, not only in .pyi files.

API breaking implications

Until pandas is a py.typed library, it would be good to have a clear statement what takes precedence to define the public API (in case there are mismatches between the documentation, import statements, or type annotations).

Additional context

When creating an empty py.typed file in the pandas directory and using pyright to validate the following code:

import pandas as pd

pd.DataFrame()

error: "DataFrame" is not exported from module "pandas" (reportPrivateImportUsage)

@twoertwein twoertwein added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 19, 2021
@simonjayhawkins simonjayhawkins added Typing type annotations, mypy/pyright type checking and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 20, 2021
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Sep 20, 2021
@jreback jreback modified the milestones: Contributions Welcome, 1.4 Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants