Skip to content

Commit 942b8b4

Browse files
FasterSpeedingpre-commit-ci[bot]always-on-duty[bot]
authored
Improve typing around callbacks with dynamic keyword-arguments (#176)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: always-on-duty[bot] <120557446+always-on-duty[bot]@users.noreply.github.com>
1 parent 73beeab commit 942b8b4

28 files changed

+1241
-578
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- [tanjun.MenuHooks][] is now exported top-level.
10+
11+
### Changed
12+
- Improved the typing of callbacks which support DI to enforce the type of any positionally
13+
passed arguments with a static type.
14+
MyPy doesn't support this and will just behave as before.
15+
16+
### Fixed
17+
- [tanjun.annotations.Converted][] now properly overrides the actual type annotation for
18+
slash commands.
19+
- The `add_{}_option` and `with_{}_option` methods for the standard slash command impl
20+
will no-longer mishandle iterable but non-sequence types like [enum.Enum][] as if
21+
they were a sequence of converters when they are passed as the value for `converters`.
22+
823
### [2.11.2] - 2023-01-23
924
### Changed
1025
- [tanjun.clients.Client.from_gateway_bot][] can now also take cache-less `ShardAware` bots.

tanjun/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ async def hello(ctx: tanjun.abc.Context, user: hikari.User | None) -> None:
115115
"InteractionAcceptsEnum",
116116
"LazyConstant",
117117
"MenuCommand",
118+
"MenuHooks",
118119
"MessageAcceptsEnum",
119120
"MessageCommand",
120121
"MessageCommandGroup",
@@ -296,6 +297,7 @@ async def hello(ctx: tanjun.abc.Context, user: hikari.User | None) -> None:
296297
from .errors import TooManyArgumentsError
297298
from .hooks import AnyHooks
298299
from .hooks import Hooks
300+
from .hooks import MenuHooks
299301
from .hooks import MessageHooks
300302
from .hooks import SlashHooks
301303
from .injecting import as_self_injecting

tanjun/_internal/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,19 @@
5555

5656
from .. import abc as tanjun
5757

58+
_T = typing.TypeVar("_T")
5859
_P = typing_extensions.ParamSpec("_P")
5960

61+
_ContextT = typing.TypeVar("_ContextT", bound=tanjun.Context)
62+
_CoroT = collections.Coroutine[typing.Any, typing.Any, _T]
6063
_TreeT = dict[
6164
typing.Union[str, "_IndexKeys"],
6265
typing.Union["_TreeT", list[tuple[list[str], tanjun.MessageCommand[typing.Any]]]],
6366
]
6467

6568

66-
_T = typing.TypeVar("_T")
6769
_KeyT = typing.TypeVar("_KeyT")
6870
_OtherT = typing.TypeVar("_OtherT")
69-
_CoroT = collections.Coroutine[typing.Any, typing.Any, _T]
7071

7172
_LOGGER = logging.getLogger("hikari.tanjun")
7273

@@ -88,21 +89,21 @@ class _NoDefaultEnum(enum.Enum):
8889
"""The type of `NO_DEFAULT`."""
8990

9091

91-
async def _execute_check(ctx: tanjun.Context, callback: tanjun.CheckSig, /) -> bool:
92+
async def _execute_check(ctx: _ContextT, callback: tanjun.CheckSig[_ContextT], /) -> bool:
9293
if result := await ctx.call_with_async_di(callback, ctx):
9394
return result
9495

9596
raise errors.FailedCheck
9697

9798

98-
async def gather_checks(ctx: tanjun.Context, checks: collections.Iterable[tanjun.CheckSig], /) -> bool:
99+
async def gather_checks(ctx: _ContextT, checks: collections.Iterable[tanjun.CheckSig[_ContextT]], /) -> bool:
99100
"""Gather a collection of checks.
100101
101102
Parameters
102103
----------
103-
ctx
104+
ctx : tanjun.abc.Context
104105
The context to check.
105-
checks
106+
checks : collections.abc.Iterable[tanjun.abc.CheckSig]
106107
An iterable of injectable checks.
107108
108109
Returns
@@ -157,7 +158,7 @@ def __len__(self) -> int:
157158
_KEYWORD_TYPES = {inspect.Parameter.KEYWORD_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD}
158159

159160

160-
def get_kwargs(callback: collections.Callable[..., typing.Any], /) -> list[str] | None:
161+
def get_kwargs(callback: collections.Callable[..., typing.Any], /) -> typing.Union[list[str], None]:
161162
"""Get a list of the keyword argument names for a callback.
162163
163164
Parameters

0 commit comments

Comments
 (0)