_deprecation.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Helper functions for deprecation.
  2. This interface is itself unstable and may change without warning. Do
  3. not use these functions yourself, even as a joke. The underscores are
  4. there for a reason. No support will be given.
  5. In particular, most of this will go away without warning once
  6. Beautiful Soup drops support for Python 3.11, since Python 3.12
  7. defines a `@typing.deprecated()
  8. decorator. <https://peps.python.org/pep-0702/>`_
  9. """
  10. import functools
  11. import warnings
  12. from typing import (
  13. Any,
  14. Callable,
  15. )
  16. def _deprecated_alias(old_name: str, new_name: str, version: str):
  17. """Alias one attribute name to another for backward compatibility
  18. :meta private:
  19. """
  20. @property # type:ignore
  21. def alias(self) -> Any:
  22. ":meta private:"
  23. warnings.warn(
  24. f"Access to deprecated property {old_name}. (Replaced by {new_name}) -- Deprecated since version {version}.",
  25. DeprecationWarning,
  26. stacklevel=2,
  27. )
  28. return getattr(self, new_name)
  29. @alias.setter
  30. def alias(self, value: str) -> None:
  31. ":meta private:"
  32. warnings.warn(
  33. f"Write to deprecated property {old_name}. (Replaced by {new_name}) -- Deprecated since version {version}.",
  34. DeprecationWarning,
  35. stacklevel=2,
  36. )
  37. return setattr(self, new_name, value)
  38. return alias
  39. def _deprecated_function_alias(
  40. old_name: str, new_name: str, version: str
  41. ) -> Callable[[Any], Any]:
  42. def alias(self, *args: Any, **kwargs: Any) -> Any:
  43. ":meta private:"
  44. warnings.warn(
  45. f"Call to deprecated method {old_name}. (Replaced by {new_name}) -- Deprecated since version {version}.",
  46. DeprecationWarning,
  47. stacklevel=2,
  48. )
  49. return getattr(self, new_name)(*args, **kwargs)
  50. return alias
  51. def _deprecated(replaced_by: str, version: str) -> Callable:
  52. def deprecate(func: Callable) -> Callable:
  53. @functools.wraps(func)
  54. def with_warning(*args: Any, **kwargs: Any) -> Any:
  55. ":meta private:"
  56. warnings.warn(
  57. f"Call to deprecated method {func.__name__}. (Replaced by {replaced_by}) -- Deprecated since version {version}.",
  58. DeprecationWarning,
  59. stacklevel=2,
  60. )
  61. return func(*args, **kwargs)
  62. return with_warning
  63. return deprecate