mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 12:54:11 +08:00
This is the result of applying the ruff `UP035` check. `Callable` is imported from `collections.abc` instead of `typing`. `TypeAlias` is also imported from `typing`. This PR is the follow-up of #163947. Pull Request resolved: https://github.com/pytorch/pytorch/pull/164054 Approved by: https://github.com/ezyang, https://github.com/Skylion007
54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
import importlib
|
|
import warnings
|
|
from collections.abc import Callable
|
|
|
|
|
|
_MESSAGE_TEMPLATE = (
|
|
r"Usage of '{old_location}' is deprecated; please use '{new_location}' instead."
|
|
)
|
|
|
|
|
|
def lazy_deprecated_import(
|
|
all: list[str],
|
|
old_module: str,
|
|
new_module: str,
|
|
) -> Callable:
|
|
r"""Import utility to lazily import deprecated packages / modules / functional.
|
|
|
|
The old_module and new_module are also used in the deprecation warning defined
|
|
by the `_MESSAGE_TEMPLATE`.
|
|
|
|
Args:
|
|
all: The list of the functions that are imported. Generally, the module's
|
|
__all__ list of the module.
|
|
old_module: Old module location
|
|
new_module: New module location / Migrated location
|
|
|
|
Returns:
|
|
Callable to assign to the `__getattr__`
|
|
|
|
Usage:
|
|
|
|
# In the `torch/nn/quantized/functional.py`
|
|
from torch.nn.utils._deprecation_utils import lazy_deprecated_import
|
|
_MIGRATED_TO = "torch.ao.nn.quantized.functional"
|
|
__getattr__ = lazy_deprecated_import(
|
|
all=__all__,
|
|
old_module=__name__,
|
|
new_module=_MIGRATED_TO)
|
|
"""
|
|
warning_message = _MESSAGE_TEMPLATE.format(
|
|
old_location=old_module, new_location=new_module
|
|
)
|
|
|
|
def getattr_dunder(name: str) -> None:
|
|
if name in all:
|
|
# We are using the "RuntimeWarning" to make sure it is not
|
|
# ignored by default.
|
|
warnings.warn(warning_message, RuntimeWarning)
|
|
package = importlib.import_module(new_module)
|
|
return getattr(package, name)
|
|
raise AttributeError(f"Module {new_module!r} has no attribute {name!r}.")
|
|
|
|
return getattr_dunder
|