Skip to content
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

Fix some issues with NumPy 2.0 #163

Merged
merged 8 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/docs-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
artifact-path: 0/docs/_build/html/index.html
circleci-jobs: Build Docs Preview
job-title: Click here to see a preview of the documentation.
api-token: ${{ secrets.CIRCLECI_TOKEN }}
- name: Check the URL
if: github.event.status != 'pending'
run: |
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ jobs:
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12-dev']
# https://numpy.org/neps/nep-0029-deprecation_policy.html
numpy-version: ['1.22', 'latest', 'dev']
exclude:
- python-version: '3.12-dev'
numpy-version: '1.22'
fail-fast: false
steps:
- uses: actions/checkout@v2
Expand All @@ -17,7 +22,13 @@ jobs:
set -x
set -e
python -m pip install pyflakes pytest pytest-doctestplus hypothesis pytest-cov pytest-flakes packaging
python -m pip install --pre --upgrade --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple numpy
if [[ ${{ matrix.numpy-version }} == 'latest' ]]; then
python -m pip install --pre --upgrade numpy
elif [[ ${{ matrix.numpy-version }} == 'dev' ]]; then
python -m pip install --pre --upgrade --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple numpy
else
python -m pip install --upgrade numpy==${{ matrix.numpy-version }}.*
fi
- name: Run Tests
run: |
set -x
Expand Down
6 changes: 5 additions & 1 deletion ndindex/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ class ArrayIndex(NDIndex):

def _typecheck(self, idx, shape=None, _copy=True):
try:
from numpy import ndarray, asarray, integer, bool_, empty, intp, VisibleDeprecationWarning
from numpy import ndarray, asarray, integer, bool_, empty, intp
except ImportError: # pragma: no cover
raise ImportError("NumPy must be installed to create array indices")
try:
from numpy import VisibleDeprecationWarning
except ImportError: # pragma: no cover
from numpy.exceptions import VisibleDeprecationWarning

if self.dtype is None:
raise TypeError("Do not instantiate the superclass ArrayIndex directly")
Expand Down
2 changes: 1 addition & 1 deletion ndindex/shapetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def iter_indices(*shapes, skip_axes=(), _debug=False):
array([[100],
[110]])
>>> for idx1, idx2 in iter_indices((1, 3), (2, 1)):
... print(f"{idx1 = }; {idx2 = }; {(a[idx1.raw], b[idx2.raw]) = }") # doctest: +SKIP38
... print(f"{idx1 = }; {idx2 = }; {(a[idx1.raw], b[idx2.raw]) = }") # doctest: +SKIPNP1
idx1 = Tuple(0, 0); idx2 = Tuple(0, 0); (a[idx1.raw], b[idx2.raw]) = (np.int64(0), np.int64(100))
idx1 = Tuple(0, 1); idx2 = Tuple(0, 0); (a[idx1.raw], b[idx2.raw]) = (np.int64(1), np.int64(100))
idx1 = Tuple(0, 2); idx2 = Tuple(0, 0); (a[idx1.raw], b[idx2.raw]) = (np.int64(2), np.int64(100))
Expand Down
10 changes: 6 additions & 4 deletions ndindex/tests/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

"""

import numpy

import sys
import unittest
import glob
Expand All @@ -24,10 +26,10 @@
from doctest import (DocTestRunner, DocFileSuite, DocTestSuite,
NORMALIZE_WHITESPACE, register_optionflag)

SKIP38 = register_optionflag("SKIP38")
PY38 = sys.version_info[1] == 8
if PY38:
SKIP_THIS_VERSION = SKIP38
SKIPNP1 = register_optionflag("SKIPNP1")
NP1 = numpy.__version__.startswith('1')
if NP1:
SKIP_THIS_VERSION = SKIPNP1
else:
SKIP_THIS_VERSION = 0

Expand Down
6 changes: 5 additions & 1 deletion ndindex/tests/test_shapetools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import numpy as np
try:
from numpy import AxisError as np_AxisError
except ImportError: # pragma: no cover
from numpy.exceptions import AxisError as np_AxisError

from hypothesis import assume, given, example
from hypothesis.strategies import (one_of, integers, tuples as
Expand Down Expand Up @@ -271,7 +275,7 @@ def test_iter_indices_errors():
# Check that the message is the same one used by NumPy
try:
np.sum(np.arange(10), axis=2)
except np.AxisError as e:
except np_AxisError as e:
np_msg = str(e)
else:
raise RuntimeError("np.sum() did not raise AxisError") # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion ndindex/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def reduce(self, shape=None, *, negative_int=False):
Integer(1)
>>> a[..., 1]
array(1)
>>> a[1] # doctest: +SKIP38
>>> a[1] # doctest: +SKIPNP1
np.int64(1)

See https://github.com/Quansight-Labs/ndindex/issues/22.
Expand Down
Loading