-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement NumPy 2.0 migration rule #7702
Conversation
Happy to include something like this. I need to think a bit more on whether this should be its own rule, or whether we should just extend As of what NumPy version are these fixes safe? As in, are there older, supported versions of NumPy for which applying these rules would cause issues? (Ultimately asking: will Ruff need to know the NumPy version?) |
Thank you for the feedback! All these breaking changes will be shipped in 2.0.0 version that is planned for this December. The latest release of numpy is 1.26.0. Only nightly 2.0.0.dev0 and future 2.0.0 versions are safe to apply these changes. If ruff would know numpy version then it could be used to determine whether to apply this rule. |
Thanks @mtsokol. Sorry if I'm misunderstanding, but some of these fixes would be safe to apply in older versions, right? For example, the rules that suggest using |
@charliermarsh Ah yes! Actually most of changes from the NumPy 2.0 migration guide can be applied to the previous versions. (My last response was confusing - only a few are backward incompatible, e.g. renaming
|
The PR's blueprint is ready from my side - it consist of three types of AST fixes:
If the code's shape is correct I will provision the rest of entries and exhaustive unit test. Please share your feedback! P.S. I get some errors in the CI that I can't reproduce locally:
I followed implementation of other numpy rules. What am I missing? |
@mtsokol - We recently renamed |
Thank you! Now CI tests are passing. |
CodSpeed Performance ReportMerging #7702 will not alter performanceComparing Summary
|
PR Check ResultsEcosystemℹ️ ecosystem check detected changes. (+3, -1, 0 error(s)) airflow (+3, -1)
- [*] 16167 fixable with the `--fix` option (6240 hidden fixes can be enabled with the `--unsafe-fixes` option). + [*] 16169 fixable with the `--fix` option (6240 hidden fixes can be enabled with the `--unsafe-fixes` option). + airflow/serialization/serializers/numpy.py:78:13: NPY201 [*] `np.float_` will be removed in the NumPy 2.0. Use `numpy.float64` instead. + airflow/serialization/serializers/numpy.py:78:60: NPY201 [*] `np.complex_` will be removed in the NumPy 2.0. Use `numpy.complex128` instead.
|
It looks like you are not changing NaN to nan. Per https://numpy.org/devdocs/numpy_2_0_migration_guide.html |
We're still finishing adding new changes but here it's just a matter of adding new entries to match in the rule (I will add the rest soon!). I was curious if the overall design of PR and three types of fixes proposed looks correct. |
87a96b4
to
6b7cca6
Compare
We finished Python API refactoring for NumPy 2.0. I updated this PR - now the new rule contains all changes introduced to the main namespace (i.e. members' removals). Out of all 52 cases in the rule only one is backward incompatible ( I added a unit test that checks all rule's fix cases. |
Thanks @mtsokol -- will queue this up for review and try to get to it shortly. |
(Sorry for the delay on this -- we shipped our formatter earlier this week and I've been busy with follow-ups from the release. This is still on my list.) |
This looks great, thanks @mtsokol! I like the way you structured the rule -- it reads well. I took some liberties in editing the rule documentation and tweaking a few of the migration messages, with the goal of making them a little more consistent with the copy and voice we use in other rules. If you have any feedback or issues with the changes, just let me know, happy to address in a follow-up PR. |
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
NPY201 | 3 | 3 | 0 | 0 | 0 |
Shouldn't the new numpy imports happen at the same level as the existing numpy import? |
@charliermarsh Hi, all tweaks look good to me - thank you for a review! |
Hello! As a user who does not know much about ruff internals, how do I try this out downstream? Do I only have to modify https://github.com/astropy/astropy/blob/main/.ruff.toml ? If so, how? Is this documented somewhere? |
@hoxbro - Can you give an example of where this is happening? |
@pllim -- You shouldn't need to modify your configuration, but you would need to build from source, which just means that you need to have Rust installed. If you have Rust installed, then you should be able to run something like: cargo run -p ruff_linter -- check ../path/to/astropy --select NPY --preview |
1 |+from numpy.lib import add_docstring | ||
1 2 | def func(): | ||
2 3 | import numpy as np |
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.
@charliermarsh, here is an example.
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.
I think that instead we should focus on trying to get this to use np.lib.add_docstring
here.
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.
I think this is a great way and will only change the line where changes are needed.
Just needs to be sure that the different submodules are initialized when running import numpy as np
.
Oh, so this isn't one of those code I can just add to the TOML? Or maybe I can in the future when this feature is released? Not sure if I want to install rust and build this from source on a CI for a Python package. |
= help: Use `numpy.lib.array_utils.byte_bounds` instead. | ||
|
||
ℹ Fix | ||
1 |+from numpy.lib.array_utils import byte_bounds |
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.
Sorry to be so noisy after the PR has been merged.
I can't do this import in numpy 1.26.0. It is possible to do from numpy.lib.utils import byte_bounds
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.
Hi! This is the only backward incompatible change in the rule, mentioned in the comment: #7702 (comment). It will be available in array_utils
from 2.0.
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.
Then I'm really sorry about the noise 🙂
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.
I should probably make this a suggested fix with a clear message.
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.
Fixed in #8474
@pllim -- It'll be available in the next release (it was just merged last night), so later today or early next week. |
And after that, I can just edit my |
@pllim yep! |
What codes are available? Is there a page where I can look this up? Thanks!! |
Everything on the rules page is available in the latest release. (We only update the documentation when we release.) |
Great, thanks! Looking forward to the release. |
Summary
Hi! Currently NumPy Python API is undergoing a cleanup process that will be delivered in NumPy 2.0 (release is planned for the end of the year).
Most changes are rather simple (renaming, removing or moving a member of the main namespace to a new place), and they could be flagged/fixed by an additional ruff rule for numpy (e.g. changing occurrences of
np.float_
tonp.float64
).Would you accept such rule?
I named it
NPY201
in the existing group, so people will receive a heads-up for changes arriving in 2.0 before actually migrating to it.This is still a draft PR.I'm not an expert in rust so if any part of code can be done better please share!NumPy 2.0 migration guide: https://numpy.org/devdocs/numpy_2_0_migration_guide.html
NEP 52: https://numpy.org/neps/nep-0052-python-api-cleanup.html
NumPy cleanup tracking issue: numpy/numpy#23999
Test Plan
A unit test is provided that checks all rule's fix cases.