_globals.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. Module defining global singleton classes.
  3. This module raises a RuntimeError if an attempt to reload it is made. In that
  4. way the identities of the classes defined here are fixed and will remain so
  5. even if numpy itself is reloaded. In particular, a function like the following
  6. will still work correctly after numpy is reloaded::
  7. def foo(arg=np._NoValue):
  8. if arg is np._NoValue:
  9. ...
  10. That was not the case when the singleton classes were defined in the numpy
  11. ``__init__.py`` file. See gh-7844 for a discussion of the reload problem that
  12. motivated this module.
  13. """
  14. __ALL__ = [
  15. 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', '_NoValue'
  16. ]
  17. # Disallow reloading this module so as to preserve the identities of the
  18. # classes defined here.
  19. if '_is_loaded' in globals():
  20. raise RuntimeError('Reloading numpy._globals is not allowed')
  21. _is_loaded = True
  22. class ModuleDeprecationWarning(DeprecationWarning):
  23. """Module deprecation warning.
  24. The nose tester turns ordinary Deprecation warnings into test failures.
  25. That makes it hard to deprecate whole modules, because they get
  26. imported by default. So this is a special Deprecation warning that the
  27. nose tester will let pass without making tests fail.
  28. """
  29. ModuleDeprecationWarning.__module__ = 'numpy'
  30. class VisibleDeprecationWarning(UserWarning):
  31. """Visible deprecation warning.
  32. By default, python will not show deprecation warnings, so this class
  33. can be used when a very visible warning is helpful, for example because
  34. the usage is most likely a user bug.
  35. """
  36. VisibleDeprecationWarning.__module__ = 'numpy'
  37. class _NoValueType:
  38. """Special keyword value.
  39. The instance of this class may be used as the default value assigned to a
  40. keyword if no other obvious default (e.g., `None`) is suitable,
  41. Common reasons for using this keyword are:
  42. - A new keyword is added to a function, and that function forwards its
  43. inputs to another function or method which can be defined outside of
  44. NumPy. For example, ``np.std(x)`` calls ``x.std``, so when a ``keepdims``
  45. keyword was added that could only be forwarded if the user explicitly
  46. specified ``keepdims``; downstream array libraries may not have added
  47. the same keyword, so adding ``x.std(..., keepdims=keepdims)``
  48. unconditionally could have broken previously working code.
  49. - A keyword is being deprecated, and a deprecation warning must only be
  50. emitted when the keyword is used.
  51. """
  52. __instance = None
  53. def __new__(cls):
  54. # ensure that only one instance exists
  55. if not cls.__instance:
  56. cls.__instance = super().__new__(cls)
  57. return cls.__instance
  58. # needed for python 2 to preserve identity through a pickle
  59. def __reduce__(self):
  60. return (self.__class__, ())
  61. def __repr__(self):
  62. return "<no value>"
  63. _NoValue = _NoValueType()