Skip to content

Commit e874373

Browse files
committed
docs: add docstrings to deprecation helpers in _deprecate.py
Five public items in `_deprecate.py` had no docstrings at all: `warn_deprecated`, `deprecated`, `deprecated_alias`, `DeprecatedAttribute`, and `deprecate_attributes`. Add Google-style Args/Returns docstrings that match the style used throughout the rest of the codebase (e.g. `_sync.py`, `_highlevel_generic.py`). No logic is changed.
1 parent 94d7159 commit e874373

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

src/trio/_deprecate.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ def warn_deprecated(
6161
stacklevel: int = 2,
6262
use_triodeprecationwarning: bool = False,
6363
) -> None:
64+
"""Issue a deprecation warning for a Trio feature.
65+
66+
Args:
67+
thing: The deprecated object or a string describing it.
68+
version: The Trio version in which the deprecation was introduced
69+
(e.g. ``"0.2.0"``).
70+
issue: The GitHub issue number tracking the deprecation, or None.
71+
instead: The replacement to suggest, or None if there is no replacement.
72+
stacklevel: Passed to :func:`warnings.warn`; controls which call frame
73+
appears in the warning message. The default of ``2`` points at the
74+
immediate caller of :func:`warn_deprecated`.
75+
use_triodeprecationwarning: If True, emit a
76+
:class:`TrioDeprecationWarning` instead of the default
77+
:class:`DeprecationWarning`.
78+
79+
"""
6480
stacklevel += 1
6581
msg = f"{_stringify(thing)} is deprecated since Trio {version}"
6682
if instead is None:
@@ -86,6 +102,33 @@ def deprecated(
86102
instead: object,
87103
use_triodeprecationwarning: bool = False,
88104
) -> Callable[[Callable[ArgsT, RetT]], Callable[ArgsT, RetT]]:
105+
"""Decorator that marks a function as deprecated.
106+
107+
When the decorated function is called, a deprecation warning is issued
108+
via :func:`warn_deprecated`. If the function already has a docstring, a
109+
``.. deprecated::`` Sphinx directive is automatically appended to it.
110+
111+
Args:
112+
version: The Trio version in which the deprecation was introduced
113+
(e.g. ``"0.2.0"``).
114+
thing: The name or object to mention in the warning. Defaults to the
115+
decorated function itself.
116+
issue: The GitHub issue number tracking the deprecation, or None.
117+
instead: The replacement to suggest, or None if there is no replacement.
118+
use_triodeprecationwarning: If True, emit a
119+
:class:`TrioDeprecationWarning` instead of :class:`DeprecationWarning`.
120+
121+
Returns:
122+
A decorator that wraps the target function with deprecation behaviour.
123+
124+
Example::
125+
126+
@deprecated("0.2.0", issue=None, instead=new_function)
127+
def old_function(...):
128+
...
129+
130+
"""
131+
89132
def do_wrap(fn: Callable[ArgsT, RetT]) -> Callable[ArgsT, RetT]:
90133
nonlocal thing
91134

@@ -130,6 +173,24 @@ def deprecated_alias(
130173
*,
131174
issue: int | None,
132175
) -> Callable[ArgsT, RetT]:
176+
"""Create a deprecated wrapper that forwards calls to *new_fn*.
177+
178+
Calling the returned callable is equivalent to calling *new_fn*, but also
179+
issues a deprecation warning attributing the call to *old_qualname*.
180+
181+
Args:
182+
old_qualname: The fully-qualified name of the old (deprecated) callable,
183+
used in the warning message (e.g. ``"trio.old_name"``).
184+
new_fn: The replacement callable that the alias should forward to.
185+
version: The Trio version in which the deprecation was introduced.
186+
issue: The GitHub issue number tracking the deprecation, or None.
187+
188+
Returns:
189+
A wrapper callable that emits a deprecation warning and then delegates
190+
to *new_fn*.
191+
192+
"""
193+
133194
@deprecated(version, issue=issue, instead=new_fn)
134195
@wraps(new_fn, assigned=("__module__", "__annotations__"))
135196
def wrapper(*args: ArgsT.args, **kwargs: ArgsT.kwargs) -> RetT:
@@ -144,6 +205,21 @@ def wrapper(*args: ArgsT.args, **kwargs: ArgsT.kwargs) -> RetT:
144205
@final
145206
@attrs.frozen(slots=False)
146207
class DeprecatedAttribute:
208+
"""Descriptor for a single deprecated module-level attribute.
209+
210+
Pass instances of this class to :func:`deprecate_attributes` to declare
211+
that a module attribute is deprecated. When the attribute is accessed,
212+
:func:`warn_deprecated` is called automatically.
213+
214+
Args:
215+
value: The current value of the attribute to return to callers.
216+
version: The Trio version in which the deprecation was introduced.
217+
issue: The GitHub issue number tracking the deprecation, or None.
218+
instead: The suggested replacement. Defaults to *value* itself when
219+
not provided.
220+
221+
"""
222+
147223
_not_set: ClassVar[object] = object()
148224

149225
value: object
@@ -155,6 +231,22 @@ class DeprecatedAttribute:
155231
def deprecate_attributes(
156232
module_name: str, deprecated_attributes: dict[str, DeprecatedAttribute]
157233
) -> None:
234+
"""Install a ``__getattr__`` hook on *module_name* to warn on deprecated attributes.
235+
236+
After this function is called, accessing any attribute listed in
237+
*deprecated_attributes* on the given module will emit a deprecation
238+
warning via :func:`warn_deprecated` and then return the attribute's value.
239+
Accessing any other undefined attribute raises :exc:`AttributeError` as
240+
normal.
241+
242+
Args:
243+
module_name: The ``__name__`` of the module to patch (pass
244+
``__name__`` from inside the module itself).
245+
deprecated_attributes: A mapping from attribute name to a
246+
:class:`DeprecatedAttribute` instance describing the deprecation.
247+
248+
"""
249+
158250
def __getattr__(name: str) -> object:
159251
if name in deprecated_attributes:
160252
info = deprecated_attributes[name]

0 commit comments

Comments
 (0)