|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import sys |
4 | 3 | import warnings |
5 | 4 | from functools import wraps |
6 | | -from types import ModuleType |
7 | 5 | from typing import TYPE_CHECKING, ClassVar, TypeVar |
8 | 6 |
|
9 | 7 | import attrs |
@@ -150,28 +148,20 @@ class DeprecatedAttribute: |
150 | 148 | instead: object = _not_set |
151 | 149 |
|
152 | 150 |
|
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] |
| 151 | +def getattr_for_deprecated_attributes( |
| 152 | + module_name: str, deprecated_attributes: dict[str, DeprecatedAttribute] |
| 153 | +) -> Callable[[str], object]: |
| 154 | + def __getattr__(name: str) -> object: |
| 155 | + if name in deprecated_attributes: |
| 156 | + info = deprecated_attributes[name] |
159 | 157 | instead = info.instead |
160 | 158 | if instead is DeprecatedAttribute._not_set: |
161 | 159 | instead = info.value |
162 | | - thing = f"{self.__name__}.{name}" |
| 160 | + thing = f"{module_name}.{name}" |
163 | 161 | warn_deprecated(thing, info.version, issue=info.issue, instead=instead) |
164 | 162 | return info.value |
165 | 163 |
|
166 | 164 | msg = "module '{}' has no attribute '{}'" |
167 | | - raise AttributeError(msg.format(self.__name__, name)) |
168 | | - |
| 165 | + raise AttributeError(msg.format(module_name, name)) |
169 | 166 |
|
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__ = {} |
| 167 | + return __getattr__ |
0 commit comments