Skip to content

Commit 1d70dec

Browse files
committed
♻️ rename maybe_coroutine to maybe_awaitable
1 parent cc8f27f commit 1d70dec

File tree

7 files changed

+23
-22
lines changed

7 files changed

+23
-22
lines changed

discord/commands/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
from ..threads import Thread
7070
from ..user import User
7171
from ..utils import MISSING, find, utcnow
72-
from ..utils.private import warn_deprecated, async_all, maybe_coroutine
72+
from ..utils.private import warn_deprecated, async_all, maybe_awaitable
7373
from .context import ApplicationContext, AutocompleteContext
7474
from .options import Option, OptionChoice
7575

@@ -433,7 +433,7 @@ async def can_run(self, ctx: ApplicationContext) -> bool:
433433
if cog is not None:
434434
local_check = cog._get_overridden_method(cog.cog_check)
435435
if local_check is not None:
436-
ret = await maybe_coroutine(local_check, ctx)
436+
ret = await maybe_awaitable(local_check, ctx)
437437
if not ret:
438438
return False
439439

discord/ext/commands/bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ async def get_prefix(self, message: Message) -> list[str] | str:
224224
"""
225225
prefix = ret = self.command_prefix
226226
if callable(prefix):
227-
ret = await discord.utils.private.maybe_coroutine(prefix, self, message)
227+
ret = await discord.utils.private.maybe_awaitable(prefix, self, message)
228228

229229
if not isinstance(ret, str):
230230
try:

discord/ext/commands/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ async def can_run(self, ctx: Context) -> bool:
11471147
if cog is not None:
11481148
local_check = Cog._get_overridden_method(cog.cog_check)
11491149
if local_check is not None:
1150-
ret = await discord.utils.private.maybe_coroutine(local_check, ctx)
1150+
ret = await discord.utils.private.maybe_awaitable(local_check, ctx)
11511151
if not ret:
11521152
return False
11531153

discord/ext/commands/flags.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from discord import utils
3535
from ...utils import MISSING, Undefined
36-
from ...utils.private import resolve_annotation, maybe_coroutine
36+
from ...utils.private import resolve_annotation, maybe_awaitable
3737

3838
from .converter import run_converters
3939
from .errors import (
@@ -486,7 +486,7 @@ async def _construct_default(cls: type[F], ctx: Context) -> F:
486486
flags = cls.__commands_flags__
487487
for flag in flags.values():
488488
if callable(flag.default):
489-
default = await maybe_coroutine(flag.default, ctx)
489+
default = await maybe_awaitable(flag.default, ctx)
490490
setattr(self, flag.attribute, default)
491491
else:
492492
setattr(self, flag.attribute, flag.default)
@@ -585,7 +585,7 @@ async def convert(cls: type[F], ctx: Context, argument: str) -> F:
585585
raise MissingRequiredFlag(flag)
586586
else:
587587
if callable(flag.default):
588-
default = await maybe_coroutine(flag.default, ctx)
588+
default = await maybe_awaitable(flag.default, ctx)
589589
setattr(self, flag.attribute, default)
590590
else:
591591
setattr(self, flag.attribute, flag.default)

discord/ext/commands/help.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from typing import TYPE_CHECKING, Any
3333

3434
from ... import utils
35-
from ...utils.private import string_width, maybe_coroutine
35+
from ...utils.private import string_width, maybe_awaitable
3636

3737
from .core import Command, Group
3838
from .errors import CommandError
@@ -868,18 +868,18 @@ async def command_callback(self, ctx, *, command=None):
868868
keys = command.split(" ")
869869
cmd = bot.all_commands.get(keys[0])
870870
if cmd is None:
871-
string = await maybe_coroutine(self.command_not_found, self.remove_mentions(keys[0]))
871+
string = await maybe_awaitable(self.command_not_found, self.remove_mentions(keys[0]))
872872
return await self.send_error_message(string)
873873

874874
for key in keys[1:]:
875875
try:
876876
found = cmd.all_commands.get(key)
877877
except AttributeError:
878-
string = await maybe_coroutine(self.subcommand_not_found, cmd, self.remove_mentions(key))
878+
string = await maybe_awaitable(self.subcommand_not_found, cmd, self.remove_mentions(key))
879879
return await self.send_error_message(string)
880880
else:
881881
if found is None:
882-
string = await maybe_coroutine(self.subcommand_not_found, cmd, self.remove_mentions(key))
882+
string = await maybe_awaitable(self.subcommand_not_found, cmd, self.remove_mentions(key))
883883
return await self.send_error_message(string)
884884
cmd = found
885885

discord/iterators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from .errors import NoMoreItems
4343
from .object import Object
4444
from .utils import generate_snowflake, snowflake_time
45-
from .utils.private import maybe_coroutine
45+
from .utils.private import maybe_awaitable
4646

4747
__all__ = (
4848
"ReactionIterator",
@@ -106,7 +106,7 @@ async def find(self, predicate: _Func[T, bool]) -> T | None:
106106
except NoMoreItems:
107107
return None
108108

109-
ret = await maybe_coroutine(predicate, elem)
109+
ret = await maybe_awaitable(predicate, elem)
110110
if ret:
111111
return elem
112112

@@ -164,7 +164,7 @@ def __init__(self, iterator, func):
164164
async def next(self) -> T:
165165
# this raises NoMoreItems and will propagate appropriately
166166
item = await self.iterator.next()
167-
return await maybe_coroutine(self.func, item)
167+
return await maybe_awaitable(self.func, item)
168168

169169

170170
class _FilteredAsyncIterator(_AsyncIterator[T]):
@@ -182,7 +182,7 @@ async def next(self) -> T:
182182
while True:
183183
# propagate NoMoreItems similar to _MappedAsyncIterator
184184
item = await getter()
185-
ret = await maybe_coroutine(pred, item)
185+
ret = await maybe_awaitable(pred, item)
186186
if ret:
187187
return item
188188

discord/utils/private.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
ForwardRef,
2323
Union,
2424
Coroutine,
25+
Awaitable,
26+
reveal_type,
2527
)
2628

2729
from ..errors import InvalidArgument, HTTPException
@@ -32,6 +34,9 @@
3234

3335
_IS_ASCII = re.compile(r"^[\x00-\x7f]+$")
3436

37+
P = ParamSpec("P")
38+
T = TypeVar("T")
39+
3540

3641
def resolve_invite(invite: Invite | str) -> str:
3742
"""
@@ -223,10 +228,6 @@ def warn_deprecated(
223228
warnings.simplefilter("default", DeprecationWarning) # reset filter
224229

225230

226-
P = ParamSpec("P")
227-
T = TypeVar("T")
228-
229-
230231
def deprecated(
231232
instead: str | None = None,
232233
since: str | None = None,
@@ -394,9 +395,9 @@ async def async_all(gen: Iterable[Any]) -> bool:
394395
return True
395396

396397

397-
async def maybe_coroutine(f, *args, **kwargs):
398+
async def maybe_awaitable(f: Callable[P, T | Awaitable[T]], *args: P.args, **kwargs: P.kwargs) -> T:
398399
value = f(*args, **kwargs)
399400
if isawaitable(value):
401+
reveal_type(f)
400402
return await value
401-
else:
402-
return value
403+
return value

0 commit comments

Comments
 (0)