-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
MAINT: Hide internals of np.lib
to only show submodules
#18447
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
Conversation
This commit creates a new module to "conveniently" import all symbols into the main namespace, while hiding them in `np.lib`.
4621823
to
91bfcfa
Compare
Hmm, I guess I may have thought a bit too complicated here. I tried to circumvent the way we currently use |
Thanks for this @seberg!
I'd prefer this option, just an alphabetically ordered explicit list seems like the cleanest solution - and easiest to modify for contributors that aren't familiar with how this works exactly. |
Well, I think we need to clarify what we want |
There's maybe a cleaner alternative here:
If we can deprecate it, that would be great. |
In this case, what would happen with the submodules in |
That's wrong to begin with. Functions must be public API in exactly one place. So we do what we have to for backwards compat with the "hiding trick", but there's no conflict here. |
I am just confused by the file/directory renaming. It would be strange to me (though not necessarily any better/worse than the current situation) to have a |
There should not be any of those, that does not make sense. Only things that end up in the main namespace need moving. |
If the goal here is to hide all re-exported functions from the |
# through `__getattr__` (see `__dir__`). | ||
__all__ = {'emath', 'Arrayterator', 'tracemalloc_domain', 'NumpyVersion'} | ||
|
||
__lib_submodules_ = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, is there a reason why this isn't just a normal single-underscored variable (e.g. _lib_submodules
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I guess I had started with dunders, and then thought that dunders are sometimes special, so no dunder might be better, I suppose it looked very private, like in classes with name mangling :).
I have to think about the approach a bit more. I am not certain that actually renaming files helps much, this achieves almost the same thing.
@BvB93 yeah, if we do this, the pyi
file should just be removed, only leaving the things that current are there also. If that doesn't include modules, that only leaves __all__
and 3 others symbols or so, I guess. I am almost surprised we have a a pyi
at all...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the pyi
file: there have been issues in the past with certain circular imports crashing mypy (#17316), issues that were alleviated by adding the likes numpy/core/__init__.pyi
. Because of this I'm a bit reluctant to remove the pyi
file here in its entirety, as I'm afraid at might break something.
cc: @kmaehashi |
Ultimately, I think that would be true if we had done a better job at keeping the main namespace clean. But, given that we haven't, a sensible topic-based organization of the Ideally, we'd move many functions from the main namespace to Side question: do we need the |
That would be a major change. I'm not saying I disagree with the principle (it does make sense), but then we'd have to do it consistently - e.g. add
We definitely need The "core array library" and recommended/essential parts of numpy are:
All the other |
That all makes sense. Would you be okay with the bigger change if Sebastian were to embark on that mission? |
I think so. But note that it may have consequences for downstream libraries that aim for compatibility with numpy - it would be good to make a proposal on the mailing list. The current broadcasting/stride-tricks questions are completely hypothetical I think, so I'd suggest finishing this PR first before attempting to mess with |
# Note that it does _not_ hide symbols from `np.lib.__dict__()`, to do | ||
# that, we would have to delete them from the locals here and hide them | ||
# into `__getattr__`. But this seems sufficient, e.g. for tab completion. | ||
return __all__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return __all__ | |
return sorted(globals().keys() | set(__all__)) |
More or less equivalent to sorted(super().__dir__() | set(__all__))
in this case,
but module-level __getattr__
lacks a self
argument.
The import thing here is that you're missing quite a few types.ModuleType
-based attributes if you just return __all__
here (and even the __all__
attribute itself).
Going to close this for now, since I am not going to make it a priority to finish it off, and whatever we do may look a bit different anyway. |
Thanks for the attempt, Sebastian. Here's the current list of submodules:
For something we can do right now, without much controversy, how about we override
The other submodules will still be accessible, but they won't be in lists of auto-completions etc. to confuse users. |
This commit creates a new module to "conveniently" import all
symbols into the main namespace, while hiding them in
np.lib
.What this does and does not:
np.lib.<tab>
is now very leannp.lib.<symbol>
will keep working for all symbols (but we can deprecate that)__all__
to define what ends up in the main namespace. That is a bit orthogonal to hidingnp.lib
though. This means for example thatfrom numpy.lib.stride_tricks import *
will not importas_strided
.It would not be hard to fix the
__all__
usage (as the comment says) there are two options I see:__all__
(which all can add to, or ignore) and manually import all of those names (this is slightly ugly, but not really tricky, I had the code ready but felt it is better taken in steps)Keeping as draft, because I think it requires a decision, I expect it is ready for review code-wise, besides a test for what is in
np.lib.__dir__
...