mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 20:28:38 +00:00
Update types to use Awaitable where possible
This commit is contained in:
parent
ff24c5229e
commit
06c257760b
@ -23,7 +23,7 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
from typing import Any, Callable, Coroutine, TYPE_CHECKING, TypeVar, Union, Tuple
|
from typing import Any, Awaitable, Callable, Coroutine, TYPE_CHECKING, TypeVar, Union, Tuple
|
||||||
|
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
@ -37,18 +37,16 @@ if TYPE_CHECKING:
|
|||||||
from .errors import CommandError
|
from .errors import CommandError
|
||||||
|
|
||||||
P = ParamSpec('P')
|
P = ParamSpec('P')
|
||||||
MaybeCoroFunc = Union[
|
MaybeAwaitableFunc = Callable[P, 'MaybeAwaitable[T]']
|
||||||
Callable[P, 'Coro[T]'],
|
|
||||||
Callable[P, T],
|
|
||||||
]
|
|
||||||
else:
|
else:
|
||||||
P = TypeVar('P')
|
P = TypeVar('P')
|
||||||
MaybeCoroFunc = Tuple[P, T]
|
MaybeAwaitableFunc = Tuple[P, T]
|
||||||
|
|
||||||
_Bot = Union['Bot', 'AutoShardedBot']
|
_Bot = Union['Bot', 'AutoShardedBot']
|
||||||
Coro = Coroutine[Any, Any, T]
|
Coro = Coroutine[Any, Any, T]
|
||||||
MaybeCoro = Union[T, Coro[T]]
|
|
||||||
CoroFunc = Callable[..., Coro[Any]]
|
CoroFunc = Callable[..., Coro[Any]]
|
||||||
|
MaybeCoro = Union[T, Coro[T]]
|
||||||
|
MaybeAwaitable = Union[T, Awaitable[T]]
|
||||||
|
|
||||||
Check = Union[Callable[["Cog", "ContextT"], MaybeCoro[bool]], Callable[["ContextT"], MaybeCoro[bool]]]
|
Check = Union[Callable[["Cog", "ContextT"], MaybeCoro[bool]], Callable[["ContextT"], MaybeCoro[bool]]]
|
||||||
Hook = Union[Callable[["Cog", "ContextT"], Coro[Any]], Callable[["ContextT"], Coro[Any]]]
|
Hook = Union[Callable[["Cog", "ContextT"], Coro[Any]], Callable[["ContextT"], Coro[Any]]]
|
||||||
|
@ -74,11 +74,11 @@ if TYPE_CHECKING:
|
|||||||
Check,
|
Check,
|
||||||
CoroFunc,
|
CoroFunc,
|
||||||
ContextT,
|
ContextT,
|
||||||
MaybeCoroFunc,
|
MaybeAwaitableFunc,
|
||||||
)
|
)
|
||||||
|
|
||||||
_Prefix = Union[Iterable[str], str]
|
_Prefix = Union[Iterable[str], str]
|
||||||
_PrefixCallable = MaybeCoroFunc[[BotT, Message], _Prefix]
|
_PrefixCallable = MaybeAwaitableFunc[[BotT, Message], _Prefix]
|
||||||
PrefixType = Union[_Prefix, _PrefixCallable[BotT]]
|
PrefixType = Union[_Prefix, _PrefixCallable[BotT]]
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
@ -31,6 +31,7 @@ import re
|
|||||||
|
|
||||||
from typing import (
|
from typing import (
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
|
Awaitable,
|
||||||
Optional,
|
Optional,
|
||||||
Generator,
|
Generator,
|
||||||
Generic,
|
Generic,
|
||||||
@ -54,7 +55,6 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
import discord.abc
|
import discord.abc
|
||||||
|
|
||||||
from ._types import Coro
|
|
||||||
from .bot import BotBase
|
from .bot import BotBase
|
||||||
from .cog import Cog
|
from .cog import Cog
|
||||||
from .context import Context
|
from .context import Context
|
||||||
@ -295,7 +295,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
|
|||||||
bot.remove_command(self.name)
|
bot.remove_command(self.name)
|
||||||
self._eject_cog()
|
self._eject_cog()
|
||||||
|
|
||||||
async def _call_without_cog(self, callback: Callable[[ContextT], Coro[T]], ctx: ContextT) -> T:
|
async def _call_without_cog(self, callback: Callable[[ContextT], Awaitable[T]], ctx: ContextT) -> T:
|
||||||
cog = self._cog
|
cog = self._cog
|
||||||
self.cog = None
|
self.cog = None
|
||||||
try:
|
try:
|
||||||
|
@ -28,7 +28,7 @@ import datetime
|
|||||||
import inspect
|
import inspect
|
||||||
import itertools
|
import itertools
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
from typing import Any, Callable, Collection, Coroutine, Dict, List, Optional, TYPE_CHECKING, Tuple, Union, Type
|
from typing import Any, Awaitable, Callable, Collection, Dict, List, Optional, TYPE_CHECKING, Tuple, Union, Type
|
||||||
|
|
||||||
import discord.abc
|
import discord.abc
|
||||||
|
|
||||||
@ -331,7 +331,7 @@ class Member(discord.abc.Messageable, _UserTag):
|
|||||||
default_avatar: Asset
|
default_avatar: Asset
|
||||||
avatar: Optional[Asset]
|
avatar: Optional[Asset]
|
||||||
dm_channel: Optional[DMChannel]
|
dm_channel: Optional[DMChannel]
|
||||||
create_dm: Callable[[], Coroutine[Any, Any, DMChannel]]
|
create_dm: Callable[[], Awaitable[DMChannel]]
|
||||||
mutual_guilds: List[Guild]
|
mutual_guilds: List[Guild]
|
||||||
public_flags: PublicUserFlags
|
public_flags: PublicUserFlags
|
||||||
banner: Optional[Asset]
|
banner: Optional[Asset]
|
||||||
|
@ -140,10 +140,7 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
P = ParamSpec('P')
|
P = ParamSpec('P')
|
||||||
|
|
||||||
MaybeCoroFunc = Union[
|
MaybeAwaitableFunc = Callable[P, 'MaybeAwaitable[T]']
|
||||||
Callable[P, Coroutine[Any, Any, 'T']],
|
|
||||||
Callable[P, 'T'],
|
|
||||||
]
|
|
||||||
|
|
||||||
_SnowflakeListBase = array.array[int]
|
_SnowflakeListBase = array.array[int]
|
||||||
|
|
||||||
@ -156,6 +153,7 @@ T = TypeVar('T')
|
|||||||
T_co = TypeVar('T_co', covariant=True)
|
T_co = TypeVar('T_co', covariant=True)
|
||||||
_Iter = Union[Iterable[T], AsyncIterable[T]]
|
_Iter = Union[Iterable[T], AsyncIterable[T]]
|
||||||
Coro = Coroutine[Any, Any, T]
|
Coro = Coroutine[Any, Any, T]
|
||||||
|
MaybeAwaitable = Union[T, Awaitable[T]]
|
||||||
|
|
||||||
|
|
||||||
class CachedSlotProperty(Generic[T, T_co]):
|
class CachedSlotProperty(Generic[T, T_co]):
|
||||||
@ -615,7 +613,7 @@ def _parse_ratelimit_header(request: Any, *, use_clock: bool = False) -> float:
|
|||||||
return float(reset_after)
|
return float(reset_after)
|
||||||
|
|
||||||
|
|
||||||
async def maybe_coroutine(f: MaybeCoroFunc[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
|
async def maybe_coroutine(f: MaybeAwaitableFunc[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
|
||||||
value = f(*args, **kwargs)
|
value = f(*args, **kwargs)
|
||||||
if _isawaitable(value):
|
if _isawaitable(value):
|
||||||
return await value
|
return await value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user