Skip to content

Use PEP 562 instead of _ModuleWithDeprecations #3230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/2135.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow `trio` to be a `types.ModuleType` and still have deprecated attributes.
4 changes: 1 addition & 3 deletions src/trio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@

from . import _deprecate as _deprecate

_deprecate.enable_attribute_deprecations(__name__)

__deprecated_attributes__: dict[str, _deprecate.DeprecatedAttribute] = {}
_deprecate.deprecate_attributes(__name__, {})

# Having the public path in .__module__ attributes is important for:
# - exception names in printed tracebacks
Expand Down
27 changes: 9 additions & 18 deletions src/trio/_deprecate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys
import warnings
from functools import wraps
from types import ModuleType
from typing import TYPE_CHECKING, ClassVar, TypeVar

import attrs
Expand Down Expand Up @@ -150,28 +149,20 @@ class DeprecatedAttribute:
instead: object = _not_set


class _ModuleWithDeprecations(ModuleType):
__deprecated_attributes__: dict[str, DeprecatedAttribute]

def __getattr__(self, name: str) -> object:
if name in self.__deprecated_attributes__:
info = self.__deprecated_attributes__[name]
def deprecate_attributes(
module_name: str, deprecated_attributes: dict[str, DeprecatedAttribute]
) -> None:
def __getattr__(name: str) -> object:
if name in deprecated_attributes:
info = deprecated_attributes[name]
instead = info.instead
if instead is DeprecatedAttribute._not_set:
instead = info.value
thing = f"{self.__name__}.{name}"
thing = f"{module_name}.{name}"
warn_deprecated(thing, info.version, issue=info.issue, instead=instead)
return info.value

msg = "module '{}' has no attribute '{}'"
raise AttributeError(msg.format(self.__name__, name))

raise AttributeError(msg.format(module_name, name))

def enable_attribute_deprecations(module_name: str) -> None:
module = sys.modules[module_name]
module.__class__ = _ModuleWithDeprecations
assert isinstance(module, _ModuleWithDeprecations)
# Make sure that this is always defined so that
# _ModuleWithDeprecations.__getattr__ can access it without jumping
# through hoops or risking infinite recursion.
module.__deprecated_attributes__ = {}
sys.modules[module_name].__getattr__ = __getattr__ # type: ignore[method-assign]
32 changes: 15 additions & 17 deletions src/trio/_tests/module_with_deprecations.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
regular = "hi"

import sys

from .. import _deprecate

_deprecate.enable_attribute_deprecations(__name__)

# Make sure that we don't trigger infinite recursion when accessing module
# attributes in between calling enable_attribute_deprecations and defining
# __deprecated_attributes__:
import sys
_deprecate.deprecate_attributes(
__name__,
{
"dep1": _deprecate.DeprecatedAttribute("value1", "1.1", issue=1),
"dep2": _deprecate.DeprecatedAttribute(
"value2",
"1.2",
issue=1,
instead="instead-string",
),
},
)

this_mod = sys.modules[__name__]
assert this_mod.regular == "hi"
assert not hasattr(this_mod, "dep1")

__deprecated_attributes__ = {
"dep1": _deprecate.DeprecatedAttribute("value1", "1.1", issue=1),
"dep2": _deprecate.DeprecatedAttribute(
"value2",
"1.2",
issue=1,
instead="instead-string",
),
}
assert "dep1" not in globals()
3 changes: 3 additions & 0 deletions src/trio/_tests/test_deprecate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import inspect
import warnings
from types import ModuleType

import pytest

Expand Down Expand Up @@ -244,6 +245,8 @@ def test_module_with_deprecations(recwarn_always: pytest.WarningsRecorder) -> No
assert module_with_deprecations.regular == "hi"
assert len(recwarn_always) == 0

assert type(module_with_deprecations) is ModuleType

filename, lineno = _here()
assert module_with_deprecations.dep1 == "value1" # type: ignore[attr-defined]
got = recwarn_always.pop(DeprecationWarning)
Expand Down