pytorch/torch/fx/_compatibility.py
Yuanyuan Chen b11593c31b [8/N] Apply ruff UP035 rule (#165214)
This is follow-up of #164653 to continue applying `UP035` fixes. The purpose is to finally enable this rule.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/165214
Approved by: https://github.com/ezyang
2025-10-15 03:18:57 +00:00

41 lines
1.1 KiB
Python

import textwrap
from collections.abc import Callable
from typing import Any, TypeVar
_BACK_COMPAT_OBJECTS: dict[Any, None] = {}
_MARKED_WITH_COMPATIBILITY: dict[Any, None] = {}
_T = TypeVar("_T")
def compatibility(is_backward_compatible: bool) -> Callable[[_T], _T]:
if is_backward_compatible:
def mark_back_compat(fn: _T) -> _T:
docstring = textwrap.dedent(getattr(fn, "__doc__", None) or "")
docstring += """
.. note::
Backwards-compatibility for this API is guaranteed.
"""
fn.__doc__ = docstring
_BACK_COMPAT_OBJECTS.setdefault(fn)
_MARKED_WITH_COMPATIBILITY.setdefault(fn)
return fn
return mark_back_compat
else:
def mark_not_back_compat(fn: _T) -> _T:
docstring = textwrap.dedent(getattr(fn, "__doc__", None) or "")
docstring += """
.. warning::
This API is experimental and is *NOT* backward-compatible.
"""
fn.__doc__ = docstring
_MARKED_WITH_COMPATIBILITY.setdefault(fn)
return fn
return mark_not_back_compat