diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2018-02-03 12:04:46 -0800 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2018-02-03 20:44:55 -0800 |
commit | 1271e17ea6ca8abe12f7dd045ac0af8674f594d4 (patch) | |
tree | e91980324d59b95b0f698de3f8a2d9f9fa7a9c45 | |
parent | 2854d508d1c6d211f2ce99e8747eda1cb427a78a (diff) | |
download | numpy-1271e17ea6ca8abe12f7dd045ac0af8674f594d4.tar.gz |
ENH: Add a repr to np._NoValue
This change _NoValue from a class to an instance, which is more inline with the builtin None.
Fixes gh-8991, closes gh-9592
-rw-r--r-- | numpy/_globals.py | 24 | ||||
-rw-r--r-- | numpy/tests/test_reloading.py | 8 |
2 files changed, 26 insertions, 6 deletions
diff --git a/numpy/_globals.py b/numpy/_globals.py index 2d7b69bc4..9a7b458f1 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -52,11 +52,25 @@ class VisibleDeprecationWarning(UserWarning): """ pass - -class _NoValue(object): +class _NoValueType(object): """Special keyword value. - This class may be used as the default value assigned to a deprecated - keyword in order to check if it has been given a user defined value. + The instance of this class may be used as the default value assigned to a + deprecated keyword in order to check if it has been given a user defined + value. """ - pass + __instance = None + def __new__(cls): + # ensure that only one instance exists + if not cls.__instance: + cls.__instance = super(_NoValueType, cls).__new__(cls) + return cls.__instance + + # needed for python 2 to preserve identity through a pickle + def __reduce__(self): + return (self.__class__, ()) + + def __repr__(self): + return "<no value>" + +_NoValue = _NoValueType() diff --git a/numpy/tests/test_reloading.py b/numpy/tests/test_reloading.py index ca651c874..4481d76ef 100644 --- a/numpy/tests/test_reloading.py +++ b/numpy/tests/test_reloading.py @@ -1,8 +1,9 @@ from __future__ import division, absolute_import, print_function import sys +import pickle -from numpy.testing import assert_raises, assert_, run_module_suite +from numpy.testing import assert_raises, assert_, assert_equal, run_module_suite if sys.version_info[:2] >= (3, 4): from importlib import reload @@ -29,6 +30,11 @@ def test_numpy_reloading(): assert_(ModuleDeprecationWarning is np.ModuleDeprecationWarning) assert_(VisibleDeprecationWarning is np.VisibleDeprecationWarning) +def test_novalue(): + import numpy as np + assert_equal(repr(np._NoValue), '<no value>') + assert_(pickle.loads(pickle.dumps(np._NoValue)) is np._NoValue) + if __name__ == "__main__": run_module_suite() |