Skip to content

Conversation

@tylerjereddy
Copy link
Contributor

  • Fixes Test failures due to asarray(..., copy=False) semantics change in numpy main #20170

  • restore the old copy behavior when using NumPy >= 2.0.0dev0 for now; I think Mateusz
    was using asarray to solve the same problem more concisely previously--I'm not bothered if folks prefer that, this was just more obviously restoring the original behavior to me (at least, I think it is...)

  • test_raise_non_numeric_gh18254 was mocking an array-like object so it gained an otherwise
    unused copy argument for compliance; same goes
    for FakeArray2 in FFT testing (and in other
    places that use arr mocking after that...)

  • old shims using np._CopyMode are not safe to use (well, they appear to be more complex
    than just bools now)

  • some non-mock objects (I think) like LowRankMatrix and Jacobian were also given a copy argument for arr to play along, though I haven't thought about the deeper consequences of doing that

  • array_api_strict needs a copy shim or two as well I think; that's external and not in scope here I don't think, but will cause this to fail until then:
    test_dispatch_to_unrecognize_library

  • apart from that one test vs. NumPy main, the rest of the suite passed with NumPy main and 1.26.4 on this branch in my hands

[skip circle] [skip cirrus]

@mtsokol I'm happy to back out the stuff you already covered in gh-20171, I think that's just a tiny subset of this though, so shouldn't be too disruptive.

* Fixes scipygh-20170

* restore the old `copy` behavior when using
NumPy >= `2.0.0dev0` for now; I think Mateusz
was using `asarray` to solve the same problem more concisely
previously--I'm not bothered if folks prefer that,
this was just more obviously restoring the original
behavior to me (at least, I think it is...)

* `test_raise_non_numeric_gh18254` was mocking
an array-like object so it gained an otherwise
unused `copy` argument for compliance; same goes
for `FakeArray2` in FFT testing (and in other
places that use arr mocking after that...)

* old shims using `np._CopyMode` are not safe
to use (well, they appear to be more complex
than just bools now)

* some non-mock objects (I think) like `LowRankMatrix` and `Jacobian`
were also given a `copy` argument for arr to play along,
though I haven't thought about the deeper consequences
of doing that

* `array_api_strict` needs a `copy` shim or two as well
I think; that's external and not in scope here I don't think,
but will cause this to fail until then:
`test_dispatch_to_unrecognize_library`

* apart from that one test vs. NumPy `main`, the rest of
the suite passed with NumPy `main` and `1.26.4` on this
branch in my hands

[skip circle] [skip cirrus]
@tylerjereddy tylerjereddy added the maintenance Items related to regular maintenance tasks label Mar 1, 2024
@tylerjereddy tylerjereddy added this to the 1.13.0 milestone Mar 1, 2024
@tylerjereddy tylerjereddy changed the title MAINT: array copy semantics shims MAINT: (additional) array copy semantics shims Mar 1, 2024
@mtsokol
Copy link
Contributor

mtsokol commented Mar 1, 2024

@mtsokol I'm happy to back out the stuff you already covered in #20171, I think that's just a tiny subset of this though, so shouldn't be too disruptive.

@tylerjereddy Let's keep it all here in one PR! I see that __array__(..., copy=...) change was missing - this is something new that was added to NumPy copy PR after I did SciPy changes - I forgot to address those too.

rgommers added 2 commits March 2, 2024 09:22
Also make the code to deal with changes in NumPy around the `copy`
keyword as concise as possible; do the version checks only in a single
place.

This also resolved linting errors in the previous commit.

[skip cirrus]
This is required, not doing so is deprecated. Unclear why it doesn't
always emit a warning.

Also reduce overparametrization of a stats test that used `__array__`.

[skip cirrus]
@rgommers
Copy link
Member

rgommers commented Mar 2, 2024

Thanks @tylerjereddy. I added some changes to:

  • resolve linter complaints,
  • complete signatures of __array__ so they always have both dtype and copy keywords
  • do the checks for numpy version only once and make the inline checks for copy keywords as concise as possible
  • removed some over-parametrization of a stats test that used __array__, since I noticed it and it failed ~20x with the same issue

@lucascolley
Copy link
Member

Remaining CI failure is a real complaint from Mypy

@rgommers
Copy link
Member

rgommers commented Mar 2, 2024

Remaining CI failure is a real complaint from Mypy

Fixed now.

This is probably best squash-merged once CI is happy. I'm away for most of the day, but if whoever does the merge could also hit the "Run workflow" button on https://github.com/scipy/scipy/actions/workflows/wheels.yml after merging, that would be very useful.

self.collapse()

def __array__(self):
def __array__(self, copy=None):
Copy link
Member

Choose a reason for hiding this comment

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

dtype=None is used in every other signature change in the PR but not here - is that deliberate? Otherwise LGTM

Copy link
Member

@rgommers rgommers Mar 2, 2024

Choose a reason for hiding this comment

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

Good catch. Tests didn't fail and my code search skills apparently fell short.

This code doesn't really seem to be used (beyond internally in one other class, so dtype/copy cannot be passed), I'll add the keyword with a warning that any input that's not None is not handled.

@rgommers rgommers merged commit f40f408 into scipy:main Mar 2, 2024
@rgommers
Copy link
Member

rgommers commented Mar 2, 2024

We have several broken CI jobs and wheels to this, so I hit the green button. Restarting wheel builds now. Thanks @tylerjereddy and @lucascolley!

@tylerjereddy tylerjereddy deleted the treddy_issue_20170 branch March 2, 2024 21:27
erykoff added a commit to erykoff/fitsio that referenced this pull request May 17, 2024
brisvag pushed a commit to vispy/vispy that referenced this pull request Jun 17, 2024
This introduces numpy 2.0 support. napari has started looking into
support, and ran into a few small issues using vispy testing against it.
See the [napari zulip
thread](https://napari.zulipchat.com/#narrow/stream/212875-general/topic/handling.20the.20numpy.202.2E0.20release/near/381330412)
for some discussion and relevant links.

The process for adding support in this PR was:
* Run the ruff numpy 2.0 support check and fix:
```
ruff check vispy --select NPY201
ruff check vispy --select NPY201 --fix
```
* Run tests, fix issues and warnings
* Update for changes to behavior of the `copy` kwarg in `np.array`
* Add `vispy.util.np_copy_if_needed` value (`False` for numpy < 1.28,
`None` for numpy >= 2.0)
    * Search for `copy=False` used in `no.array` and remove it
* Search for `copy=copy` used in `np.array`, replace with
`copy=vispy.util.np_copy_if_needed`
    * See references for more information
* [NumPy 2.0 Migration
Guide](https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword)
* [SciPy PR doing the same](scipy/scipy#20172)
* [napari PR doing the same](napari/napari#6932)
* Update build configuration for Cython code
* Previously, it was important to link against the oldest compatible
numpy version to ensure
      forward compatibility. This was accomplished by depending on

[oldest-supported-numpy](https://pypi.org/project/oldest-supported-numpy/).
* Now, you can just depend on `numpy>=2.0.0rc1`, and the build will be
compatible with both 1.x
      and 2.x.
* This is updated in the `[build-system]` section of `pyproject.toml`
* See [Depending on
NumPy](https://numpy.org/devdocs/dev/depending_on_numpy.html#numpy-2-0-specific-advice)
for more information


I think this is ready for review, but I'm going to try creating a wheel
and using to run napari tests with numpy 2.0 as well. I'll mark it
non-draft after I do that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Items related to regular maintenance tasks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failures due to asarray(..., copy=False) semantics change in numpy main

4 participants