_compatibility.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import textwrap
  2. from collections.abc import Callable
  3. from typing import Any, TypeVar
  4. _BACK_COMPAT_OBJECTS: dict[Any, None] = {}
  5. _MARKED_WITH_COMPATIBILITY: dict[Any, None] = {}
  6. _T = TypeVar("_T")
  7. def compatibility(is_backward_compatible: bool) -> Callable[[_T], _T]:
  8. if is_backward_compatible:
  9. def mark_back_compat(fn: _T) -> _T:
  10. docstring = textwrap.dedent(getattr(fn, "__doc__", None) or "")
  11. docstring += """
  12. .. note::
  13. Backwards-compatibility for this API is guaranteed.
  14. """
  15. fn.__doc__ = docstring
  16. _BACK_COMPAT_OBJECTS.setdefault(fn)
  17. _MARKED_WITH_COMPATIBILITY.setdefault(fn)
  18. return fn
  19. return mark_back_compat
  20. else:
  21. def mark_not_back_compat(fn: _T) -> _T:
  22. docstring = textwrap.dedent(getattr(fn, "__doc__", None) or "")
  23. docstring += """
  24. .. warning::
  25. This API is experimental and is *NOT* backward-compatible.
  26. """
  27. fn.__doc__ = docstring
  28. _MARKED_WITH_COMPATIBILITY.setdefault(fn)
  29. return fn
  30. return mark_not_back_compat