Skip to content

MAINT: Ensure that re-exported sub-modules are properly annotated#18618

Merged
charris merged 3 commits intonumpy:mainfrom
BvB93:modules
Mar 16, 2021
Merged

MAINT: Ensure that re-exported sub-modules are properly annotated#18618
charris merged 3 commits intonumpy:mainfrom
BvB93:modules

Conversation

@BvB93
Copy link
Copy Markdown
Member

@BvB93 BvB93 commented Mar 15, 2021

closes #18601.

Sub-modules, such as np.linalg, must be explicitly imported in the main namespaces' stub file if one wants
to access the sub-module via a getattr operation, i.e. so that one can directly use np.linalg.norm rather
than import numpy.linalg; np.linalg.norm.

While this was taken care of in the main numpy namespace, the relevant annotations were missing for others
such as np.lib.*. This PR fixes aforementioned issue.

Examples

The behavior prior to this PR:

>>> import numpy as np

>>> x = np.arange(6)
>>> out = np.lib.stride_tricks.sliding_window_view(x, 3)

Mypy output:

test.pyi:4:7: error: Module has no attribute "stride_tricks"  [attr-defined]

Bas van Beek added 2 commits March 15, 2021 14:50
With `as_strided`, and the newly introduced `sliding_window_view` function, there are currently 2 public objects that can:
a. Only be imported from a private module
b. Are publicly documented to-be imported from aforementioned module

Both observations are problematic and in need of rectification.
This commit therefore moves `np.lib.stride_tricks` to the `PUBLIC_MODULES` list.
Ensures that type checkers will allow the likes of:

>>> import numpy as np
>>> out = np.lib.stride_tricks.sliding_window_view(...)
"lib.mixins",
"lib.recfunctions",
"lib.scimath",
"lib.stride_tricks",
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With as_strided, and the newly introduced sliding_window_view function, there are currently 2
public objects in lib.stride_tricks that can:
a. Only be imported from a private module.
b. Are publicly documented to-be imported from aforementioned module.

Because of this lib.stride_tricks has now been (formally) moved to the list of public modules.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgommers Thoughts about adding lib.stride_tricks to the list of public modules?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me.

… list

Aforementioned module was accidently marked as public
@charris charris merged commit 8e58398 into numpy:main Mar 16, 2021
@charris
Copy link
Copy Markdown
Member

charris commented Mar 16, 2021

Thanks Bas.

@BvB93 BvB93 removed the 09 - Backport-Candidate PRs tagged should be backported label Mar 17, 2021
@BvB93 BvB93 deleted the modules branch March 17, 2021 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Annotations don't import np.lib.*

3 participants