_globals.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. import enum
  15. __ALL__ = [
  16. 'ModuleDeprecationWarning', 'VisibleDeprecationWarning',
  17. '_NoValue', '_CopyMode'
  18. ]
  19. # Disallow reloading this module so as to preserve the identities of the
  20. # classes defined here.
  21. if '_is_loaded' in globals():
  22. raise RuntimeError('Reloading numpy._globals is not allowed')
  23. _is_loaded = True
  24. class ModuleDeprecationWarning(DeprecationWarning):
  25. """Module deprecation warning.
  26. The nose tester turns ordinary Deprecation warnings into test failures.
  27. That makes it hard to deprecate whole modules, because they get
  28. imported by default. So this is a special Deprecation warning that the
  29. nose tester will let pass without making tests fail.
  30. """
  31. ModuleDeprecationWarning.__module__ = 'numpy'
  32. class VisibleDeprecationWarning(UserWarning):
  33. """Visible deprecation warning.
  34. By default, python will not show deprecation warnings, so this class
  35. can be used when a very visible warning is helpful, for example because
  36. the usage is most likely a user bug.
  37. """
  38. VisibleDeprecationWarning.__module__ = 'numpy'
  39. class _NoValueType:
  40. """Special keyword value.
  41. The instance of this class may be used as the default value assigned to a
  42. keyword if no other obvious default (e.g., `None`) is suitable,
  43. Common reasons for using this keyword are:
  44. - A new keyword is added to a function, and that function forwards its
  45. inputs to another function or method which can be defined outside of
  46. NumPy. For example, ``np.std(x)`` calls ``x.std``, so when a ``keepdims``
  47. keyword was added that could only be forwarded if the user explicitly
  48. specified ``keepdims``; downstream array libraries may not have added
  49. the same keyword, so adding ``x.std(..., keepdims=keepdims)``
  50. unconditionally could have broken previously working code.
  51. - A keyword is being deprecated, and a deprecation warning must only be
  52. emitted when the keyword is used.
  53. """
  54. __instance = None
  55. def __new__(cls):
  56. # ensure that only one instance exists
  57. if not cls.__instance:
  58. cls.__instance = super().__new__(cls)
  59. return cls.__instance
  60. def __repr__(self):
  61. return "<no value>"
  62. _NoValue = _NoValueType()
  63. class _CopyMode(enum.Enum):
  64. """
  65. An enumeration for the copy modes supported
  66. by numpy.copy() and numpy.array(). The following three modes are supported,
  67. - ALWAYS: This means that a deep copy of the input
  68. array will always be taken.
  69. - IF_NEEDED: This means that a deep copy of the input
  70. array will be taken only if necessary.
  71. - NEVER: This means that the deep copy will never be taken.
  72. If a copy cannot be avoided then a `ValueError` will be
  73. raised.
  74. Note that the buffer-protocol could in theory do copies. NumPy currently
  75. assumes an object exporting the buffer protocol will never do this.
  76. """
  77. ALWAYS = True
  78. IF_NEEDED = False
  79. NEVER = 2
  80. def __bool__(self):
  81. # For backwards compatibility
  82. if self == _CopyMode.ALWAYS:
  83. return True
  84. if self == _CopyMode.IF_NEEDED:
  85. return False
  86. raise ValueError(f"{self} is neither True nor False.")
  87. _CopyMode.__module__ = 'numpy'