Skip to content

Commit

Permalink
Initial restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
FasterSpeeding committed Jul 30, 2022
1 parent 31604de commit 5cb81dc
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 67 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ classifiers = [
"Topic :: Utilities",
"Typing :: Typed"
]
dependencies = ["alluka~=0.1", "hikari~=2.0.0.dev109"]
dependencies = ["alluka~=0.1", "hikari~=2.0.0.dev109", "typing-extensions>=4.2.0"]
dynamic = ["description", "version"]

[project.urls]
Expand Down
62 changes: 41 additions & 21 deletions tanjun/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
from collections import abc as collections

import hikari
import typing_extensions
from alluka import abc as alluka

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

_P = typing_extensions.ParamSpec("_P")
_T = typing.TypeVar("_T")
_AppCommandContextT = typing.TypeVar("_AppCommandContextT", bound="AppCommandContext")
_CommandCallbackSigT = typing.TypeVar("_CommandCallbackSigT", bound="CommandCallbackSig")
Expand All @@ -102,8 +104,10 @@
"_MenuTypeT", typing.Literal[hikari.CommandType.USER], typing.Literal[hikari.CommandType.MESSAGE]
)

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

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


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

CommandCallbackSig = collections.Callable[..., _CoroT[None]]
AnyCheckSig = _CheckSig["Context", ...]

MessageCheckSig = _CheckSig["MessageContext", ...]

SlashCheckSig = _CheckSig["SlashContext", ...]


_CommandCallbackSig = collections.Callable[typing_extensions.Concatenate[_ContextT_contra, _P], None]

_MenuValueT = typing.TypeVar("_MenuValueT", hikari.User, hikari.InteractionMember)
_ManuCallbackSig = collections.Callable[typing_extensions.Concatenate[_ContextT_contra, _MenuValueT, _P], None]
MenuCallbackSig = _ManuCallbackSig["MenuContext", _MenuValueT, ...]
"""Type hint of a context menu command callback.
This is guaranteed two positional; arguments of type [tanjun.abc.MenuContext][]
and either `hikari.User | hikari.InteractionMember` and/or
[hikari.messages.Message][] dependent on the type(s) of menu this is.
"""

MessageCallbackSig = _CommandCallbackSig["MessageContext", ...]
SlashCallbackSig = _CommandCallbackSig["SlashContext", ...]

CommandCallbackSig = _CommandCallbackSig["Context", ...]
"""Type hint of the callback a callable [tanjun.abc.ExecutableCommand][] instance will operate on.
This will be called when executing a command and will need to take one
Expand All @@ -136,10 +162,9 @@
This will have to be asynchronous.
"""

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

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

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

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

ListenerCallbackSig = collections.Callable[..., _CoroT[None]]
_ListenerCallbackSig = collections.Callable[typing_extensions.Concatenate[Exception, _P], _CoroT[None]]

ListenerCallbackSig = _ListenerCallbackSig[...]
"""Type hint of a hikari event manager callback.
This is guaranteed one positional arg of type [hikari.events.base_events.Event][]
regardless of implementation and must be a coruotine function which returns [None][].
"""

MenuCommandCallbackSig = collections.Callable[..., _CoroT[None]]
"""Type hint of a context menu command callback.
This is guaranteed two positional; arguments of type [tanjun.abc.MenuContext][]
and either `hikari.User | hikari.InteractionMember` and/or
[hikari.messages.Message][] dependent on the type(s) of menu this is.
"""

MetaEventSig = typing.Union[collections.Callable[..., _CoroT[None]], collections.Callable[..., None]]
MetaEventSig = _MaybeAwaitable[..., None]
"""Type hint of a client callback.
The positional arguments this is guaranteed depend on the event name its being
Expand Down Expand Up @@ -2383,7 +2403,7 @@ class ExecutableCommand(abc.ABC, typing.Generic[_ContextT_co]):

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

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

@abc.abstractmethod
def add_check(self: _T, check: CheckSig, /) -> _T: # TODO: remove or add with_check?
def add_check(self: _T, check: CheckSig[_ContextT_co], /) -> _T: # TODO: remove or add with_check?
"""Add a check to the command.
Parameters
Expand All @@ -2455,7 +2475,7 @@ def add_check(self: _T, check: CheckSig, /) -> _T: # TODO: remove or add with_c
"""

@abc.abstractmethod
def remove_check(self: _T, check: CheckSig, /) -> _T:
def remove_check(self: _T, check: CheckSig[_ContextT_co], /) -> _T:
"""Remove a check from the command.
Parameters
Expand Down
2 changes: 1 addition & 1 deletion tanjun/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@


def _optional_kwargs(
command: typing.Optional[_CommandT], check: tanjun.CheckSig, /
command: typing.Optional[_CommandT], check: tanjun.AnyCheckSig, /
) -> typing.Union[_CommandT, collections.Callable[[_CommandT], _CommandT]]:
if command:
return command.add_check(check)
Expand Down
10 changes: 5 additions & 5 deletions tanjun/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
if typing.TYPE_CHECKING:
import types

_CheckSigT = typing.TypeVar("_CheckSigT", bound=tanjun.CheckSig)
_CheckSigT = typing.TypeVar("_CheckSigT", bound=tanjun.AnyCheckSig)
_ClientT = typing.TypeVar("_ClientT", bound="Client")
_ListenerCallbackSigT = typing.TypeVar("_ListenerCallbackSigT", bound=tanjun.ListenerCallbackSig)
_MetaEventSigT = typing.TypeVar("_MetaEventSigT", bound=tanjun.MetaEventSig)
Expand Down Expand Up @@ -670,7 +670,7 @@ def __init__(
self._auto_defer_after: typing.Optional[float] = 2.0
self._cache = cache
self._cached_application_id: typing.Optional[hikari.Snowflake] = None
self._checks: list[tanjun.CheckSig] = []
self._checks: list[tanjun.AnyCheckSig] = []
self._client_callbacks: dict[str, list[tanjun.MetaEventSig]] = {}
self._components: dict[str, tanjun.Component] = {}
self._defaults_to_ephemeral: bool = False
Expand Down Expand Up @@ -1026,7 +1026,7 @@ def cache(self) -> typing.Optional[hikari.api.Cache]:
return self._cache

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

return self

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

return self

def remove_check(self: _ClientT, check: tanjun.CheckSig, /) -> _ClientT:
def remove_check(self: _ClientT, check: tanjun.AnyCheckSig, /) -> _ClientT:
"""Remove a check from the client.
Parameters
Expand Down
10 changes: 5 additions & 5 deletions tanjun/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from .. import components

if typing.TYPE_CHECKING:
_CheckSigT = typing.TypeVar("_CheckSigT", bound=tanjun.CheckSig)
_CheckSigT = typing.TypeVar("_CheckSigT", bound=tanjun.AnyCheckSig)
_PartialCommandT = typing.TypeVar("_PartialCommandT", bound="PartialCommand[typing.Any]")


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

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

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

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

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

return self

def remove_check(self: _PartialCommandT, check: tanjun.CheckSig, /) -> _PartialCommandT:
def remove_check(self: _PartialCommandT, check: tanjun.AnyCheckSig, /) -> _PartialCommandT:
# <<inherited docstring from tanjun.abc.ExecutableCommand>>.
self._checks.remove(check)
return self
Expand Down
Loading

0 comments on commit 5cb81dc

Please sign in to comment.