summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Thielen <github@jont.cc>2019-12-08 21:28:27 -0600
committerJon Thielen <github@jont.cc>2019-12-08 21:40:03 -0600
commit65a9af13631191230e6dbb477f0c49a35adcb31a (patch)
tree15bc46ea50a27edc2cbcea005d198555384f7770
parent3fe1d273cb5515dddb3935b12c869039d3b6b5b7 (diff)
downloadpint-65a9af13631191230e6dbb477f0c49a35adcb31a.tar.gz
Move BehaviorChangeWarning to first array Quantity creation
-rw-r--r--pint/compat.py21
-rw-r--r--pint/quantity.py10
-rw-r--r--pint/testsuite/test_quantity.py14
3 files changed, 34 insertions, 11 deletions
diff --git a/pint/compat.py b/pint/compat.py
index 254a7b1..abe5126 100644
--- a/pint/compat.py
+++ b/pint/compat.py
@@ -25,17 +25,21 @@ def tokenizer(input_string):
# TODO: remove this warning after v0.10
class BehaviorChangeWarning(UserWarning):
pass
-_msg = """The way pint handles numpy operations has changed with the implementation of NEP 18.
-Unimplemented numpy operations will now fail instead of making assumptions about units. Some
-functions, eg concat, will now return Quanties with units, where they returned ndarrays
-previously. See https://github.com/hgrecco/pint/pull/905.
+array_function_change_msg = """The way Pint handles NumPy operations has changed with the
+implementation of NEP 18. Unimplemented NumPy operations will now fail instead of making
+assumptions about units. Some functions, eg concat, will now return Quanties with units, where
+they returned ndarrays previously. See https://github.com/hgrecco/pint/pull/905.
-To hide this warning use the following code to import pint:
+To hide this warning, wrap your first creation of an array Quantity with
+warnings.catch_warnings(), like the following:
+import numpy as np
import warnings
+from pint import Quantity
+
with warnings.catch_warnings():
warnings.simplefilter("ignore")
- import pint
+ Quantity([])
To disable the new behavior, see
https://www.numpy.org/neps/nep-0018-array-function-protocol.html#implementation
@@ -74,9 +78,7 @@ try:
return False
HAS_NUMPY_ARRAY_FUNCTION = _test_array_function_protocol()
-
- if HAS_NUMPY_ARRAY_FUNCTION:
- warnings.warn(_msg, BehaviorChangeWarning)
+ SKIP_ARRAY_FUNCTION_CHANGE_WARNING = not HAS_NUMPY_ARRAY_FUNCTION
NP_NO_VALUE = np._NoValue
@@ -91,6 +93,7 @@ except ImportError:
NUMPY_VER = '0'
NUMERIC_TYPES = (Number, Decimal)
HAS_NUMPY_ARRAY_FUNCTION = False
+ SKIP_ARRAY_FUNCTION_CHANGE_WARNING = True
NP_NO_VALUE = None
def _to_magnitude(value, force_ndarray=False):
diff --git a/pint/quantity.py b/pint/quantity.py
index ca6f25f..6d57e42 100644
--- a/pint/quantity.py
+++ b/pint/quantity.py
@@ -26,7 +26,9 @@ from .formatting import (remove_custom_flags, siunitx_format_unit, ndarray_to_la
from .errors import (DimensionalityError, OffsetUnitCalculusError, PintTypeError,
UndefinedUnitError, UnitStrippedWarning)
from .definitions import UnitDefinition
-from .compat import Loc, NUMPY_VER, ndarray, np, _to_magnitude, is_upcast_type, eq
+from .compat import (Loc, NUMPY_VER, SKIP_ARRAY_FUNCTION_CHANGE_WARNING,
+ BehaviorChangeWarning, ndarray, np, _to_magnitude, is_upcast_type, eq,
+ array_function_change_msg)
from .util import (PrettyIPython, logger, UnitsContainer, SharedRegistryObject,
to_units_container, infer_base_unit, iterable, sized)
from .numpy_func import (HANDLED_UFUNCS, copy_units_output_ufuncs, get_op_output_unit,
@@ -118,6 +120,8 @@ class Quantity(PrettyIPython, SharedRegistryObject):
return _unpickle, (Quantity, self.magnitude, self._units)
def __new__(cls, value, units=None):
+ global SKIP_ARRAY_FUNCTION_CHANGE_WARNING
+
if units is None:
if isinstance(value, str):
if value == '':
@@ -156,6 +160,10 @@ class Quantity(PrettyIPython, SharedRegistryObject):
inst.__used = False
inst.__handling = None
+ if not SKIP_ARRAY_FUNCTION_CHANGE_WARNING and isinstance(inst._magnitude, ndarray):
+ warnings.warn(array_function_change_msg, BehaviorChangeWarning)
+ SKIP_ARRAY_FUNCTION_CHANGE_WARNING = True
+
return inst
@property
diff --git a/pint/testsuite/test_quantity.py b/pint/testsuite/test_quantity.py
index d07e229..dc82ff2 100644
--- a/pint/testsuite/test_quantity.py
+++ b/pint/testsuite/test_quantity.py
@@ -4,12 +4,14 @@ import copy
import datetime
import math
import operator as op
+import warnings
from pint import DimensionalityError, OffsetUnitCalculusError, UnitRegistry
from pint.unit import UnitsContainer
-from pint.compat import np
+from pint.compat import BehaviorChangeWarning, np
from pint.testsuite import QuantityTestCase, helpers
from pint.testsuite.parameterized import ParameterizedTestCase
+from unittest.mock import patch
class TestQuantity(QuantityTestCase):
@@ -402,6 +404,16 @@ class TestQuantity(QuantityTestCase):
with self.assertRaises(TypeError):
iter(x)
+ @helpers.requires_array_function_protocol()
+ @patch('pint.quantity.SKIP_ARRAY_FUNCTION_CHANGE_WARNING', False)
+ def test_array_function_warning_on_creation(self):
+ # Test that warning is raised on first creation, but not second
+ with self.assertWarns(BehaviorChangeWarning):
+ self.Q_([])
+ with warnings.catch_warnings():
+ warnings.filterwarnings('error')
+ self.Q_([])
+
class TestQuantityToCompact(QuantityTestCase):