Skip to content

Commit 5cb81dc

Browse files
Initial restructuring
1 parent 31604de commit 5cb81dc

File tree

8 files changed

+90
-67
lines changed

8 files changed

+90
-67
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ classifiers = [
2626
"Topic :: Utilities",
2727
"Typing :: Typed"
2828
]
29-
dependencies = ["alluka~=0.1", "hikari~=2.0.0.dev109"]
29+
dependencies = ["alluka~=0.1", "hikari~=2.0.0.dev109", "typing-extensions>=4.2.0"]
3030
dynamic = ["description", "version"]
3131

3232
[project.urls]

tanjun/abc.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
from collections import abc as collections
7474

7575
import hikari
76+
import typing_extensions
7677
from alluka import abc as alluka
7778

7879
if typing.TYPE_CHECKING:
@@ -91,6 +92,7 @@
9192
_MessageCommandT = typing.TypeVar("_MessageCommandT", bound="MessageCommand[typing.Any]")
9293
_MetaEventSigT = typing.TypeVar("_MetaEventSigT", bound="MetaEventSig")
9394

95+
_P = typing_extensions.ParamSpec("_P")
9496
_T = typing.TypeVar("_T")
9597
_AppCommandContextT = typing.TypeVar("_AppCommandContextT", bound="AppCommandContext")
9698
_CommandCallbackSigT = typing.TypeVar("_CommandCallbackSigT", bound="CommandCallbackSig")
@@ -102,8 +104,10 @@
102104
"_MenuTypeT", typing.Literal[hikari.CommandType.USER], typing.Literal[hikari.CommandType.MESSAGE]
103105
)
104106

107+
_MaybeAwaitable = typing.Union[collections.Callable[_P, _CoroT[_T]], collections.Callable[_P, _T]]
108+
_AutocompleteCallbackSig = collections.Callable[typing_extensions.Concatenate["AutocompleteContext", _P], _CoroT[None]]
105109

106-
AutocompleteCallbackSig = collections.Callable[..., _CoroT[None]]
110+
AutocompleteCallbackSig = _AutocompleteCallbackSig[...]
107111
"""Type hint of the callback an autocomplete callback should have.
108112
109113
This will be called when handling autocomplete and should be an asynchronous
@@ -112,8 +116,8 @@
112116
autocomplete type), returns [None][] and may use dependency injection.
113117
"""
114118

115-
116-
CheckSig = typing.Union[collections.Callable[..., _CoroT[bool]], collections.Callable[..., bool]]
119+
_CheckSig = _MaybeAwaitable[typing_extensions.Concatenate[_ContextT_contra, _P], bool]
120+
CheckSig = _CheckSig[_ContextT_contra, ...]
117121
"""Type hint of a general context check used with Tanjun [tanjun.abc.ExecutableCommand][] classes.
118122
119123
This may be registered with a [tanjun.abc.ExecutableCommand][] to add a rule
@@ -124,7 +128,29 @@
124128
current context shouldn't lead to an execution.
125129
"""
126130

127-
CommandCallbackSig = collections.Callable[..., _CoroT[None]]
131+
AnyCheckSig = _CheckSig["Context", ...]
132+
133+
MessageCheckSig = _CheckSig["MessageContext", ...]
134+
135+
SlashCheckSig = _CheckSig["SlashContext", ...]
136+
137+
138+
_CommandCallbackSig = collections.Callable[typing_extensions.Concatenate[_ContextT_contra, _P], None]
139+
140+
_MenuValueT = typing.TypeVar("_MenuValueT", hikari.User, hikari.InteractionMember)
141+
_ManuCallbackSig = collections.Callable[typing_extensions.Concatenate[_ContextT_contra, _MenuValueT, _P], None]
142+
MenuCallbackSig = _ManuCallbackSig["MenuContext", _MenuValueT, ...]
143+
"""Type hint of a context menu command callback.
144+
145+
This is guaranteed two positional; arguments of type [tanjun.abc.MenuContext][]
146+
and either `hikari.User | hikari.InteractionMember` and/or
147+
[hikari.messages.Message][] dependent on the type(s) of menu this is.
148+
"""
149+
150+
MessageCallbackSig = _CommandCallbackSig["MessageContext", ...]
151+
SlashCallbackSig = _CommandCallbackSig["SlashContext", ...]
152+
153+
CommandCallbackSig = _CommandCallbackSig["Context", ...]
128154
"""Type hint of the callback a callable [tanjun.abc.ExecutableCommand][] instance will operate on.
129155
130156
This will be called when executing a command and will need to take one
@@ -136,10 +162,9 @@
136162
This will have to be asynchronous.
137163
"""
138164

165+
_ErrorHookSig = _MaybeAwaitable[typing_extensions.Concatenate[_ContextT_contra, Exception, _P], typing.Optional[bool]]
139166

140-
ErrorHookSig = typing.Union[
141-
collections.Callable[..., typing.Optional[bool]], collections.Callable[..., _CoroT[typing.Optional[bool]]]
142-
]
167+
ErrorHookSig = _ErrorHookSig[_ContextT_contra, ...]
143168
"""Type hint of the callback used as a unexpected command error hook.
144169
145170
This will be called whenever an unexpected [Exception][] is raised during the
@@ -153,8 +178,9 @@
153178
[False][] is returned to indicate that the exception should be re-raised.
154179
"""
155180

181+
_HookSig = _MaybeAwaitable[typing_extensions.Concatenate[_ContextT_contra, _P], None]
156182

157-
HookSig = typing.Union[collections.Callable[..., None], collections.Callable[..., _CoroT[None]]]
183+
HookSig = _HookSig[_ContextT_contra, ...]
158184
"""Type hint of the callback used as a general command hook.
159185
160186
!!! note
@@ -163,22 +189,16 @@
163189
are passed dependent on the type of hook this is being registered as.
164190
"""
165191

166-
ListenerCallbackSig = collections.Callable[..., _CoroT[None]]
192+
_ListenerCallbackSig = collections.Callable[typing_extensions.Concatenate[Exception, _P], _CoroT[None]]
193+
194+
ListenerCallbackSig = _ListenerCallbackSig[...]
167195
"""Type hint of a hikari event manager callback.
168196
169197
This is guaranteed one positional arg of type [hikari.events.base_events.Event][]
170198
regardless of implementation and must be a coruotine function which returns [None][].
171199
"""
172200

173-
MenuCommandCallbackSig = collections.Callable[..., _CoroT[None]]
174-
"""Type hint of a context menu command callback.
175-
176-
This is guaranteed two positional; arguments of type [tanjun.abc.MenuContext][]
177-
and either `hikari.User | hikari.InteractionMember` and/or
178-
[hikari.messages.Message][] dependent on the type(s) of menu this is.
179-
"""
180-
181-
MetaEventSig = typing.Union[collections.Callable[..., _CoroT[None]], collections.Callable[..., None]]
201+
MetaEventSig = _MaybeAwaitable[..., None]
182202
"""Type hint of a client callback.
183203
184204
The positional arguments this is guaranteed depend on the event name its being
@@ -2383,7 +2403,7 @@ class ExecutableCommand(abc.ABC, typing.Generic[_ContextT_co]):
23832403

23842404
@property
23852405
@abc.abstractmethod
2386-
def checks(self) -> collections.Collection[CheckSig]:
2406+
def checks(self) -> collections.Collection[CheckSig[_ContextT_co]]:
23872407
"""Collection of checks that must be met before the command can be executed."""
23882408

23892409
@property
@@ -2440,7 +2460,7 @@ def set_hooks(self: _T, hooks: typing.Optional[Hooks[_ContextT_co]], /) -> _T:
24402460
"""
24412461

24422462
@abc.abstractmethod
2443-
def add_check(self: _T, check: CheckSig, /) -> _T: # TODO: remove or add with_check?
2463+
def add_check(self: _T, check: CheckSig[_ContextT_co], /) -> _T: # TODO: remove or add with_check?
24442464
"""Add a check to the command.
24452465
24462466
Parameters
@@ -2455,7 +2475,7 @@ def add_check(self: _T, check: CheckSig, /) -> _T: # TODO: remove or add with_c
24552475
"""
24562476

24572477
@abc.abstractmethod
2458-
def remove_check(self: _T, check: CheckSig, /) -> _T:
2478+
def remove_check(self: _T, check: CheckSig[_ContextT_co], /) -> _T:
24592479
"""Remove a check from the command.
24602480
24612481
Parameters

tanjun/checks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373

7474
def _optional_kwargs(
75-
command: typing.Optional[_CommandT], check: tanjun.CheckSig, /
75+
command: typing.Optional[_CommandT], check: tanjun.AnyCheckSig, /
7676
) -> typing.Union[_CommandT, collections.Callable[[_CommandT], _CommandT]]:
7777
if command:
7878
return command.add_check(check)

tanjun/clients.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
if typing.TYPE_CHECKING:
7070
import types
7171

72-
_CheckSigT = typing.TypeVar("_CheckSigT", bound=tanjun.CheckSig)
72+
_CheckSigT = typing.TypeVar("_CheckSigT", bound=tanjun.AnyCheckSig)
7373
_ClientT = typing.TypeVar("_ClientT", bound="Client")
7474
_ListenerCallbackSigT = typing.TypeVar("_ListenerCallbackSigT", bound=tanjun.ListenerCallbackSig)
7575
_MetaEventSigT = typing.TypeVar("_MetaEventSigT", bound=tanjun.MetaEventSig)
@@ -670,7 +670,7 @@ def __init__(
670670
self._auto_defer_after: typing.Optional[float] = 2.0
671671
self._cache = cache
672672
self._cached_application_id: typing.Optional[hikari.Snowflake] = None
673-
self._checks: list[tanjun.CheckSig] = []
673+
self._checks: list[tanjun.AnyCheckSig] = []
674674
self._client_callbacks: dict[str, list[tanjun.MetaEventSig]] = {}
675675
self._components: dict[str, tanjun.Component] = {}
676676
self._defaults_to_ephemeral: bool = False
@@ -1026,7 +1026,7 @@ def cache(self) -> typing.Optional[hikari.api.Cache]:
10261026
return self._cache
10271027

10281028
@property
1029-
def checks(self) -> collections.Collection[tanjun.CheckSig]:
1029+
def checks(self) -> collections.Collection[tanjun.AnyCheckSig]:
10301030
"""Collection of the level [tanjun.abc.Context][] checks registered to this client.
10311031
10321032
!!! note
@@ -1642,7 +1642,7 @@ def set_human_only(self: _ClientT, value: bool = True) -> _ClientT:
16421642

16431643
return self
16441644

1645-
def add_check(self: _ClientT, check: tanjun.CheckSig, /) -> _ClientT:
1645+
def add_check(self: _ClientT, check: tanjun.AnyCheckSig, /) -> _ClientT:
16461646
"""Add a generic check to this client.
16471647
16481648
This will be applied to both message and slash command execution.
@@ -1664,7 +1664,7 @@ def add_check(self: _ClientT, check: tanjun.CheckSig, /) -> _ClientT:
16641664

16651665
return self
16661666

1667-
def remove_check(self: _ClientT, check: tanjun.CheckSig, /) -> _ClientT:
1667+
def remove_check(self: _ClientT, check: tanjun.AnyCheckSig, /) -> _ClientT:
16681668
"""Remove a check from the client.
16691669
16701670
Parameters

tanjun/commands/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from .. import components
4343

4444
if typing.TYPE_CHECKING:
45-
_CheckSigT = typing.TypeVar("_CheckSigT", bound=tanjun.CheckSig)
45+
_CheckSigT = typing.TypeVar("_CheckSigT", bound=tanjun.AnyCheckSig)
4646
_PartialCommandT = typing.TypeVar("_PartialCommandT", bound="PartialCommand[typing.Any]")
4747

4848

@@ -55,13 +55,13 @@ class PartialCommand(tanjun.ExecutableCommand[_ContextT], components.AbstractCom
5555
__slots__ = ("_checks", "_component", "_hooks", "_metadata")
5656

5757
def __init__(self) -> None:
58-
self._checks: list[tanjun.CheckSig] = []
58+
self._checks: list[tanjun.AnyCheckSig] = []
5959
self._component: typing.Optional[tanjun.Component] = None
6060
self._hooks: typing.Optional[tanjun.Hooks[_ContextT]] = None
6161
self._metadata: dict[typing.Any, typing.Any] = {}
6262

6363
@property
64-
def checks(self) -> collections.Collection[tanjun.CheckSig]:
64+
def checks(self) -> collections.Collection[tanjun.AnyCheckSig]:
6565
# <<inherited docstring from tanjun.abc.ExecutableCommand>>.
6666
return self._checks.copy()
6767

@@ -98,14 +98,14 @@ def set_metadata(self: _PartialCommandT, key: typing.Any, value: typing.Any, /)
9898
self._metadata[key] = value
9999
return self
100100

101-
def add_check(self: _PartialCommandT, check: tanjun.CheckSig, /) -> _PartialCommandT:
101+
def add_check(self: _PartialCommandT, check: tanjun.AnyCheckSig, /) -> _PartialCommandT:
102102
# <<inherited docstring from tanjun.abc.ExecutableCommand>>.
103103
if check not in self._checks:
104104
self._checks.append(check)
105105

106106
return self
107107

108-
def remove_check(self: _PartialCommandT, check: tanjun.CheckSig, /) -> _PartialCommandT:
108+
def remove_check(self: _PartialCommandT, check: tanjun.AnyCheckSig, /) -> _PartialCommandT:
109109
# <<inherited docstring from tanjun.abc.ExecutableCommand>>.
110110
self._checks.remove(check)
111111
return self

0 commit comments

Comments
 (0)