Skip to content

Commit

Permalink
Add support for NumPy 2.0. (#681)
Browse files Browse the repository at this point in the history
* Add support for NumPy 2.0.

* Avoid using private NumPy API.

* Allow for `OverflowError` in `can_store`.

* Replace `np.array(..., copy=False)` with `np.asarray(...)`.
  • Loading branch information
hameerabbasi committed May 17, 2024
1 parent a9ef0ea commit 669d423
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
matrix:
os: [ubuntu-latest]
python: ['3.10', '3.11', '3.12']
pip_opts: ['']
numba_boundscheck: [0]
include:
- os: macos-latest
Expand All @@ -16,6 +17,8 @@ jobs:
- os: ubuntu-latest
python: '3.10'
numba_boundscheck: 1
- os: ubuntu-latest
pip_opts: "--pre -U"
fail-fast: false
runs-on: ${{ matrix.os }}
env:
Expand Down Expand Up @@ -47,6 +50,7 @@ jobs:
- name: Install package
run: |
pip install -e .[tests]
pip install ${{ matrix.pip_opts }} numpy numba
- name: Run tests
run: |
pytest --pyargs sparse
Expand Down
2 changes: 1 addition & 1 deletion sparse/numba_backend/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,7 @@ def asnumpy(a, dtype=None, order=None):

if isinstance(a, SparseArray):
a = a.todense()
return np.array(a, dtype=dtype, copy=False, order=order)
return np.asarray(a, dtype=dtype, order=order)


# this code was taken from numpy.moveaxis
Expand Down
4 changes: 1 addition & 3 deletions sparse/numba_backend/_coo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,6 @@ def roll(a, shift, axis=None):
res : ndarray
Output array, with the same shape as a.
"""
from numpy.core._exceptions import UFuncTypeError

from .core import COO, as_coo

a = as_coo(a)
Expand Down Expand Up @@ -786,7 +784,7 @@ def roll(a, shift, axis=None):
for sh, ax in zip(shift, axis, strict=True):
coords[ax] += sh
coords[ax] %= a.shape[ax]
except UFuncTypeError as e:
except TypeError as e:
if is_unsigned_dtype(coords.dtype):
raise ValueError(
f"rolling with coords.dtype as {coords.dtype} is not safe. Try using a signed dtype."
Expand Down
2 changes: 1 addition & 1 deletion sparse/numba_backend/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def can_store(dtype, scalar):
warnings.simplefilter("ignore")
warnings.filterwarnings("error", "out-of-bound", DeprecationWarning)
return np.array(scalar, dtype=dtype) == np.array(scalar)
except ValueError:
except (ValueError, OverflowError):
return False


Expand Down

0 comments on commit 669d423

Please sign in to comment.