__init__.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. This is a module for defining private helpers which do not depend on the
  3. rest of NumPy.
  4. Everything in here must be self-contained so that it can be
  5. imported anywhere else without creating circular imports.
  6. If a utility requires the import of NumPy, it probably belongs
  7. in ``numpy._core``.
  8. """
  9. import functools
  10. import warnings
  11. from ._convertions import asbytes, asunicode
  12. def set_module(module):
  13. """Private decorator for overriding __module__ on a function or class.
  14. Example usage::
  15. @set_module('numpy')
  16. def example():
  17. pass
  18. assert example.__module__ == 'numpy'
  19. """
  20. def decorator(func):
  21. if module is not None:
  22. if isinstance(func, type):
  23. try:
  24. func._module_source = func.__module__
  25. except (AttributeError):
  26. pass
  27. func.__module__ = module
  28. return func
  29. return decorator
  30. def _rename_parameter(old_names, new_names, dep_version=None):
  31. """
  32. Generate decorator for backward-compatible keyword renaming.
  33. Apply the decorator generated by `_rename_parameter` to functions with a
  34. renamed parameter to maintain backward-compatibility.
  35. After decoration, the function behaves as follows:
  36. If only the new parameter is passed into the function, behave as usual.
  37. If only the old parameter is passed into the function (as a keyword), raise
  38. a DeprecationWarning if `dep_version` is provided, and behave as usual
  39. otherwise.
  40. If both old and new parameters are passed into the function, raise a
  41. DeprecationWarning if `dep_version` is provided, and raise the appropriate
  42. TypeError (function got multiple values for argument).
  43. Parameters
  44. ----------
  45. old_names : list of str
  46. Old names of parameters
  47. new_name : list of str
  48. New names of parameters
  49. dep_version : str, optional
  50. Version of NumPy in which old parameter was deprecated in the format
  51. 'X.Y.Z'. If supplied, the deprecation message will indicate that
  52. support for the old parameter will be removed in version 'X.Y+2.Z'
  53. Notes
  54. -----
  55. Untested with functions that accept *args. Probably won't work as written.
  56. """
  57. def decorator(fun):
  58. @functools.wraps(fun)
  59. def wrapper(*args, **kwargs):
  60. __tracebackhide__ = True # Hide traceback for py.test
  61. for old_name, new_name in zip(old_names, new_names):
  62. if old_name in kwargs:
  63. if dep_version:
  64. end_version = dep_version.split('.')
  65. end_version[1] = str(int(end_version[1]) + 2)
  66. end_version = '.'.join(end_version)
  67. msg = (f"Use of keyword argument `{old_name}` is "
  68. f"deprecated and replaced by `{new_name}`. "
  69. f"Support for `{old_name}` will be removed "
  70. f"in NumPy {end_version}.")
  71. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  72. if new_name in kwargs:
  73. msg = (f"{fun.__name__}() got multiple values for "
  74. f"argument now known as `{new_name}`")
  75. raise TypeError(msg)
  76. kwargs[new_name] = kwargs.pop(old_name)
  77. return fun(*args, **kwargs)
  78. return wrapper
  79. return decorator