Thanks to visit codestin.com
Credit goes to github.com

Skip to content

API: Cleaning numpy/__init__.py and main namespace - Part 2 [NEP 52] #24357

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

Merged
merged 14 commits into from
Aug 12, 2023

Conversation

mtsokol
Copy link
Member

@mtsokol mtsokol commented Aug 7, 2023

This PR is a second batch of NumPy main namespace changes.

First merged part available here: #24316.

Scope of PR

  • Removed from ... import * from the main namespace numpy/__init__.py, which is the main purpose of this PR. Changes listed below are direct consequences of this modification. A full main namespace cleanup will be a separate PR.

  • Made submodules are imported lazily

  • Removed a subset of names from the top namespace listed in the “remove” section in
    ENH: Overhaul of NumPy main namespace [NEP 52] #24306 (comment). The current changelog:

    • FLOATING_POINT_SUPPORT
    • FPE_DIVIDEBYZERO
    • FPE_INVALID
    • FPE_OVERFLOW
    • FPE_UNDERFLOW
    • NINF, NZERO, PINF, PZERO
    • UFUNC_BUFSIZE_DEFAULT
    • UFUNC_PYVALS_NAME
    • CLIP, WRAP, RAISE
    • BUFSIZE
    • ALLOW_THREADS
    • MAXDIMS
    • MAY_SHARE_EXACT, MAY_SHARE_BOUNDS
    • add_newdoc, add_docstring, add_newdoc_ufunc
  • Removed issubdtype from lib as it's already imported from core (top __init__.py had to remove one of them)

  • Moved add_newdoc, add_docstring, add_newdoc_ufunc tests to core, as this module hosts their implementations

  • Removed Arrayterator from lib's __all__

@mtsokol mtsokol changed the title Overhaul of main namespace part 2 API: Cleaning numpy/__init__.py and main namespace - Part 2 [NEP 52] Aug 7, 2023
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Getting rid of import * will be useful. The last commit looks close to ready after a rebase. Note that there are some more __all__.remove usages in __init__.py that can be removed (e.g. for issubdtype, long, unicode).

@mtsokol mtsokol force-pushed the overhaul-of-main-namespace-part-2 branch from fee0b86 to ed17e34 Compare August 8, 2023 08:07
@mtsokol mtsokol force-pushed the overhaul-of-main-namespace-part-2 branch from ed17e34 to 09dad16 Compare August 8, 2023 08:14
@mtsokol mtsokol force-pushed the overhaul-of-main-namespace-part-2 branch from 64a4f4c to 110b953 Compare August 8, 2023 16:19
@mtsokol
Copy link
Member Author

mtsokol commented Aug 8, 2023

Hi @rgommers!

I pushed another batch of changes addressing review comments. Currently, in this PR:

[EDIT 9.08.2023] Changelog is reported in PR's description/top message

@ngoldbaum
Copy link
Member

I've got one question: These are used quite a bit in the codebase, what should I use in place of NZERO and PZERO? Just 0.0 and -0.0?

I'd probably change the usages in the tests to import these constants from numpy.core.umath. They probably don't need explicit docs anymore now that they're not public, so you could remove the entries for them from add_newdoc.py.

@ngoldbaum
Copy link
Member

Great to see import * starting to go away! 🚀

@mtsokol mtsokol force-pushed the overhaul-of-main-namespace-part-2 branch from e42af77 to 9bbae54 Compare August 9, 2023 10:01
@mtsokol
Copy link
Member Author

mtsokol commented Aug 9, 2023

Hi @rgommers @ngoldbaum,

This PR is ready for a review - some CI staged failed due to the fact that Pandas uses np.NINF, which was removed here. As before, I will propagate these changes in separate PRs in other related libraries.

This work's primary purpose is to remove * imports and clean a few names that are surely being removed from the top namespace. Next stage of cleanup will be in a separate PR.

@ngoldbaum
Copy link
Member

Tests are failing because pandas fails to import in the doctests build. Will be fixed once https://github.com/pandas-dev/pandas/pull/54468/files is merged and a new pandas wheel gets uploaded.

Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

Just one comment that should be deleted I think, otherwise LGTM!

@rgommers rgommers added the 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. label Aug 9, 2023
@rgommers
Copy link
Member

rgommers commented Aug 9, 2023

Tests are failing because pandas fails to import in the doctests build. Will be fixed once https://github.com/pandas-dev/pandas/pull/54468/files is merged and a new pandas wheel gets uploaded.

Looks like we're in luck; pandas uploads nightlies frequently and it's already fixed, the job passed.

@rgommers rgommers added this to the 2.0.0 release milestone Aug 9, 2023
@mtsokol mtsokol force-pushed the overhaul-of-main-namespace-part-2 branch from 39e813b to fbc386b Compare August 10, 2023 10:19
@mtsokol mtsokol force-pushed the overhaul-of-main-namespace-part-2 branch from fbc386b to 04dd24d Compare August 10, 2023 10:30
@mtsokol mtsokol requested a review from rgommers August 10, 2023 11:07
copybara-service bot pushed a commit to jax-ml/jax that referenced this pull request Aug 10, 2023
This follows the upstream NumPy deprecation of these names (numpy/numpy#24357).

PiperOrigin-RevId: 555530455
copybara-service bot pushed a commit to jax-ml/jax that referenced this pull request Aug 10, 2023
This follows the upstream NumPy deprecation of these names (numpy/numpy#24357).

PiperOrigin-RevId: 555530455
copybara-service bot pushed a commit to jax-ml/jax that referenced this pull request Aug 10, 2023
This follows the upstream NumPy deprecation of these names (numpy/numpy#24357).

PiperOrigin-RevId: 555530455
copybara-service bot pushed a commit to jax-ml/jax that referenced this pull request Aug 10, 2023
This follows the upstream NumPy deprecation of these names (numpy/numpy#24357).

PiperOrigin-RevId: 555548986
@rgommers
Copy link
Member

I'm not sure about the use of globals(), it isn't great indeed. But it's a little tricky to figure out what to replace it with. We can punt on that for now I'd say.

The one thing I did notice is that the list of public symbols still isn't 100% correct for submodule names. From https://numpy.org/neps/nep-0052-python-api-cleanup.html#cleaning-up-the-submodule-structure I think __dir__ and __all__ should contain all the namespaces that are listed as "regular", "special-purpose" and "legacy", and the ones listed for "removal" can be left out (or included now and removed later). And all the other ones can be lazy-loaded.

@mtsokol
Copy link
Member Author

mtsokol commented Aug 11, 2023

The one thing I did notice is that the list of public symbols still isn't 100% correct for submodule names. From https://numpy.org/neps/nep-0052-python-api-cleanup.html#cleaning-up-the-submodule-structure I think __dir__ and __all__ should contain all the namespaces that are listed as "regular", "special-purpose" and "legacy", and the ones listed for "removal" can be left out (or included now and removed later). And all the other ones can be lazy-loaded.

@rgommers In this PR I wanted to mostly focus on removing from ... import *. The PR with Part 3 that I'm working on I cover exact refactor of main namespace (following NEP), but I can include all target submodules here already. Should I move it here?

@rgommers
Copy link
Member

This looks good to me now:

>>> l = np.__dir__()
>>> l2 = [str(s) for s in np.load('../bldnumpy/main.npy')]  # __dir__ from current `main`
>>> set(l2) - set(l)
{'PZERO', 'add_newdoc', 'MAY_SHARE_EXACT', 'ALLOW_THREADS', 'UFUNC_BUFSIZE_DEFAULT', 'CLIP', 'RAISE', 'NAN', 'UFUNC_PYVALS_NAME', 'NINF', 'FPE_OVERFLOW', 'version', 'FPE_INVALID', 'FPE_UNDERFLOW', '_UFUNC_API', 'MAXDIMS', 'WRAP', 'MAY_SHARE_BOUNDS', 'add_docstring', 'NZERO', 'add_newdoc_ufunc', 'FPE_DIVIDEBYZERO', 'PINF', '_add_newdoc_ufunc', 'FLOATING_POINT_SUPPORT', 'BUFSIZE'}
>>> set(l) - set(l2)
{'f2py', 'array_api', 'typing', 'distutils', '__numpy_submodules__'}

>>> {s for s in set(np.__dir__()) - set(np.__all__)}
{'__getattr__', '__package__', '_NoValue', '_pyinstaller_hooks_dir', '__builtins__', '__spec__', '_typing', '__loader__', '__git_version__', '__config__', '__all__', '_globals', '__path__', '__doc__', '__NUMPY_SETUP__', '__dir__', '_distributor_init', '__file__', '_pytesttester', '_version', '__future_scalars__', '__cached__', '_utils', '_type_info', '_int_extended_msg', '__name__', '__former_attrs__', '_CopyMode', '_msg', '__numpy_submodules__', '_mat', '_specific_msg', '_version_meson'}

>>> {s for s in set(np.__dir__()) - set(np.__all__) if not s.startswith('_')}
set()
>>> {s for s in set(np.__all__) - set(np.__dir__()) if not s.startswith('_')}
set()

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

This looks good to me now. The added test to ensure that __dir__() and __all__ contain the same public members is nice and will be useful going forward.

One last question about Arrayterator, since that's a change to numpy.lib that would have been nicer to avoid.

All the removed items from the main namespace here seem uncontroversial, and you've checked that none of them are used in Pandas, scikit-learn, SciPy, Matplotlib, JAX, CuPy et al. anymore (or, in most cases, never were used to begin with).

@mtsokol
Copy link
Member Author

mtsokol commented Aug 11, 2023

@rgommers One last thing - I double checked other libraries that are impacted and I noticed that SciPy uses from numpy.lib import add_newdoc, and I removed add_newdoc also from lib here, but shouldn't - let me revert that change.

@rgommers
Copy link
Member

Thanks Mateusz! I know this is fiddly, but in the long run it'll be worth it to have PRs that are independent and scoped to a well-defined purpose.

I'll look at merging this once CI is happy.

@mtsokol
Copy link
Member Author

mtsokol commented Aug 11, 2023

Thanks Mateusz! I know this is fiddly, but in the long run it'll be worth it to have PRs that are independent and scoped to a well-defined purpose.

I'll look at merging this once CI is happy.

@rgommers Looks that CI completed. I did one more round checking usages of removed items in other libraries and found one in SciPy that I missed: scipy/scipy#19045

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Thanks @mtsokol, all LGTM now. I ran out of time with the many other NumPy changes; will leave this open right now just in case someone else wants to have a look. I'll plan to merge it tomorrow.

@rgommers rgommers merged commit 250e147 into numpy:main Aug 12, 2023
@rgommers
Copy link
Member

In it went - thanks again Mateusz!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. Numpy 2.0 API Changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants