1# mypy: allow-untyped-defs 2import importlib 3import warnings 4from typing import Callable, List 5 6 7_MESSAGE_TEMPLATE = ( 8 r"Usage of '{old_location}' is deprecated; please use '{new_location}' instead." 9) 10 11 12def lazy_deprecated_import( 13 all: List[str], 14 old_module: str, 15 new_module: str, 16) -> Callable: 17 r"""Import utility to lazily import deprecated packages / modules / functional. 18 19 The old_module and new_module are also used in the deprecation warning defined 20 by the `_MESSAGE_TEMPLATE`. 21 22 Args: 23 all: The list of the functions that are imported. Generally, the module's 24 __all__ list of the module. 25 old_module: Old module location 26 new_module: New module location / Migrated location 27 28 Returns: 29 Callable to assign to the `__getattr__` 30 31 Usage: 32 33 # In the `torch/nn/quantized/functional.py` 34 from torch.nn.utils._deprecation_utils import lazy_deprecated_import 35 _MIGRATED_TO = "torch.ao.nn.quantized.functional" 36 __getattr__ = lazy_deprecated_import( 37 all=__all__, 38 old_module=__name__, 39 new_module=_MIGRATED_TO) 40 """ 41 warning_message = _MESSAGE_TEMPLATE.format( 42 old_location=old_module, new_location=new_module 43 ) 44 45 def getattr_dunder(name): 46 if name in all: 47 # We are using the "RuntimeWarning" to make sure it is not 48 # ignored by default. 49 warnings.warn(warning_message, RuntimeWarning) 50 package = importlib.import_module(new_module) 51 return getattr(package, name) 52 raise AttributeError(f"Module {new_module!r} has no attribute {name!r}.") 53 54 return getattr_dunder 55