Skip to content

Commit ebe1e1d

Browse files
committed
Avoid clunky TYPE_CHECKING checks
1 parent a21564b commit ebe1e1d

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

src/trio/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@
112112

113113
from . import _deprecate as _deprecate
114114

115-
if not TYPE_CHECKING:
116-
__getattr__ = _deprecate.getattr_for_deprecated_attributes(__name__, {})
115+
_deprecate.deprecate_attributes(__name__, {})
117116

118117
# Having the public path in .__module__ attributes is important for:
119118
# - exception names in printed tracebacks

src/trio/_deprecate.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import sys
34
import warnings
45
from functools import wraps
56
from typing import TYPE_CHECKING, ClassVar, TypeVar
@@ -148,7 +149,7 @@ class DeprecatedAttribute:
148149
instead: object = _not_set
149150

150151

151-
def getattr_for_deprecated_attributes(
152+
def deprecate_attributes(
152153
module_name: str, deprecated_attributes: dict[str, DeprecatedAttribute]
153154
) -> Callable[[str], object]:
154155
def __getattr__(name: str) -> object:
@@ -164,4 +165,4 @@ def __getattr__(name: str) -> object:
164165
msg = "module '{}' has no attribute '{}'"
165166
raise AttributeError(msg.format(module_name, name))
166167

167-
return __getattr__
168+
sys.modules[module_name].__getattr__ = __getattr__
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
regular = "hi"
22

3-
# Make sure that we don't trigger infinite recursion when accessing module
4-
# attributes in between calling enable_attribute_deprecations and defining
5-
# __deprecated_attributes__:
63
import sys
7-
from typing import TYPE_CHECKING
84

95
from .. import _deprecate
106

7+
_deprecate.deprecate_attributes(
8+
__name__,
9+
{
10+
"dep1": _deprecate.DeprecatedAttribute("value1", "1.1", issue=1),
11+
"dep2": _deprecate.DeprecatedAttribute(
12+
"value2",
13+
"1.2",
14+
issue=1,
15+
instead="instead-string",
16+
),
17+
},
18+
)
19+
1120
this_mod = sys.modules[__name__]
1221
assert this_mod.regular == "hi"
13-
assert not hasattr(this_mod, "dep1")
14-
15-
if not TYPE_CHECKING:
16-
__getattr__ = _deprecate.getattr_for_deprecated_attributes(
17-
__name__,
18-
{
19-
"dep1": _deprecate.DeprecatedAttribute("value1", "1.1", issue=1),
20-
"dep2": _deprecate.DeprecatedAttribute(
21-
"value2",
22-
"1.2",
23-
issue=1,
24-
instead="instead-string",
25-
),
26-
},
27-
)
22+
assert "dep1" not in globals()

0 commit comments

Comments
 (0)