diff --git a/pint/compat/__init__.py b/pint/compat/__init__.py index eaf639ff1..bfe88e75f 100644 --- a/pint/compat/__init__.py +++ b/pint/compat/__init__.py @@ -122,6 +122,8 @@ def __array_function__(self, *args, **kwargs): if HAS_NUMPY_ARRAY_FUNCTION: warnings.warn(_msg, BehaviorChangeWarning) + NP_NO_VALUE = np._NoValue + except ImportError: np = None @@ -133,6 +135,7 @@ class ndarray(object): NUMPY_VER = '0' NUMERIC_TYPES = (Number, Decimal) HAS_NUMPY_ARRAY_FUNCTION = False + NP_NO_VALUE = None def _to_magnitude(value, force_ndarray=False): if isinstance(value, (dict, bool)) or value is None: diff --git a/pint/numpy_func.py b/pint/numpy_func.py index 538a0b462..7131bdde5 100644 --- a/pint/numpy_func.py +++ b/pint/numpy_func.py @@ -7,7 +7,7 @@ :license: BSD, see LICENSE for more details. """ -from .compat import is_upcast_type, np, string_types, eq +from .compat import NP_NO_VALUE, is_upcast_type, np, string_types, eq from .errors import DimensionalityError from .util import iterable, sized @@ -140,6 +140,10 @@ def decorator(func): def implement_func(func_type, func_str, input_units=None, output_unit=None): """TODO""" + # If NumPy is not available, do not attempt implement that which does not exist + if np is None: + return + func = getattr(np, func_str) @implements(func_str, func_type) @@ -418,9 +422,9 @@ def _unwrap(p, discont=None, axis=-1): @implements('amin', 'function') -def _amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, - where=np._NoValue): - if initial == np._NoValue: +def _amin(a, axis=None, out=None, keepdims=NP_NO_VALUE, initial=NP_NO_VALUE, + where=NP_NO_VALUE): + if initial == NP_NO_VALUE: (a,), output_wrap = unwrap_and_wrap_consistent_units(a) else: (a, initial), output_wrap = unwrap_and_wrap_consistent_units(a, initial) @@ -429,9 +433,9 @@ def _amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, @implements('amax', 'function') -def _amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, - where=np._NoValue): - if initial == np._NoValue: +def _amax(a, axis=None, out=None, keepdims=NP_NO_VALUE, initial=NP_NO_VALUE, + where=NP_NO_VALUE): + if initial == NP_NO_VALUE: (a,), output_wrap = unwrap_and_wrap_consistent_units(a) else: (a, initial), output_wrap = unwrap_and_wrap_consistent_units(a, initial)