Skip to content

Commit

Permalink
Some post-rebase fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
FasterSpeeding committed Aug 14, 2022
1 parent c862e88 commit ce5c618
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 90 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", "typing-extensions>=4.2.0"]
dependencies = ["alluka~=0.1", "hikari~=2.0.0.dev110", "typing-extensions>=4.2.0"]
dynamic = ["description", "version"]

[project.urls]
Expand Down
33 changes: 18 additions & 15 deletions tanjun/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"Hooks",
"ListenerCallbackSig",
"MenuCommand",
"MenuCommandCallbackSig",
"MenuCallbackSig",
"MenuContext",
"MenuHooks",
"MessageCommand",
Expand Down Expand Up @@ -95,11 +95,9 @@
_P = typing_extensions.ParamSpec("_P")
_T = typing.TypeVar("_T")
_AppCommandContextT = typing.TypeVar("_AppCommandContextT", bound="AppCommandContext")
_CommandCallbackSigT = typing.TypeVar("_CommandCallbackSigT", bound="CommandCallbackSig")
_ContextT_co = typing.TypeVar("_ContextT_co", covariant=True, bound="Context")
_ContextT_contra = typing.TypeVar("_ContextT_contra", bound="Context", contravariant=True)
_CoroT = collections.Coroutine[typing.Any, typing.Any, _T]
_MenuCommandCallbackSigT = typing.TypeVar("_MenuCommandCallbackSigT", bound="MenuCommandCallbackSig")
_MenuTypeT = typing.TypeVar(
"_MenuTypeT", typing.Literal[hikari.CommandType.USER], typing.Literal[hikari.CommandType.MESSAGE]
)
Expand Down Expand Up @@ -147,8 +145,13 @@
[hikari.messages.Message][] dependent on the type(s) of menu this is.
"""

_MenuCallbackSigT = typing.TypeVar("_MenuCallbackSigT", bound="MenuCallbackSig[typing.Any]")

MessageCallbackSig = _CommandCallbackSig["MessageContext", ...]
_MessageCallbackSigT = typing.TypeVar("_MessageCallbackSigT", bound=MessageCallbackSig)

SlashCallbackSig = _CommandCallbackSig["SlashContext", ...]
_SlashCallbackSigT = typing.TypeVar("_SlashCallbackSigT", bound=SlashCallbackSig)

CommandCallbackSig = _CommandCallbackSig["Context", ...]
"""Type hint of the callback a callable [tanjun.abc.ExecutableCommand][] instance will operate on.
Expand Down Expand Up @@ -2078,7 +2081,7 @@ def copy(self: _T) -> _T:
raise NotImplementedError

@abc.abstractmethod
def add_on_error(self: _T, callback: ErrorHookSig, /) -> _T:
def add_on_error(self: _T, callback: ErrorHookSig[_ContextT_contra], /) -> _T:
"""Add an error callback to this hook object.
!!! note
Expand Down Expand Up @@ -2147,7 +2150,7 @@ async def on_error(ctx: tanjun.abc.Context, error: Exception) -> bool:
"""

@abc.abstractmethod
def add_on_parser_error(self: _T, callback: HookSig, /) -> _T:
def add_on_parser_error(self: _T, callback: HookSig[_ContextT_contra], /) -> _T:
"""Add a parser error callback to this hook object.
Parameters
Expand Down Expand Up @@ -2198,7 +2201,7 @@ async def on_parser_error(ctx: tanjun.abc.Context, error: tanjun.ParserError) ->
"""

@abc.abstractmethod
def add_post_execution(self: _T, callback: HookSig, /) -> _T:
def add_post_execution(self: _T, callback: HookSig[_ContextT_contra], /) -> _T:
"""Add a post-execution callback to this hook object.
Parameters
Expand Down Expand Up @@ -2246,7 +2249,7 @@ async def post_execution(ctx: tanjun.abc.Context) -> None:
"""

@abc.abstractmethod
def add_pre_execution(self: _T, callback: HookSig, /) -> _T:
def add_pre_execution(self: _T, callback: HookSig[_ContextT_contra], /) -> _T:
"""Add a pre-execution callback for this hook object.
Parameters
Expand Down Expand Up @@ -2294,7 +2297,7 @@ async def pre_execution(ctx: tanjun.abc.Context) -> None:
"""

@abc.abstractmethod
def add_on_success(self: _T, callback: HookSig, /) -> _T:
def add_on_success(self: _T, callback: HookSig[_ContextT_contra], /) -> _T:
"""Add a success callback to this hook object.
Parameters
Expand Down Expand Up @@ -2715,14 +2718,14 @@ async def execute_autocomplete(
...


class SlashCommand(BaseSlashCommand, abc.ABC, typing.Generic[_CommandCallbackSigT]):
class SlashCommand(BaseSlashCommand, abc.ABC, typing.Generic[_SlashCallbackSigT]):
"""A command that can be executed in a slash context."""

__slots__ = ()

@property
@abc.abstractmethod
def callback(self) -> _CommandCallbackSigT:
def callback(self) -> _SlashCallbackSigT:
"""Callback which is called during execution."""

@property
Expand All @@ -2741,14 +2744,14 @@ def str_autocompletes(self) -> collections.Mapping[str, AutocompleteCallbackSig]
"""Collection of the string option autocompletes."""


class MenuCommand(AppCommand[MenuContext], typing.Generic[_MenuCommandCallbackSigT, _MenuTypeT]):
class MenuCommand(AppCommand[MenuContext], typing.Generic[_MenuCallbackSigT, _MenuTypeT]):
"""A contextmenu command."""

__slots__ = ()

@property
@abc.abstractmethod
def callback(self) -> _MenuCommandCallbackSigT:
def callback(self) -> _MenuCallbackSigT:
"""Callback which is called during execution."""

@property
Expand Down Expand Up @@ -2912,14 +2915,14 @@ def validate_arg_keys(self, callback_name: str, names: collections.Container[str
"""


class MessageCommand(ExecutableCommand[MessageContext], abc.ABC, typing.Generic[_CommandCallbackSigT]):
class MessageCommand(ExecutableCommand[MessageContext], abc.ABC, typing.Generic[_MessageCallbackSigT]):
"""Standard interface of a message command."""

__slots__ = ()

@property
@abc.abstractmethod
def callback(self) -> _CommandCallbackSigT:
def callback(self) -> _MessageCallbackSigT:
"""Callback which is called during execution.
!!! note
Expand Down Expand Up @@ -3004,7 +3007,7 @@ async def execute(
raise NotImplementedError


class MessageCommandGroup(MessageCommand[_CommandCallbackSigT], abc.ABC):
class MessageCommandGroup(MessageCommand[_MessageCallbackSigT], abc.ABC):
"""Standard interface of a message command group."""

__slots__ = ()
Expand Down
70 changes: 40 additions & 30 deletions tanjun/commands/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

import typing

import hikari

from .. import abc as tanjun
from .. import components
from .. import errors
Expand All @@ -46,23 +48,38 @@
if typing.TYPE_CHECKING:
from collections import abc as collections

_CommandT = typing.Union[
tanjun.MenuCommand["_MenuCommandCallbackSigT", typing.Any],
tanjun.MessageCommand["_MenuCommandCallbackSigT"],
tanjun.SlashCommand["_MenuCommandCallbackSigT"],
_AnyCallbackSigT = typing.TypeVar("_CommandCallbackSigT", bound="tanjun.CommandCallbackSig")
_AnyCommandT = typing.Union[
tanjun.MenuCommand["_AnyCallbackSigT", typing.Any],
tanjun.MessageCommand["_AnyCallbackSigT"],
tanjun.SlashCommand["_AnyCallbackSigT"],
]
_CallbackishT = typing.Union["_MenuCommandCallbackSigT", _CommandT["_MenuCommandCallbackSigT"]]
_CallbackishT = typing.Union["_AnyCallbackSigT", _AnyCommandT["_AnyCallbackSigT"]]
_MenuCommandT = typing.TypeVar("_MenuCommandT", bound="MenuCommand[typing.Any, typing.Any]")

import hikari

_MenuCommandCallbackSigT = typing.TypeVar("_MenuCommandCallbackSigT", bound="tanjun.MenuCommandCallbackSig")
_MenuCommandCallbackSigT = typing.TypeVar("_MenuCommandCallbackSigT", bound="tanjun.MenuCallbackSig[typing.Any]")
_MenuTypeT = typing.TypeVar(
"_MenuTypeT", typing.Literal[hikari.CommandType.USER], typing.Literal[hikari.CommandType.MESSAGE]
)
_EMPTY_HOOKS: typing.Final[hooks_.Hooks[typing.Any]] = hooks_.Hooks()


class _ResultProto(typing.Protocol[_MenuTypeT]):
@typing.overload
def __call__(self, _: _AnyCommandT[_AnyCallbackSigT], /) -> MenuCommand[_AnyCallbackSigT, _MenuTypeT]:
...

@typing.overload
def __call__(self, _: _MenuCommandCallbackSigT, /) -> MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]:
...

def __call__(
self, _: _CallbackishT[_AnyCallbackSigT], /
) -> MenuCommand[_AnyCallbackSigT, _MenuTypeT]:
raise NotImplementedError


def _as_menu(
name: str,
type_: _MenuTypeT,
Expand All @@ -72,9 +89,17 @@ def _as_menu(
dm_enabled: typing.Optional[bool] = None,
is_global: bool = True,
) -> _ResultProto[_MenuTypeT]:
@typing.overload
def decorator(callback: _AnyCommandT[_AnyCallbackSigT], /) -> MenuCommand[_AnyCallbackSigT, _MenuTypeT]:
...

@typing.overload
def decorator(callback: _MenuCommandCallbackSigT, /) -> MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]:
...

def decorator(
callback: _CallbackishT[_MenuCommandCallbackSigT], /
) -> MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]:
callback: _CallbackishT[_AnyCallbackSigT], /
) -> MenuCommand[_AnyCallbackSigT, _MenuTypeT]:
if isinstance(callback, (tanjun.MenuCommand, tanjun.MessageCommand, tanjun.SlashCommand)):
wrapped_command = callback
callback = callback.callback
Expand All @@ -97,21 +122,6 @@ def decorator(
return decorator


class _ResultProto(typing.Protocol[_MenuTypeT]):
@typing.overload
def __call__(self, _: _CommandT[_MenuCommandCallbackSigT], /) -> MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]:
...

@typing.overload
def __call__(self, _: _MenuCommandCallbackSigT, /) -> MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]:
...

def __call__(
self, _: _CallbackishT[_MenuCommandCallbackSigT], /
) -> MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]:
raise NotImplementedError


def as_message_menu(
name: str,
/,
Expand Down Expand Up @@ -313,9 +323,9 @@ class MenuCommand(base.PartialCommand[tanjun.MenuContext], tanjun.MenuCommand[_M

@typing.overload
def __init__(
self,
callback: _CommandT[
_MenuCommandCallbackSigT,
self: MenuCommand[_AnyCallbackSigT,_MenuTypeT],
callback: _AnyCommandT[
_AnyCallbackSigT,
],
type_: _MenuTypeT,
name: str,
Expand Down Expand Up @@ -348,8 +358,8 @@ def __init__(
...

def __init__(
self,
callback: _CallbackishT[_MenuCommandCallbackSigT],
self: MenuCommand[_AnyCallbackSigT,_MenuTypeT],
callback: _CallbackishT[_AnyCallbackSigT],
type_: _MenuTypeT,
name: str,
/,
Expand Down Expand Up @@ -444,7 +454,7 @@ def __init__(
default_member_permissions = hikari.Permissions(default_member_permissions)

self._always_defer = always_defer
self._callback = callback
self._callback: _MenuCommandCallbackSigT = callback
self._default_member_permissions = default_member_permissions
self._defaults_to_ephemeral = default_to_ephemeral
self._is_dm_enabled = dm_enabled
Expand Down
Loading

0 comments on commit ce5c618

Please sign in to comment.