-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
API: Cleaning numpy/__init__.py
and main namespace - Part 1 [NEP 52]
#24316
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
Thanks @mtsokol, this is a useful thing to tackle now. I'm thinking we may want to identify the parts tht can be merged straight away and do this in a few different PRs; the |
@rgommers, works for me! Please comment these items - I will work on it tomorrow. Then I guess first PR will be about "cleanup |
b970255
to
1e1b754
Compare
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.
@mtsokol I added the comments regarding what can be merged in a first PR. That should make the diff here a lot smaller.
Also note that if you push updates to this PR, it's probably preferable to add [skip ci]
in the commit message - no need for a full battery of CI here yet.
@rgommers it works for me! Then this PR will be for the first batch of changes (general cleaning of The second, a separate PR, will cover solving cyclic dependencies and getting rid of I'm running CI here because I prepared first batch of changes (also, I'm working on reflecting them in other libraries). |
bab34f5
to
c55bee6
Compare
@rgommers Looks that there's still a |
That's not completely clear cut (right now it goes with |
53b8a39
to
775b5dd
Compare
numpy/__init__.py
and main namespace - Part 1 [NEP 52]
I think it's ready for a review: In files where an exception/warning was used only once I used |
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.
This looks great, thanks Mateusz! And thank you for the due diligence and fixing things in Matplotlib, Pandas, SciPy, scikit-learn and JAX.
The list of differences between the np.__dir__()
output on this PR vs. 1.25.0 is:
{'ERR_CALL',
'ERR_DEFAULT',
'ERR_IGNORE',
'ERR_LOG',
'ERR_PRINT',
'ERR_RAISE',
'ERR_WARN',
'SHIFT_DIVIDEBYZERO',
'SHIFT_INVALID',
'SHIFT_OVERFLOW',
'SHIFT_UNDERFLOW',
'__deprecated_attrs__',
'__expired_functions__',
'_builtins',
'_financial_names',
'_using_numpy2_behavior',
'cast',
'compat',
'fastCopyAndTranspose',
'geterrobj',
'kernel_version',
'lookfor',
'numarray',
'oldnumeric',
'set_numeric_ops',
'seterrobj',
'source'}
all those things have indeed been removed, so this looks good.
There's nothing in here that should be controversial, so let's get it in to keep the ball rolling.
# but do not use them, we define them here for backward compatibility. | ||
oldnumeric = 'removed' | ||
numarray = 'removed' | ||
|
||
def __getattr__(attr): |
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 a next PR: copying the pattern from scipy/__init__.py
for __getattr__
to import all submodules in a lazy way rather than only numpy.testing
would be useful.
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.
Sure! And as we discussed, this will help fixing cyclic dependencies.
Just a note, the effective change here was that previously |
I can add a custom message about these warnings/exceptions when accessing them from the main namespace (same as |
I'd prefer not to do that for now - at least not until/unless we start seeing a real need. The change is trivial and should be easy to find in case one runs into it. If we are going to add messages for all changes, we will again end up with hundreds of lines of cruft in We already planned to have a single doc page with all these changes for 2.0; no need to do double work here. Everyone who uses nightlies can easily deal with this. |
Right agreed. My concern is currently only about the sum of changes being overwhelming. For some things we have good reasons to do so because they are things that nobody understands or every dev understands that their logic is flawed. For these, they are a bit fuzzy to me: it is basically a file with "legacy aliases" that is just a long list we would keep long enough that users can adopt it without a The branching is the real reason here, as we have said many times asking users to change things twice isn't great. I don't think this matters for larger libraries, they are used to it, but it does matter for scripts/small libraries. |
In other words, the reason I am fine with it unless someone disagrees, is that I think for the users that we should care about (those who are not used to adding such branching), my guess is that there are very few who will notice the changes. |
Exactly, I agree with the "no branching for the average user" rule. These are examples I think that are well below the line of usage frequency. Things like widely-used aliases (e.g., |
Relevant issues #24306 #23999
Hi @rgommers @seberg @ngoldbaum,
Here I share a draft PR connected to issue #24306. It mostly covers restructuring of
numpy/__init__.py
file.In a nutshell:
*
import._main_namespace_definition.py
is meant to be a contract of the main namespace and is used for defining__dir__
and__all__
attributes of NumPy's top namespace. Therefore it is versioned and can't be altered without modifying file in question.from .core import *
andfrom .lib import *
uncovered some cyclic dependencies in the codebase (for now I explicitly imported these names that are used internally withnp.*
causing a cycle), but ideally there should be none of them.__init__.py
file that I thought were obsolete.__init__.py
as continuous file, rather than a diff.Please share your feedback!