Skip to content

Commit

Permalink
Fix UnitStrippedWarning for non arrays (#1909)
Browse files Browse the repository at this point in the history
* check magnitude is array
* numpy check
  • Loading branch information
andrewgsavage authored Dec 27, 2023
1 parent 83bffe1 commit 29a139f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pint/facets/numpy/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ..plain import PlainQuantity, MagnitudeT

from ..._typing import Shape
from ...compat import _to_magnitude, np
from ...compat import _to_magnitude, np, HAS_NUMPY
from ...errors import DimensionalityError, PintTypeError, UnitStrippedWarning
from .numpy_func import (
HANDLED_UFUNCS,
Expand Down Expand Up @@ -115,11 +115,12 @@ 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,
)
if HAS_NUMPY and isinstance(self._magnitude, 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):
Expand Down

0 comments on commit 29a139f

Please sign in to comment.