Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing around callbacks with dynamic keyword-arguments #176

Merged
merged 45 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
46f6452
Switch to dataclases' genned slots
FasterSpeeding Jan 25, 2023
f521805
Initial restructuring
FasterSpeeding Jun 3, 2022
4e2b78a
Some post-rebase fixes
FasterSpeeding Aug 14, 2022
34e962f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 14, 2022
acc7787
Post-rebase fixes (mypy broken point)
FasterSpeeding Sep 5, 2022
8ce093e
More type-checking fixes
FasterSpeeding Jan 22, 2023
5351509
More typing fixes + backwards compat fixes
FasterSpeeding Jan 22, 2023
56604bd
Freeze PR dependency changes
always-on-duty[bot] Jan 22, 2023
5bf2fe9
Typing fixes and simplifications
FasterSpeeding Jan 22, 2023
b2b00fb
Improve autocomplete and converter typing
FasterSpeeding Jan 23, 2023
a3c1b8b
Post-rebase fixes
FasterSpeeding Jan 24, 2023
dd1824b
Fix typings in tests
FasterSpeeding Jan 24, 2023
b121e9b
Freeze PR dependency changes
always-on-duty[bot] Jan 24, 2023
fd02905
Fixes
FasterSpeeding Jan 24, 2023
7701995
Remove seemingly unnecessary overloading
FasterSpeeding Jan 24, 2023
56fd210
Bring back overloads for as_cmd and cmd.init
FasterSpeeding Jan 24, 2023
9ab6701
Add overloading for with_check + bump min typing-extensions
FasterSpeeding Jan 24, 2023
d6c21df
Reformat PR code
always-on-duty[bot] Jan 24, 2023
0d5a40b
Freeze PR dependency changes
always-on-duty[bot] Jan 24, 2023
b458eb7
Fix hook types
FasterSpeeding Jan 24, 2023
d9eb54c
Fix annotations.Converted typing
FasterSpeeding Jan 24, 2023
007bd61
simplify maybe coroutine types
FasterSpeeding Jan 24, 2023
b68725b
Remove dated TODO
FasterSpeeding Jan 24, 2023
26b3fd2
Test and behaviour fixes
FasterSpeeding Jan 24, 2023
215e20d
Revert lil logic change to to_slash_option
FasterSpeeding Jan 25, 2023
a9c3737
Freeze PR dependency changes
always-on-duty[bot] Jan 25, 2023
bb7b283
Remove some unnecessary annotations logic
FasterSpeeding Jan 25, 2023
3bde5bb
Avoid unnecessarily building typevars and importing at runtime
FasterSpeeding Jan 25, 2023
00dc1ae
Some doc and type-checking fixes
FasterSpeeding Jan 25, 2023
039bddd
More doc fixes
FasterSpeeding Jan 25, 2023
e958bea
Don't error on 3.10 and 3.9
FasterSpeeding Jan 29, 2023
ee38be0
Post-rebase fixes
FasterSpeeding Jan 30, 2023
8d2a52d
Freeze PR dependency changes
always-on-duty[bot] Jan 30, 2023
d16f502
Some more fixes + changelog entry
FasterSpeeding Jan 30, 2023
706069b
Another small fixa
FasterSpeeding Jan 30, 2023
f663318
Remove typing_extensions runtime dep
FasterSpeeding Jan 30, 2023
4f2d85d
Add back typing_extensions test dep
FasterSpeeding Jan 30, 2023
8a07a84
Don't import typing_extensions at runtime
FasterSpeeding Jan 30, 2023
6765b96
Some simplifications + restore all
FasterSpeeding Jan 30, 2023
0cc054c
Remove typing_extensions from generated constraints file
FasterSpeeding Jan 30, 2023
1907f88
Remove typing_extensions from generated constraints file
FasterSpeeding Jan 30, 2023
2172f0a
Update CHANGELOG.md
FasterSpeeding Jan 30, 2023
8b83f14
Doc improvements
FasterSpeeding Jan 31, 2023
4b7e4f1
Reformat PR code
always-on-duty[bot] Jan 31, 2023
f4621e2
Typo fix
FasterSpeeding Jan 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- [tanjun.MenuHooks][] is now exported top-level.

### Fixed
- [tanjun.annotations.Converted][] now properly overrides the actual type annotation for
slash commands.
- The `add_{}_option` and `with_{}_option` methods for the standard slash command impl
will no-longer mishandle iterable but non-sequence types like [enum.Enum][] as if
they were a sequence of converters when they are passed as the value for `converters`.

### [2.11.2] - 2023-01-23
### Changed
- [tanjun.clients.Client.from_gateway_bot][] can now also take cache-less `ShardAware` bots.
- Improved the typing of callbacks which support DI to enforce the type of any positionally
passed arguments with a static type.
MyPy doesn't support this and will just behave as before.

### Fixed
- [tanjun.clients.Client.set_human_only][] no longer registers the internal check multiple
Expand Down
2 changes: 2 additions & 0 deletions tanjun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async def hello(ctx: tanjun.abc.Context, user: hikari.User | None) -> None:
"InteractionAcceptsEnum",
"LazyConstant",
"MenuCommand",
"MenuHooks",
"MessageAcceptsEnum",
"MessageCommand",
"MessageCommandGroup",
Expand Down Expand Up @@ -296,6 +297,7 @@ async def hello(ctx: tanjun.abc.Context, user: hikari.User | None) -> None:
from .errors import TooManyArgumentsError
from .hooks import AnyHooks
from .hooks import Hooks
from .hooks import MenuHooks
from .hooks import MessageHooks
from .hooks import SlashHooks
from .injecting import as_self_injecting
Expand Down
15 changes: 8 additions & 7 deletions tanjun/_internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@

from .. import abc as tanjun

_T = typing.TypeVar("_T")
_P = typing_extensions.ParamSpec("_P")

_ContextT = typing.TypeVar("_ContextT", bound=tanjun.Context)
_CoroT = collections.Coroutine[typing.Any, typing.Any, _T]
_TreeT = dict[
typing.Union[str, "_IndexKeys"],
typing.Union["_TreeT", list[tuple[list[str], tanjun.MessageCommand[typing.Any]]]],
]


_T = typing.TypeVar("_T")
_KeyT = typing.TypeVar("_KeyT")
_OtherT = typing.TypeVar("_OtherT")
_CoroT = collections.Coroutine[typing.Any, typing.Any, _T]

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

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


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

raise errors.FailedCheck


async def gather_checks(ctx: tanjun.Context, checks: collections.Iterable[tanjun.CheckSig], /) -> bool:
async def gather_checks(ctx: _ContextT, checks: collections.Iterable[tanjun.CheckSig[_ContextT]], /) -> bool:
"""Gather a collection of checks.

Parameters
----------
ctx
ctx : tanjun.abc.Context
The context to check.
checks
checks : collections.abc.Iterable[tanjun.abc.CheckSig]
An iterable of injectable checks.

Returns
Expand Down Expand Up @@ -157,7 +158,7 @@ def __len__(self) -> int:
_KEYWORD_TYPES = {inspect.Parameter.KEYWORD_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD}


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

Parameters
Expand Down
Loading