|
3 | 3 | import sys
|
4 | 4 | import warnings
|
5 | 5 | from functools import wraps
|
6 |
| -from types import ModuleType |
7 | 6 | from typing import TYPE_CHECKING, ClassVar, TypeVar
|
8 | 7 |
|
9 | 8 | import attrs
|
@@ -150,28 +149,20 @@ class DeprecatedAttribute:
|
150 | 149 | instead: object = _not_set
|
151 | 150 |
|
152 | 151 |
|
153 |
| -class _ModuleWithDeprecations(ModuleType): |
154 |
| - __deprecated_attributes__: dict[str, DeprecatedAttribute] |
155 |
| - |
156 |
| - def __getattr__(self, name: str) -> object: |
157 |
| - if name in self.__deprecated_attributes__: |
158 |
| - info = self.__deprecated_attributes__[name] |
| 152 | +def deprecate_attributes( |
| 153 | + module_name: str, deprecated_attributes: dict[str, DeprecatedAttribute] |
| 154 | +) -> None: |
| 155 | + def __getattr__(name: str) -> object: |
| 156 | + if name in deprecated_attributes: |
| 157 | + info = deprecated_attributes[name] |
159 | 158 | instead = info.instead
|
160 | 159 | if instead is DeprecatedAttribute._not_set:
|
161 | 160 | instead = info.value
|
162 |
| - thing = f"{self.__name__}.{name}" |
| 161 | + thing = f"{module_name}.{name}" |
163 | 162 | warn_deprecated(thing, info.version, issue=info.issue, instead=instead)
|
164 | 163 | return info.value
|
165 | 164 |
|
166 | 165 | msg = "module '{}' has no attribute '{}'"
|
167 |
| - raise AttributeError(msg.format(self.__name__, name)) |
168 |
| - |
| 166 | + raise AttributeError(msg.format(module_name, name)) |
169 | 167 |
|
170 |
| -def enable_attribute_deprecations(module_name: str) -> None: |
171 |
| - module = sys.modules[module_name] |
172 |
| - module.__class__ = _ModuleWithDeprecations |
173 |
| - assert isinstance(module, _ModuleWithDeprecations) |
174 |
| - # Make sure that this is always defined so that |
175 |
| - # _ModuleWithDeprecations.__getattr__ can access it without jumping |
176 |
| - # through hoops or risking infinite recursion. |
177 |
| - module.__deprecated_attributes__ = {} |
| 168 | + sys.modules[module_name].__getattr__ = __getattr__ # type: ignore[method-assign] |
0 commit comments