__init__.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 asunicode, asbytes
  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. func.__module__ = module
  23. return func
  24. return decorator
  25. def _rename_parameter(old_names, new_names, dep_version=None):
  26. """
  27. Generate decorator for backward-compatible keyword renaming.
  28. Apply the decorator generated by `_rename_parameter` to functions with a
  29. renamed parameter to maintain backward-compatibility.
  30. After decoration, the function behaves as follows:
  31. If only the new parameter is passed into the function, behave as usual.
  32. If only the old parameter is passed into the function (as a keyword), raise
  33. a DeprecationWarning if `dep_version` is provided, and behave as usual
  34. otherwise.
  35. If both old and new parameters are passed into the function, raise a
  36. DeprecationWarning if `dep_version` is provided, and raise the appropriate
  37. TypeError (function got multiple values for argument).
  38. Parameters
  39. ----------
  40. old_names : list of str
  41. Old names of parameters
  42. new_name : list of str
  43. New names of parameters
  44. dep_version : str, optional
  45. Version of NumPy in which old parameter was deprecated in the format
  46. 'X.Y.Z'. If supplied, the deprecation message will indicate that
  47. support for the old parameter will be removed in version 'X.Y+2.Z'
  48. Notes
  49. -----
  50. Untested with functions that accept *args. Probably won't work as written.
  51. """
  52. def decorator(fun):
  53. @functools.wraps(fun)
  54. def wrapper(*args, **kwargs):
  55. __tracebackhide__ = True # Hide traceback for py.test
  56. for old_name, new_name in zip(old_names, new_names):
  57. if old_name in kwargs:
  58. if dep_version:
  59. end_version = dep_version.split('.')
  60. end_version[1] = str(int(end_version[1]) + 2)
  61. end_version = '.'.join(end_version)
  62. msg = (f"Use of keyword argument `{old_name}` is "
  63. f"deprecated and replaced by `{new_name}`. "
  64. f"Support for `{old_name}` will be removed "
  65. f"in NumPy {end_version}.")
  66. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  67. if new_name in kwargs:
  68. msg = (f"{fun.__name__}() got multiple values for "
  69. f"argument now known as `{new_name}`")
  70. raise TypeError(msg)
  71. kwargs[new_name] = kwargs.pop(old_name)
  72. return fun(*args, **kwargs)
  73. return wrapper
  74. return decorator