Skip to content

Commit 1893d85

Browse files
committed
Use sphinx style docstrings for new lock utils
1 parent 472fb1e commit 1893d85

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

pydis_core/utils/function.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
import typing
99
from collections.abc import Callable, Sequence, Set
1010

11-
__all__ = ["command_wraps", "GlobalNameConflictError", "update_wrapper_globals"]
11+
__all__ = [
12+
"command_wraps",
13+
"get_arg_value",
14+
"get_arg_value_wrapper",
15+
"get_bound_args",
16+
"GlobalNameConflictError",
17+
"update_wrapper_globals",
18+
]
1219

1320

1421
if typing.TYPE_CHECKING:
@@ -29,10 +36,13 @@ def get_arg_value(name_or_pos: Argument, arguments: BoundArgs) -> typing.Any:
2936
"""
3037
Return a value from `arguments` based on a name or position.
3138
32-
`arguments` is an ordered mapping of parameter names to argument values.
33-
34-
Raise TypeError if `name_or_pos` isn't a str or int.
35-
Raise ValueError if `name_or_pos` does not match any argument.
39+
Arguments:
40+
arguments: An ordered mapping of parameter names to argument values.
41+
Returns:
42+
Value from `arguments` based on a name or position.
43+
Raises:
44+
TypeError: `name_or_pos` isn't a str or int.
45+
ValueError: `name_or_pos` does not match any argument.
3646
"""
3747
if isinstance(name_or_pos, int):
3848
# Convert arguments to a tuple to make them indexable.
@@ -62,12 +72,14 @@ def get_arg_value_wrapper(
6272
"""
6373
Call `decorator_func` with the value of the arg at the given name/position.
6474
65-
`decorator_func` must accept a callable as a parameter to which it will pass a mapping of
66-
parameter names to argument values of the function it's decorating.
75+
Arguments:
76+
decorator_func: A function that must accept a callable as a parameter to which it will pass a mapping of
77+
parameter names to argument values of the function it's decorating.
78+
name_or_pos: The name/position of the arg to get the value from.
79+
func: An optional callable which will return a new value given the argument's value.
6780
68-
`func` is an optional callable which will return a new value given the argument's value.
69-
70-
Return the decorator returned by `decorator_func`.
81+
Returns:
82+
The decorator returned by `decorator_func`.
7183
"""
7284
def wrapper(args: BoundArgs) -> typing.Any:
7385
value = get_arg_value(name_or_pos, args)

pydis_core/utils/lock.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class LockedResourceError(RuntimeError):
2323
Exception raised when an operation is attempted on a locked resource.
2424
2525
Attributes:
26-
`type` -- name of the locked resource's type
27-
`id` -- ID of the locked resource
26+
type (str): Name of the locked resource's type
27+
id (typing.Hashable): ID of the locked resource
2828
"""
2929

3030
def __init__(self, resource_type: str, resource_id: Hashable):
@@ -76,19 +76,20 @@ def lock(
7676
"""
7777
Turn the decorated coroutine function into a mutually exclusive operation on a `resource_id`.
7878
79-
If `wait` is True, wait until the lock becomes available. Otherwise, if any other mutually
80-
exclusive function currently holds the lock for a resource, do not run the decorated function
81-
and return None.
82-
83-
If `raise_error` is True, raise `LockedResourceError` if the lock cannot be acquired.
84-
85-
`namespace` is an identifier used to prevent collisions among resource IDs.
86-
87-
`resource_id` identifies a resource on which to perform a mutually exclusive operation.
88-
It may also be a callable or awaitable which will return the resource ID given an ordered
89-
mapping of the parameters' names to arguments' values.
90-
9179
If decorating a command, this decorator must go before (below) the `command` decorator.
80+
81+
Arguments:
82+
namespace (typing.Hashable): An identifier used to prevent collisions among resource IDs.
83+
resource_id: identifies a resource on which to perform a mutually exclusive operation.
84+
It may also be a callable or awaitable which will return the resource ID given an ordered
85+
mapping of the parameters' names to arguments' values.
86+
raise_error (bool): If True, raise `LockedResourceError` if the lock cannot be acquired.
87+
wait (bool): If True, wait until the lock becomes available. Otherwise, if any other mutually
88+
exclusive function currently holds the lock for a resource, do not run the decorated function
89+
and return None.
90+
91+
Raises:
92+
:exc:`LockedResourceError`: If the lock can't be acquired and `raise_error` is set to True.
9293
"""
9394
def decorator(func: types.FunctionType) -> types.FunctionType:
9495
name = func.__name__
@@ -144,8 +145,10 @@ def lock_arg(
144145
"""
145146
Apply the `lock` decorator using the value of the arg at the given name/position as the ID.
146147
147-
`func` is an optional callable or awaitable which will return the ID given the argument value.
148148
See `lock` docs for more information.
149+
150+
Arguments:
151+
func: An optional callable or awaitable which will return the ID given the argument value.
149152
"""
150153
decorator_func = partial(lock, namespace, raise_error=raise_error, wait=wait)
151154
return function.get_arg_value_wrapper(decorator_func, name_or_pos, func)

0 commit comments

Comments
 (0)