From baae9291cc22e333bd0d16acd79863df4046cfa0 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 16 Jun 2024 20:05:55 -0400 Subject: [PATCH] Update tests for numpy 2.0 (#4258) --- newsfragments/4258.added.md | 1 + pyo3-macros-backend/src/pyimpl.rs | 1 + pytests/tests/test_misc.py | 6 +++++- src/types/boolobject.rs | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 newsfragments/4258.added.md diff --git a/newsfragments/4258.added.md b/newsfragments/4258.added.md new file mode 100644 index 00000000..0ceea11f --- /dev/null +++ b/newsfragments/4258.added.md @@ -0,0 +1 @@ +Added support for `bool` conversion with `numpy` 2.0's `numpy.bool` type diff --git a/pyo3-macros-backend/src/pyimpl.rs b/pyo3-macros-backend/src/pyimpl.rs index 0cb7631a..27188254 100644 --- a/pyo3-macros-backend/src/pyimpl.rs +++ b/pyo3-macros-backend/src/pyimpl.rs @@ -219,6 +219,7 @@ fn impl_py_methods( ) -> TokenStream { let Ctx { pyo3_path } = ctx; quote! { + #[allow(unknown_lints, non_local_definitions)] impl #pyo3_path::impl_::pyclass::PyMethods<#ty> for #pyo3_path::impl_::pyclass::PyClassImplCollector<#ty> { diff --git a/pytests/tests/test_misc.py b/pytests/tests/test_misc.py index 88af735e..fc8e1095 100644 --- a/pytests/tests/test_misc.py +++ b/pytests/tests/test_misc.py @@ -54,7 +54,11 @@ def test_import_in_subinterpreter_forbidden(): def test_type_full_name_includes_module(): numpy = pytest.importorskip("numpy") - assert pyo3_pytests.misc.get_type_full_name(numpy.bool_(True)) == "numpy.bool_" + # For numpy 1.x and 2.x + assert pyo3_pytests.misc.get_type_full_name(numpy.bool_(True)) in [ + "numpy.bool", + "numpy.bool_", + ] def test_accepts_numpy_bool(): diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index 9b5aa659..52465ef3 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -114,7 +114,7 @@ impl FromPyObject<'_> for bool { if obj .get_type() .name() - .map_or(false, |name| name == "numpy.bool_") + .map_or(false, |name| name == "numpy.bool_" || name == "numpy.bool") { let missing_conversion = |obj: &Bound<'_, PyAny>| { PyTypeError::new_err(format!(