deprecation_tools.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from __future__ import annotations
  18. import functools
  19. import importlib
  20. import sys
  21. import warnings
  22. from types import ModuleType
  23. def getattr_with_deprecation(
  24. imports: dict[str, str],
  25. module: str,
  26. override_deprecated_classes: dict[str, str],
  27. extra_message: str,
  28. name: str,
  29. ):
  30. """
  31. Retrieve the imported attribute from the redirected module and raises a deprecation warning.
  32. :param imports: dict of imports and their redirection for the module
  33. :param module: name of the module in the package to get the attribute from
  34. :param override_deprecated_classes: override target classes with deprecated ones. If target class is
  35. found in the dictionary, it will be displayed in the warning message.
  36. :param extra_message: extra message to display in the warning or import error message
  37. :param name: attribute name
  38. :return:
  39. """
  40. target_class_full_name = imports.get(name)
  41. if not target_class_full_name:
  42. raise AttributeError(f"The module `{module!r}` has no attribute `{name!r}`")
  43. warning_class_name = target_class_full_name
  44. if override_deprecated_classes and name in override_deprecated_classes:
  45. warning_class_name = override_deprecated_classes[name]
  46. message = f"The `{module}.{name}` class is deprecated. Please use `{warning_class_name!r}`."
  47. if extra_message:
  48. message += f" {extra_message}."
  49. warnings.warn(message, DeprecationWarning, stacklevel=2)
  50. new_module, new_class_name = target_class_full_name.rsplit(".", 1)
  51. try:
  52. return getattr(importlib.import_module(new_module), new_class_name)
  53. except ImportError as e:
  54. error_message = (
  55. f"Could not import `{new_module}.{new_class_name}` while trying to import `{module}.{name}`."
  56. )
  57. if extra_message:
  58. error_message += f" {extra_message}."
  59. raise ImportError(error_message) from e
  60. def add_deprecated_classes(
  61. module_imports: dict[str, dict[str, str]],
  62. package: str,
  63. override_deprecated_classes: dict[str, dict[str, str]] | None = None,
  64. extra_message: str | None = None,
  65. ):
  66. """
  67. Add deprecated class PEP-563 imports and warnings modules to the package.
  68. :param module_imports: imports to use
  69. :param package: package name
  70. :param override_deprecated_classes: override target classes with deprecated ones. If module +
  71. target class is found in the dictionary, it will be displayed in the warning message.
  72. :param extra_message: extra message to display in the warning or import error message
  73. """
  74. for module_name, imports in module_imports.items():
  75. full_module_name = f"{package}.{module_name}"
  76. module_type = ModuleType(full_module_name)
  77. if override_deprecated_classes and module_name in override_deprecated_classes:
  78. override_deprecated_classes_for_module = override_deprecated_classes[module_name]
  79. else:
  80. override_deprecated_classes_for_module = {}
  81. # Mypy is not able to derive the right function signature https://github.com/python/mypy/issues/2427
  82. module_type.__getattr__ = functools.partial( # type: ignore[assignment]
  83. getattr_with_deprecation,
  84. imports,
  85. full_module_name,
  86. override_deprecated_classes_for_module,
  87. extra_message or "",
  88. )
  89. sys.modules.setdefault(full_module_name, module_type)