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

Remove __array__ interface #1778

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 3 additions & 12 deletions pint/facets/numpy/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@

import functools
import math
import warnings
from typing import Any, Generic

from ..plain import PlainQuantity, MagnitudeT

from ..._typing import Shape
from ...compat import _to_magnitude, np
from ...errors import DimensionalityError, PintTypeError, UnitStrippedWarning
from ...errors import DimensionalityError, PintTypeError
from .numpy_func import (
HANDLED_UFUNCS,
copy_units_output_ufuncs,
Expand Down Expand Up @@ -104,14 +103,6 @@ def _numpy_method_wrap(self, func, *args, **kwargs):

return value

def __array__(self, t=None) -> np.ndarray:
warnings.warn(
"The unit of the quantity is stripped when downcasting to ndarray.",
UnitStrippedWarning,
stacklevel=2,
)
return _to_magnitude(self._magnitude, force_ndarray=True)

def clip(self, min=None, max=None, out=None, **kwargs):
if min is not None:
if isinstance(min, self.__class__):
Expand All @@ -138,10 +129,10 @@ def put(self: NumpyQuantity, indices, values, mode="raise") -> None:
if isinstance(values, self.__class__):
values = values.to(self).magnitude
elif self.dimensionless:
values = self.__class__(values, "").to(self)
values = self.__class__(values, "").to(self).magnitude
else:
raise DimensionalityError("dimensionless", self._units)
self.magnitude.put(indices, values, mode)
self._magnitude.put(indices, values, mode)

@property
def real(self) -> NumpyQuantity:
Expand Down
13 changes: 2 additions & 11 deletions pint/testsuite/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from pint import DimensionalityError, OffsetUnitCalculusError, UnitStrippedWarning
from pint import DimensionalityError, OffsetUnitCalculusError
from pint.compat import np
from pint.testsuite import helpers
from pint.testsuite.test_umath import TestUFuncs
Expand Down Expand Up @@ -1255,17 +1255,8 @@ def test_delete(self):
np.array([1, 3, 5, 7, 8, 9, 10, 11, 12]) * self.ureg.m,
)

def test_ndarray_downcast(self):
with pytest.warns(UnitStrippedWarning):
np.asarray(self.q)

def test_ndarray_downcast_with_dtype(self):
with pytest.warns(UnitStrippedWarning):
qarr = np.asarray(self.q, dtype=np.float64)
assert qarr.dtype == np.float64

def test_array_protocol_unavailable(self):
for attr in ("__array_struct__", "__array_interface__"):
for attr in ("__array__", "__array_struct__", "__array_interface__"):
with pytest.raises(AttributeError):
getattr(self.q, attr)

Expand Down
2 changes: 1 addition & 1 deletion pint/testsuite/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def test_no_longer_array_function_warning_on_creation(self):

@helpers.requires_not_numpy()
def test_no_ndarray_coercion_without_numpy(self):
with pytest.raises(ValueError):
with pytest.raises(AttributeError):
self.Q_(1, "m").__array__()

@patch(
Expand Down