From 0471c907ce8350fa225beff183d47c2acaff1259 Mon Sep 17 00:00:00 2001 From: Akash Kumar <116457960+akashchamp@users.noreply.github.com> Date: Fri, 11 Sep 2026 23:52:02 +0530 Subject: [PATCH 1/2] GH-51301: [Python] Fix NumPy and pandas nightly warnings --- python/pyarrow/tests/test_array.py | 8 ++++++++ python/pyarrow/tests/test_dlpack.py | 19 ++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py index 74ff837e16e8..d9a14abe1a33 100644 --- a/python/pyarrow/tests/test_array.py +++ b/python/pyarrow/tests/test_array.py @@ -2737,6 +2737,10 @@ def test_array_from_list_of_timestamps(unit): @pytest.mark.numpy +@pytest.mark.filterwarnings( + "ignore:The 'generic' unit for NumPy timedelta is deprecated:" + "DeprecationWarning" +) def test_array_from_timestamp_with_generic_unit(): if Version(np.__version__) >= Version("2.5.0"): pytest.skip("generic units of timedelta64 deprecated") @@ -2779,6 +2783,10 @@ def test_array_from_numpy_timedelta(dtype, type): @pytest.mark.numpy +@pytest.mark.filterwarnings( + "ignore:The 'generic' unit for NumPy timedelta is deprecated:" + "DeprecationWarning" +) def test_array_from_numpy_timedelta_incorrect_unit(): # generic (no unit) if Version(np.__version__) < Version("2.5.0"): diff --git a/python/pyarrow/tests/test_dlpack.py b/python/pyarrow/tests/test_dlpack.py index 0c3c082fd6ed..0fc0c2df0d63 100644 --- a/python/pyarrow/tests/test_dlpack.py +++ b/python/pyarrow/tests/test_dlpack.py @@ -270,10 +270,11 @@ def immutable_tensor(): @pytest.mark.parametrize('max_version', [None, (0, 8)]) def test_dlpack_legacy_capsule_immutable_tensor(max_version): tensor = immutable_tensor() - with pytest.raises(NotImplementedError, - match="Legacy DLPack support is not implemented " - "for immutable tensors"): - tensor.__dlpack__(max_version=max_version) + with pytest.warns(DeprecationWarning, match="unversioned DLPack capsule"): + with pytest.raises(NotImplementedError, + match="Legacy DLPack support is not implemented " + "for immutable tensors"): + tensor.__dlpack__(max_version=max_version) @check_bytes_allocated @@ -307,7 +308,7 @@ def test_dlpack_versioned_capsule(obj, max_version, copy): @check_bytes_allocated @pytest.mark.parametrize('obj', dlpack_objects()) def test_dlpack_versioned_roundtrip(obj): - expected = np.from_dlpack(DLPackForwarder(obj, max_version=None)) + expected = np.from_dlpack(DLPackForwarder(obj, max_version=(1, 0))) for copy in [None, False, True]: result = np.from_dlpack( DLPackForwarder(obj, max_version=(1, 0), copy=copy)) @@ -335,24 +336,24 @@ def test_dlpack_not_supported(): arr = pa.array([1, None, 3]) with pytest.raises(TypeError, match="Can only use DLPack " "on arrays with no nulls."): - np.from_dlpack(arr) + np.from_dlpack(DLPackForwarder(arr, max_version=(1, 0))) arr = pa.array( [[0, 1], [3, 4]], type=pa.list_(pa.int32()) ) with pytest.raises(TypeError, match="DataType is not compatible with DLPack spec"): - np.from_dlpack(arr) + np.from_dlpack(DLPackForwarder(arr, max_version=(1, 0))) arr = pa.array([]) with pytest.raises(TypeError, match="DataType is not compatible with DLPack spec"): - np.from_dlpack(arr) + np.from_dlpack(DLPackForwarder(arr, max_version=(1, 0))) # DLPack doesn't support bit-packed boolean values arr = pa.array([True, False, True]) with pytest.raises(TypeError, match="Bit-packed boolean data type " "not supported by DLPack."): - np.from_dlpack(arr) + np.from_dlpack(DLPackForwarder(arr, max_version=(1, 0))) def test_dlpack_cuda_not_supported(): From 9e00eb7264d3d3ece12d6df0eebb6894da8d07a3 Mon Sep 17 00:00:00 2001 From: Akash Kumar <116457960+akashchamp@users.noreply.github.com> Date: Sun, 20 Sep 2026 16:03:27 +0530 Subject: [PATCH 2/2] GH-51301: [Python] Emit legacy DLPack deprecation warning only after a successful export Array.__dlpack__ and Tensor.__dlpack__ warned about the deprecated unversioned capsule before calling into the C++ exporter, so a failing export (for example an immutable Tensor) raised NotImplementedError and emitted a DeprecationWarning at the same time. Create the legacy tensor and its capsule first and only warn once that succeeded, so the error path no longer warns and a warning escalated to an error cannot leak the exported tensor. Drop the warning expectation from test_dlpack_legacy_capsule_immutable_tensor accordingly. --- python/pyarrow/array.pxi | 7 +++++-- python/pyarrow/tensor.pxi | 7 +++++-- python/pyarrow/tests/test_dlpack.py | 9 ++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index bf9a6d672280..f460ebcfff7a 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -2390,12 +2390,15 @@ cdef class Array(_PandasConvertible): ) # Note: from March 2025 onwards, it's okay to raise BufferError here. # Still we keep the V0 version as the V1 was only added in August 2026. + legacy_tensor = GetResultValue(ExportArrayToDLPack(self.sp_array)) + # Wrap the exported tensor in its capsule before warning so that a + # warning escalated to an error cannot leak the tensor. + capsule = PyCapsule_New(legacy_tensor, 'dltensor', dlpack_pycapsule_deleter) warnings.warn( "Exporting an unversioned DLPack capsule is deprecated, " "pass max_version=(1, 0) or higher.", DeprecationWarning, stacklevel=2) - legacy_tensor = GetResultValue(ExportArrayToDLPack(self.sp_array)) - return PyCapsule_New(legacy_tensor, 'dltensor', dlpack_pycapsule_deleter) + return capsule # Currently no major version other than legacy 0 and current 1.3 dlm_tensor = GetResultValue( diff --git a/python/pyarrow/tensor.pxi b/python/pyarrow/tensor.pxi index 7f006f294175..c66fc6321de7 100644 --- a/python/pyarrow/tensor.pxi +++ b/python/pyarrow/tensor.pxi @@ -401,12 +401,15 @@ strides: {self.strides}""" ) # Note: from March 2025 onwards, it's okay to raise BufferError here. # Still we keep the V0 version as the V1 was only added in August 2026. + legacy_tensor = GetResultValue(ExportTensorToDLPack(self.sp_tensor)) + # Wrap the exported tensor in its capsule before warning so that a + # warning escalated to an error cannot leak the tensor. + capsule = PyCapsule_New(legacy_tensor, 'dltensor', dlpack_pycapsule_deleter) warnings.warn( "Exporting an unversioned DLPack capsule is deprecated, " "pass max_version=(1, 0) or higher.", DeprecationWarning, stacklevel=2) - legacy_tensor = GetResultValue(ExportTensorToDLPack(self.sp_tensor)) - return PyCapsule_New(legacy_tensor, 'dltensor', dlpack_pycapsule_deleter) + return capsule # Currently no major version other than legacy 0 and current 1.3 dlm_tensor = GetResultValue( diff --git a/python/pyarrow/tests/test_dlpack.py b/python/pyarrow/tests/test_dlpack.py index 0fc0c2df0d63..88eab11e0609 100644 --- a/python/pyarrow/tests/test_dlpack.py +++ b/python/pyarrow/tests/test_dlpack.py @@ -270,11 +270,10 @@ def immutable_tensor(): @pytest.mark.parametrize('max_version', [None, (0, 8)]) def test_dlpack_legacy_capsule_immutable_tensor(max_version): tensor = immutable_tensor() - with pytest.warns(DeprecationWarning, match="unversioned DLPack capsule"): - with pytest.raises(NotImplementedError, - match="Legacy DLPack support is not implemented " - "for immutable tensors"): - tensor.__dlpack__(max_version=max_version) + with pytest.raises(NotImplementedError, + match="Legacy DLPack support is not implemented " + "for immutable tensors"): + tensor.__dlpack__(max_version=max_version) @check_bytes_allocated