Skip to content

Commit

Permalink
Merge #525 from hakonanes/use-numpy-exceptions
Browse files Browse the repository at this point in the history
v0.13.2: NumPy 2.0 compatibility by using two import routes for VisibleDeprecationWarning
  • Loading branch information
pc494 authored Sep 24, 2024
2 parents 8f6cb1a + 40b6101 commit dbb4c7e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ All user facing changes to this project are documented in this file. The format
on `Keep a Changelog <https://keepachangelog.com/en/1.0.0/>`__, and this project tries
its best to adhere to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`__.

2024-09-25 - version 0.13.2
===========================

Added
-----
- Compatibility with NumPy v2.0.

2024-09-20 - version 0.13.1
===========================

Expand Down
2 changes: 1 addition & 1 deletion orix/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.13.1"
__version__ = "0.13.2"
# Sorted by line contributions (ideally excluding lines in notebook files)
__credits__ = [
"Håkon Wiik Ånes",
Expand Down
10 changes: 5 additions & 5 deletions orix/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import inspect
import warnings

import numpy as np
from orix.constants import VisibleDeprecationWarning


class deprecated:
Expand Down Expand Up @@ -88,12 +88,12 @@ def __call__(self, func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
warnings.simplefilter(
action="always", category=np.VisibleDeprecationWarning, append=True
action="always", category=VisibleDeprecationWarning, append=True
)
func_code = func.__code__
warnings.warn_explicit(
message=msg,
category=np.VisibleDeprecationWarning,
category=VisibleDeprecationWarning,
filename=func_code.co_filename,
lineno=func_code.co_firstlineno + 1,
)
Expand Down Expand Up @@ -141,12 +141,12 @@ def wrapped(*args, **kwargs):
msg += f"Use `{self.alternative}` instead. "
msg += f"See the documentation of `{func.__name__}()` for more details."
warnings.simplefilter(
action="always", category=np.VisibleDeprecationWarning, append=True
action="always", category=VisibleDeprecationWarning, append=True
)
func_code = func.__code__
warnings.warn_explicit(
message=msg,
category=np.VisibleDeprecationWarning,
category=VisibleDeprecationWarning,
filename=func_code.co_filename,
lineno=func_code.co_firstlineno + 1,
)
Expand Down
10 changes: 10 additions & 0 deletions orix/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@
eps9 = 1e-9
eps12 = 1e-12

# TODO: Remove and use numpy.exceptions.VisibleDeprecationWarning once
# NumPy 1.25 is minimal supported version
try:
# Added in NumPy 1.25.0
from numpy.exceptions import VisibleDeprecationWarning
except ImportError: # pragma: no cover
# Removed in NumPy 2.0.0
from numpy import VisibleDeprecationWarning


del optional_deps
3 changes: 2 additions & 1 deletion orix/tests/io/test_ang.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import numpy as np
import pytest

from orix.constants import VisibleDeprecationWarning
from orix.crystal_map import CrystalMap, Phase
from orix.io import load, loadang, save
from orix.io.plugins.ang import (
Expand Down Expand Up @@ -76,7 +77,7 @@
indirect=["angfile_astar"],
)
def test_loadang(angfile_astar, expected_data):
with pytest.warns(np.VisibleDeprecationWarning):
with pytest.warns(VisibleDeprecationWarning):
loaded_data = loadang(angfile_astar)
assert np.allclose(loaded_data.data, expected_data)

Expand Down
3 changes: 2 additions & 1 deletion orix/tests/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import numpy as np
import pytest

from orix.constants import VisibleDeprecationWarning
from orix.crystal_map import Phase, PhaseList
from orix.io import _overwrite_or_not, _plugin_from_manufacturer, load, loadctf, save
from orix.io.plugins import bruker_h5ebsd, emsoft_h5ebsd, orix_hdf5
Expand Down Expand Up @@ -151,6 +152,6 @@ def test_loadctf():
fname = "temp.ctf"
np.savetxt(fname, z)

with pytest.warns(np.VisibleDeprecationWarning):
with pytest.warns(VisibleDeprecationWarning):
_ = loadctf(fname)
os.remove(fname)
12 changes: 6 additions & 6 deletions orix/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import warnings

import numpy as np
import pytest

from orix._util import deprecated, deprecated_argument
from orix.constants import VisibleDeprecationWarning


class TestDeprecateFunctionOrProperty:
Expand Down Expand Up @@ -58,7 +58,7 @@ def bar_func2(self, n):

my_foo = Foo()

with pytest.warns(np.VisibleDeprecationWarning) as record:
with pytest.warns(VisibleDeprecationWarning) as record:
assert my_foo.bar_func1(4) == 5
desired_msg = (
"Function `bar_func1()` is deprecated and will be removed in version 0.8. "
Expand All @@ -72,7 +72,7 @@ def bar_func2(self, n):
f" {desired_msg}"
)

with pytest.warns(np.VisibleDeprecationWarning) as record2:
with pytest.warns(VisibleDeprecationWarning) as record2:
assert my_foo.bar_func2(4) == 6
desired_msg2 = "Function `bar_func2()` is deprecated."
assert str(record2[0].message) == desired_msg2
Expand All @@ -84,7 +84,7 @@ def bar_func2(self, n):
f" {desired_msg2}"
)

with pytest.warns(np.VisibleDeprecationWarning) as record3:
with pytest.warns(VisibleDeprecationWarning) as record3:
assert my_foo.bar_prop == 1
desired_msg3 = (
"Property `bar_prop` is deprecated and will be removed in version 1.4. "
Expand Down Expand Up @@ -123,7 +123,7 @@ def bar_arg_alt(self, **kwargs):
assert my_foo.bar_arg(b=1) == {"b": 1}

# Warns
with pytest.warns(np.VisibleDeprecationWarning) as record2:
with pytest.warns(VisibleDeprecationWarning) as record2:
assert my_foo.bar_arg(a=2) == {"a": 2}
assert str(record2[0].message) == (
r"Argument `a` is deprecated and will be removed in version 1.4. "
Expand All @@ -132,7 +132,7 @@ def bar_arg_alt(self, **kwargs):
)

# Warns with alternative
with pytest.warns(np.VisibleDeprecationWarning) as record3:
with pytest.warns(VisibleDeprecationWarning) as record3:
assert my_foo.bar_arg_alt(a=3) == {"a": 3}
assert str(record3[0].message) == (
r"Argument `a` is deprecated and will be removed in version 1.4. "
Expand Down

0 comments on commit dbb4c7e

Please sign in to comment.