diff --git a/CHANGELOG.md b/CHANGELOG.md index 66efa6f08..0840b5c3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +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] +### Changed +- Bumped the minimum Alluka ver to `v0.2.0`. +- Tanjun's client will now use the "local" Alluka client returned by + [alluka.local.get][] if no client is passed and a local Alluka client is set. +- [Client.get_type_dependency][tanjun.abc.Client.get_type_dependency] will now + raise a [KeyError][] if no dependency is found and no default is passed. + This is a change imposed by Alluka. + +### Fixed +- Now compatible with Alluka `v0.2.0`. + +### Removed +- `tanjun.injecting.UndefinedOr`, `tanjun.injecting.UNDEFINED`, + `tanjun.injecting.Undefined`, and `tanjun.injecting.UndefinedOr` as these + no-longer exist in Alluka. + ## [2.17.3] - 2024-03-31 ### Fixed - Post execution command hooks will no-longer be triggered after a base diff --git a/dev-requirements/constraints.in b/dev-requirements/constraints.in index efda33dfa..18fd03175 100644 --- a/dev-requirements/constraints.in +++ b/dev-requirements/constraints.in @@ -1,3 +1,3 @@ -alluka>=0.1.2, <1 +alluka>=0.2.0, <1 hikari>=2.0.0.dev115, <3 typing-extensions>=4.5, <5 diff --git a/dev-requirements/constraints.txt b/dev-requirements/constraints.txt index 72cd803d8..2dcd7c5b8 100644 --- a/dev-requirements/constraints.txt +++ b/dev-requirements/constraints.txt @@ -84,9 +84,9 @@ aiohttp==3.9.5 ; python_full_version >= "3.9.0" and python_version < "3.13" \ aiosignal==1.3.1 ; python_full_version >= "3.9.0" and python_version < "3.13" \ --hash=sha256:54cd96e15e1649b75d6c87526a6ff0b6c1b0dd3459f43d9ca11d48c339b68cfc \ --hash=sha256:f8376fb07dd1e86a584e4fcdec80b36b7f81aac666ebc724e2c090300dd83b17 -alluka==0.1.5 ; python_full_version >= "3.9.0" and python_version < "3.13" \ - --hash=sha256:0b71687c829330dfa34aefa417a917fa9423e4e37e486c82db5a86a3031097db \ - --hash=sha256:f0d8fb0bf067e5dbc75b51070fd2ea7651369e10055d6bb9bfcd4cbc28a93af5 +alluka==0.2.0 ; python_full_version >= "3.9.0" and python_version < "3.13" \ + --hash=sha256:8d5e45fab2dfb4cbc705bbbb9090d9d7ed79836024ff719a0fe494d89a0b0da2 \ + --hash=sha256:f9c85d91d8a4964e3dea8ba719c2548230d4a1a141ec25c5daf35b16f649018c async-timeout==4.0.3 ; python_full_version >= "3.9.0" and python_version < "3.11" \ --hash=sha256:4640d96be84d82d02ed59ea2b7105a0f7b33abe8703703cd0ab0bf87c427522f \ --hash=sha256:7405140ff1230c310e51dc27b3145b9092d659ce68ff733fb0cefe3ee42be028 diff --git a/dev-requirements/tests.txt b/dev-requirements/tests.txt index ae251f63d..21a27163c 100644 --- a/dev-requirements/tests.txt +++ b/dev-requirements/tests.txt @@ -84,9 +84,9 @@ aiohttp==3.9.5 ; python_full_version >= "3.9.0" and python_version < "3.13" \ aiosignal==1.3.1 ; python_full_version >= "3.9.0" and python_version < "3.13" \ --hash=sha256:54cd96e15e1649b75d6c87526a6ff0b6c1b0dd3459f43d9ca11d48c339b68cfc \ --hash=sha256:f8376fb07dd1e86a584e4fcdec80b36b7f81aac666ebc724e2c090300dd83b17 -alluka==0.1.5 ; python_full_version >= "3.9.0" and python_version < "3.13" \ - --hash=sha256:0b71687c829330dfa34aefa417a917fa9423e4e37e486c82db5a86a3031097db \ - --hash=sha256:f0d8fb0bf067e5dbc75b51070fd2ea7651369e10055d6bb9bfcd4cbc28a93af5 +alluka==0.2.0 ; python_full_version >= "3.9.0" and python_version < "3.13" \ + --hash=sha256:8d5e45fab2dfb4cbc705bbbb9090d9d7ed79836024ff719a0fe494d89a0b0da2 \ + --hash=sha256:f9c85d91d8a4964e3dea8ba719c2548230d4a1a141ec25c5daf35b16f649018c async-timeout==4.0.3 ; python_full_version >= "3.9.0" and python_version < "3.11" \ --hash=sha256:4640d96be84d82d02ed59ea2b7105a0f7b33abe8703703cd0ab0bf87c427522f \ --hash=sha256:7405140ff1230c310e51dc27b3145b9092d659ce68ff733fb0cefe3ee42be028 diff --git a/dev-requirements/type-checking.txt b/dev-requirements/type-checking.txt index f4788559c..a9104eb03 100644 --- a/dev-requirements/type-checking.txt +++ b/dev-requirements/type-checking.txt @@ -84,9 +84,9 @@ aiohttp==3.9.5 ; python_full_version >= "3.9.0" and python_version < "3.13" \ aiosignal==1.3.1 ; python_full_version >= "3.9.0" and python_version < "3.13" \ --hash=sha256:54cd96e15e1649b75d6c87526a6ff0b6c1b0dd3459f43d9ca11d48c339b68cfc \ --hash=sha256:f8376fb07dd1e86a584e4fcdec80b36b7f81aac666ebc724e2c090300dd83b17 -alluka==0.1.5 ; python_full_version >= "3.9.0" and python_version < "3.13" \ - --hash=sha256:0b71687c829330dfa34aefa417a917fa9423e4e37e486c82db5a86a3031097db \ - --hash=sha256:f0d8fb0bf067e5dbc75b51070fd2ea7651369e10055d6bb9bfcd4cbc28a93af5 +alluka==0.2.0 ; python_full_version >= "3.9.0" and python_version < "3.13" \ + --hash=sha256:8d5e45fab2dfb4cbc705bbbb9090d9d7ed79836024ff719a0fe494d89a0b0da2 \ + --hash=sha256:f9c85d91d8a4964e3dea8ba719c2548230d4a1a141ec25c5daf35b16f649018c annotated-types==0.6.0 ; python_full_version >= "3.9.0" and python_version < "3.13" \ --hash=sha256:0641064de18ba7a25dee8f96403ebc39113d0cb953a01429249d5c7564666a43 \ --hash=sha256:563339e807e53ffd9c267e99fc6d9ea23eb8443c08f112651963e24e22f84a5d @@ -167,9 +167,9 @@ exceptiongroup==1.2.1 ; python_full_version >= "3.9.0" and python_version < "3.1 execnet==2.1.1 ; python_full_version >= "3.9.0" and python_version < "3.13" \ --hash=sha256:26dee51f1b80cebd6d0ca8e74dd8745419761d3bef34163928cbebbdc4749fdc \ --hash=sha256:5189b52c6121c24feae288166ab41b32549c7e2348652736540b9e6e7d4e72e3 -filelock==3.13.4 ; python_full_version >= "3.9.0" and python_version < "3.13" \ - --hash=sha256:404e5e9253aa60ad457cae1be07c0f0ca90a63931200a47d9b6a6af84fd7b45f \ - --hash=sha256:d13f466618bfde72bd2c18255e269f72542c6e70e7bac83a0232d6b1cc5c8cf4 +filelock==3.14.0 ; python_full_version >= "3.9.0" and python_version < "3.13" \ + --hash=sha256:43339835842f110ca7ae60f1e1c160714c5a6afd15a2873419ab185334975c0f \ + --hash=sha256:6ea72da3be9b8c82afd3edcf99f2fffbb5076335a5ae4d03248bb5b6c3eae78a freezegun==1.5.0 ; python_full_version >= "3.9.0" and python_version < "3.13" \ --hash=sha256:200a64359b363aa3653d8aac289584078386c7c3da77339d257e46a01fb5c77c \ --hash=sha256:ec3f4ba030e34eb6cf7e1e257308aee2c60c3d038ff35996d7475760c9ff3719 @@ -586,9 +586,9 @@ tomli==2.0.1 ; python_full_version >= "3.9.0" and python_version < "3.13" \ typing-extensions==4.11.0 ; python_full_version >= "3.9.0" and python_version < "3.13" \ --hash=sha256:83f085bd5ca59c80295fc2a82ab5dac679cbe02b9f33f7d83af68e241bea51b0 \ --hash=sha256:c1f94d72897edaf4ce775bb7558d5b79d8126906a14ea5ed1635921406c0387a -virtualenv==20.26.0 ; python_full_version >= "3.9.0" and python_version < "3.13" \ - --hash=sha256:0846377ea76e818daaa3e00a4365c018bc3ac9760cbb3544de542885aad61fb3 \ - --hash=sha256:ec25a9671a5102c8d2657f62792a27b48f016664c6873f6beed3800008577210 +virtualenv==20.26.1 ; python_full_version >= "3.9.0" and python_version < "3.13" \ + --hash=sha256:604bfdceaeece392802e6ae48e69cec49168b9c5f4a44e483963f9242eb0e78b \ + --hash=sha256:7aa9982a728ae5892558bff6a2839c00b9ed145523ece2274fad6f414690ae75 yarl==1.9.4 ; python_full_version >= "3.9.0" and python_version < "3.13" \ --hash=sha256:008d3e808d03ef28542372d01057fd09168419cdc8f848efe2804f894ae03e51 \ --hash=sha256:03caa9507d3d3c83bca08650678e25364e1843b484f19986a527630ca376ecce \ diff --git a/pyproject.toml b/pyproject.toml index 098621902..0d17030f2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ classifiers = [ "Topic :: Utilities", "Typing :: Typed", ] -dependencies = ["alluka>=0.1.2, <1", "hikari>=2.0.0.dev115, <3", "typing-extensions>=4.5, <5"] +dependencies = ["alluka>=0.2.0, <1", "hikari>=2.0.0.dev115, <3", "typing-extensions>=4.5, <5"] dynamic = ["description"] [project.urls] diff --git a/tanjun/_internal/cache.py b/tanjun/_internal/cache.py index 93da48ba3..a4053bc6a 100644 --- a/tanjun/_internal/cache.py +++ b/tanjun/_internal/cache.py @@ -74,7 +74,7 @@ async def get_perm_channel(client: tanjun.Client, channel_id: hikari.Snowflake, if client.cache and (channel := client.cache.get_guild_channel(channel_id)): return channel - thread_cache = client.injector.get_type_dependency(_ThreadCacheT) + thread_cache = client.injector.get_type_dependency(_ThreadCacheT, default=None) if thread_cache: # noqa: SIM102 # Has to be nested cause of pyright bug: if thread := await thread_cache.get(channel_id, default=None): # noqa: SIM102 diff --git a/tanjun/abc.py b/tanjun/abc.py index 88da77e37..61bc3183a 100644 --- a/tanjun/abc.py +++ b/tanjun/abc.py @@ -109,6 +109,8 @@ _MessageCommandT = typing.TypeVar("_MessageCommandT", bound="MessageCommand[typing.Any]") _MetaEventSigT = typing.TypeVar("_MetaEventSigT", bound="MetaEventSig") + _DefaultT = typing.TypeVar("_DefaultT") + _T = typing.TypeVar("_T") _AppCommandContextT = typing.TypeVar("_AppCommandContextT", bound="AppCommandContext") _AutocompleteValueT = typing.TypeVar("_AutocompleteValueT", int, str, float) @@ -4896,19 +4898,37 @@ def set_type_dependency(self, type_: type[_T], value: _T, /) -> Self: The client instance to allow chaining. """ + @typing.overload + @abc.abstractmethod + def get_type_dependency(self, type_: type[_T], /) -> _T: ... + + @typing.overload + @abc.abstractmethod + def get_type_dependency(self, type_: type[_T], /, *, default: _DefaultT) -> typing.Union[_T, _DefaultT]: ... + @abc.abstractmethod - def get_type_dependency(self, type_: type[_T], /) -> typing.Union[_T, alluka.Undefined]: + def get_type_dependency(self, type_: type[_T], /, *, default: _DefaultT = ...) -> typing.Union[_T, _DefaultT]: """Get the implementation for an injected type. Parameters ---------- type_ The associated type. + default + The default value to return if the type is not implemented. Returns ------- - _T | alluka.abc.Undefined - The resolved type if found, else [alluka.abc.UNDEFINED][]. + _T | _DefaultT + The resolved type if found. + + If the type isn't implemented then the value of `default` + will be returned if it is provided. + + Raises + ------ + KeyError + If no dependency was found when no default was provided. """ @abc.abstractmethod diff --git a/tanjun/clients.py b/tanjun/clients.py index e9a93e56b..012ad274e 100644 --- a/tanjun/clients.py +++ b/tanjun/clients.py @@ -85,6 +85,7 @@ _PrefixGetterSigT = typing.TypeVar("_PrefixGetterSigT", bound="PrefixGetterSig") _T = typing.TypeVar("_T") _P = typing_extensions.ParamSpec("_P") + _DefaultT = typing.TypeVar("_DefaultT") class _AutocompleteContextMakerProto(typing.Protocol): def __call__( @@ -680,7 +681,8 @@ def __init__( injector The alluka client this should use for dependency injection. - If not provided then the client will initialise its own DI client. + If not provided then either the "local" Alluka client will be used or + the client will initialise its own DI client. mention_prefix Whether or not mention prefixes should be automatically set when this client is first started. @@ -760,11 +762,7 @@ def __init__( self._injector = injector or alluka.Client() self._is_closing = False self._listeners: dict[ - type[hikari.Event], - dict[ - tanjun.ListenerCallbackSig[typing.Any], - alluka.abc.AsyncSelfInjecting[tanjun.ListenerCallbackSig[typing.Any]], - ], + type[hikari.Event], dict[tanjun.ListenerCallbackSig[typing.Any], tanjun.ListenerCallbackSig[typing.Any]] ] = {} self._loop: typing.Optional[asyncio.AbstractEventLoop] = None self._make_autocomplete_context: _AutocompleteContextMakerProto = context.AutocompleteContext @@ -1225,7 +1223,7 @@ def listeners( self, ) -> collections.Mapping[type[hikari.Event], collections.Collection[tanjun.ListenerCallbackSig[typing.Any]]]: # <>. - return _internal.CastedView(self._listeners, lambda x: [callback.callback for callback in x.values()]) + return _internal.CastedView(self._listeners, lambda x: list(x.values())) @property def is_alive(self) -> bool: @@ -2078,7 +2076,7 @@ def decorator(callback: _MetaEventSigT, /) -> _MetaEventSigT: def add_listener(self, event_type: type[_EventT], /, *callbacks: tanjun.ListenerCallbackSig[_EventT]) -> Self: # <>. for callback in callbacks: - injected = self.injector.as_async_self_injecting(callback) + injected = self.injector.auto_inject_async(callback) try: if callback in self._listeners[event_type]: continue @@ -2090,7 +2088,7 @@ def add_listener(self, event_type: type[_EventT], /, *callbacks: tanjun.Listener self._listeners[event_type][callback] = injected if self._loop and self._events: - self._events.subscribe(event_type, injected.__call__) + self._events.subscribe(event_type, injected) return self @@ -2107,7 +2105,7 @@ def remove_listener(self, event_type: type[_EventT], callback: tanjun.ListenerCa del self._listeners[event_type] if self._loop and self._events: - self._events.unsubscribe(event_type, registered_callback.__call__) + self._events.unsubscribe(event_type, registered_callback) return self @@ -2330,7 +2328,7 @@ async def close(self, *, deregister_listeners: bool = True) -> None: for event_type_, listeners in self._listeners.items(): for listener in listeners.values(): - _try_unsubscribe(self._events, event_type_, listener.__call__) + _try_unsubscribe(self._events, event_type_, listener) if deregister_listeners and self._server: _try_deregister_listener(self._server, hikari.CommandInteraction, self.on_command_interaction_request) @@ -2370,7 +2368,9 @@ async def open(self, *, register_listeners: bool = True) -> None: if self._cache: user = self._cache.get_me() - if not user and (user_cache := self.get_type_dependency(dependencies.SingleStoreCache[hikari.OwnUser])): + if not user and ( + user_cache := self.get_type_dependency(dependencies.SingleStoreCache[hikari.OwnUser], default=None) + ): user = await user_cache.get(default=None) if not user: @@ -2403,7 +2403,7 @@ async def open(self, *, register_listeners: bool = True) -> None: for event_type_, listeners in self._listeners.items(): for listener in listeners.values(): - self._events.subscribe(event_type_, listener.__call__) + self._events.subscribe(event_type_, listener) self._add_task(self._loop.create_task(self.dispatch_client_callback(ClientCallbackNames.STARTED))) @@ -2419,8 +2419,8 @@ async def fetch_rest_application_id(self) -> hikari.Snowflake: return self._cached_application_id application_cache = self.get_type_dependency( - dependencies.SingleStoreCache[hikari.Application] - ) or self.get_type_dependency(dependencies.SingleStoreCache[hikari.AuthorizationApplication]) + dependencies.SingleStoreCache[hikari.Application], default=None + ) or self.get_type_dependency(dependencies.SingleStoreCache[hikari.AuthorizationApplication], default=None) if application_cache: # noqa: SIM102 # Has to be nested cause of pyright bug if application := await application_cache.get(default=None): @@ -2755,9 +2755,20 @@ def set_type_dependency(self, type_: type[_T], value: _T, /) -> Self: self._injector.set_type_dependency(type_, value) return self - def get_type_dependency(self, type_: type[_T], /) -> typing.Union[_T, alluka.abc.Undefined]: + @typing.overload + def get_type_dependency(self, type_: type[_T], /) -> _T: ... + + @typing.overload + def get_type_dependency(self, type_: type[_T], /, *, default: _DefaultT) -> typing.Union[_T, _DefaultT]: ... + + def get_type_dependency( + self, type_: type[_T], /, *, default: _DefaultT = tanjun.NO_DEFAULT + ) -> typing.Union[_T, _DefaultT]: # <>. - return self._injector.get_type_dependency(type_) + if default is tanjun.NO_DEFAULT: + return self._injector.get_type_dependency(type_) + + return self._injector.get_type_dependency(type_, default=default) def remove_type_dependency(self, type_: type[typing.Any], /) -> Self: # <>. diff --git a/tanjun/dependencies/data.py b/tanjun/dependencies/data.py index 0bf1c5562..ad25f5166 100644 --- a/tanjun/dependencies/data.py +++ b/tanjun/dependencies/data.py @@ -40,6 +40,8 @@ import alluka +from .. import abc as tanjun + if typing.TYPE_CHECKING: import contextlib from collections import abc as collections @@ -232,7 +234,7 @@ def __init__( self._callback = callback self._last_called: typing.Optional[float] = None self._lock: typing.Optional[asyncio.Lock] = None - self._result: typing.Union[_T, alluka.abc.Undefined] = alluka.abc.UNDEFINED + self._result: typing.Union[_T, tanjun.NoDefault] = tanjun.NO_DEFAULT if expire_after is None: pass elif isinstance(expire_after, datetime.timedelta): @@ -253,16 +255,14 @@ def _has_expired(self) -> bool: # Positional arg(s) may be guaranteed under some contexts so we want to pass those through. async def __call__(self, *args: typing.Any, ctx: alluka.Injected[alluka.abc.Context]) -> _T: - if self._result is not alluka.abc.UNDEFINED and not self._has_expired: - assert not isinstance(self._result, alluka.abc.Undefined) + if self._result is not tanjun.NO_DEFAULT and not self._has_expired: return self._result if not self._lock: self._lock = asyncio.Lock() async with self._lock: - if self._result is not alluka.abc.UNDEFINED and not self._has_expired: - assert not isinstance(self._result, alluka.abc.Undefined) + if self._result is not tanjun.NO_DEFAULT and not self._has_expired: return self._result result = await ctx.call_with_async_di(self._callback, *args) diff --git a/tanjun/dependencies/limiters.py b/tanjun/dependencies/limiters.py index d4c7ed3bf..fe7f013ab 100644 --- a/tanjun/dependencies/limiters.py +++ b/tanjun/dependencies/limiters.py @@ -447,13 +447,13 @@ async def _get_ctx_target(ctx: tanjun.Context, type_: BucketResource, /) -> hika if cached_channel := ctx.get_channel(): return cached_channel.parent_id or ctx.guild_id - channel_cache = ctx.get_type_dependency(async_cache.SfCache[hikari.PermissibleGuildChannel]) + channel_cache = ctx.get_type_dependency(async_cache.SfCache[hikari.PermissibleGuildChannel], default=None) if channel_cache: # noqa: SIM102 # Has to be nested cause of pyright bug: if channel := await channel_cache.get(ctx.channel_id, default=None): return channel.parent_id or ctx.guild_id - thread_cache = ctx.get_type_dependency(async_cache.SfCache[hikari.GuildThreadChannel]) + thread_cache = ctx.get_type_dependency(async_cache.SfCache[hikari.GuildThreadChannel], default=None) if thread_cache: # noqa: SIM102 # Has to be nested cause of pyright bug if channel := await thread_cache.get(ctx.channel_id, default=None): @@ -476,7 +476,7 @@ async def _get_ctx_target(ctx: tanjun.Context, type_: BucketResource, /) -> hika try_rest = not roles if try_rest: # noqa: SIM102 # Has to be nested cause of pyright bug - if role_cache := ctx.get_type_dependency(async_cache.SfCache[hikari.Role]): + if role_cache := ctx.get_type_dependency(async_cache.SfCache[hikari.Role], default=None): try: roles = filter(None, [await _try_get_role(role_cache, role_id) for role_id in ctx.member.role_ids]) try_rest = False diff --git a/tanjun/dependencies/owners.py b/tanjun/dependencies/owners.py index cb0be37a0..726a0538d 100644 --- a/tanjun/dependencies/owners.py +++ b/tanjun/dependencies/owners.py @@ -168,7 +168,7 @@ async def check_ownership(self, client: tanjun.Client, user: hikari.User, /) -> if not self._fallback_to_application: return False - application_cache = client.get_type_dependency(_ApplicationCacheT) + application_cache = client.get_type_dependency(_ApplicationCacheT, default=None) if application_cache: # noqa: SIM102 # Has to be nested cause of pyright bug if application := await application_cache.get(default=None): diff --git a/tanjun/injecting.py b/tanjun/injecting.py index 7c0668efe..c4bfb018c 100644 --- a/tanjun/injecting.py +++ b/tanjun/injecting.py @@ -44,9 +44,6 @@ "Injected", "InjectorClient", "SelfInjectingCallback", - "UNDEFINED", - "Undefined", - "UndefinedOr", "as_self_injecting", "inject", "injected", @@ -56,16 +53,14 @@ import typing import alluka -from alluka import AsyncSelfInjecting as SelfInjectingCallback +from alluka import AsyncSelfInjecting as SelfInjectingCallback # pyright: ignore[reportDeprecated] from alluka import BasicContext as BasicInjectionContext from alluka import Client as InjectorClient from alluka import Injected from alluka import inject from alluka import inject as injected -from alluka.abc import UNDEFINED from alluka.abc import CallbackSig from alluka.abc import Context as AbstractInjectionContext -from alluka.abc import Undefined if typing.TYPE_CHECKING: from . import abc as tanjun @@ -73,13 +68,12 @@ _T = typing.TypeVar("_T") _CallbackSigT = typing.TypeVar("_CallbackSigT", bound=alluka.abc.CallbackSig[typing.Any]) -UndefinedOr = typing.Union[Undefined, _T] -"""Type-hint generic union used to indicate that a value may be undefined or `_T`.""" - def as_self_injecting( client: tanjun.Client, / -) -> collections.Callable[[_CallbackSigT], alluka.AsyncSelfInjecting[_CallbackSigT]]: +) -> collections.Callable[ + [_CallbackSigT], alluka.AsyncSelfInjecting[_CallbackSigT] # pyright: ignore[reportDeprecated] +]: """Make a callback self-inecting by linking it to a client through a decorator call. Examples @@ -106,7 +100,9 @@ async def get_int_value( Decorator callback that returns a self-injecting callback. """ - def decorator(callback: _CallbackSigT, /) -> alluka.AsyncSelfInjecting[_CallbackSigT]: - return alluka.AsyncSelfInjecting(client.injector, callback) + def decorator( + callback: _CallbackSigT, / + ) -> alluka.AsyncSelfInjecting[_CallbackSigT]: # pyright: ignore[reportDeprecated] + return alluka.AsyncSelfInjecting(client.injector, callback) # pyright: ignore[reportDeprecated] return decorator diff --git a/tanjun/permissions.py b/tanjun/permissions.py index ff6724a62..7293c234d 100644 --- a/tanjun/permissions.py +++ b/tanjun/permissions.py @@ -210,7 +210,7 @@ async def fetch_permissions( guild = client.cache.get_guild(member.guild_id) if client.cache else None if not guild: # noqa: SIM102 # Has to be nested cause of pyright bug. - if guild_cache := client.get_type_dependency(_GuildCacheT): + if guild_cache := client.get_type_dependency(_GuildCacheT, default=None): try: guild = await guild_cache.get(member.guild_id) @@ -231,7 +231,7 @@ async def fetch_permissions( roles = roles or client.cache and client.cache.get_roles_view_for_guild(member.guild_id) if not roles: # noqa: SIM102 # Has to be nested cause of pyright bug. - if role_cache := client.get_type_dependency(_GuldRoleCacheT): + if role_cache := client.get_type_dependency(_GuldRoleCacheT, default=None): roles = {role.id: role for role in await role_cache.iter_for_guild(member.guild_id)} if not roles: @@ -324,7 +324,7 @@ async def fetch_everyone_permissions( role = client.cache.get_role(guild_id) if client.cache else None if not role: # noqa: SIM102 # Has to be nested cause of pyright bug. - if role_cache := client.get_type_dependency(_RoleCacheT): + if role_cache := client.get_type_dependency(_RoleCacheT, default=None): try: role = await role_cache.get(guild_id) diff --git a/tests/context/test_base.py b/tests/context/test_base.py index b9b42f63c..cba3f118e 100644 --- a/tests/context/test_base.py +++ b/tests/context/test_base.py @@ -69,7 +69,7 @@ def stub_class( @pytest.fixture def mock_client() -> tanjun.abc.Client: - return mock.MagicMock(tanjun.abc.Client, rest=mock.AsyncMock(hikari.api.RESTClient)) + return mock.MagicMock(tanjun.abc.Client, rest=mock.AsyncMock(hikari.api.RESTClient), injector=alluka.Client()) @pytest.fixture @@ -138,23 +138,23 @@ def test_set_component(self, context: base_context.BaseContext): assert context.get_type_dependency(tanjun.abc.Component) is component def test_set_component_when_none_and_previously_set(self, context: base_context.BaseContext): - assert isinstance(context.injection_client.get_type_dependency, mock.Mock) - context.injection_client.get_type_dependency.return_value = alluka.abc.UNDEFINED mock_component = mock.Mock() context.set_component(mock_component) context.set_component(None) assert context.component is None - assert context.get_type_dependency(tanjun.abc.Component) is alluka.abc.UNDEFINED + + with pytest.raises(KeyError): + context.get_type_dependency(tanjun.abc.Component) def test_set_component_when_none(self, context: base_context.BaseContext): - assert isinstance(context.injection_client.get_type_dependency, mock.Mock) - context.injection_client.get_type_dependency.return_value = alluka.abc.UNDEFINED context.set_component(None) context.set_component(None) assert context.component is None - assert context.get_type_dependency(tanjun.abc.Component) is alluka.abc.UNDEFINED + + with pytest.raises(KeyError): + context.get_type_dependency(tanjun.abc.Component) def test_set_component_when_final(self, context: base_context.BaseContext): component = mock.Mock() diff --git a/tests/context/test_message.py b/tests/context/test_message.py index a320a78dc..59584e11a 100644 --- a/tests/context/test_message.py +++ b/tests/context/test_message.py @@ -69,7 +69,7 @@ def stub_class( @pytest.fixture def mock_client() -> tanjun.abc.Client: - return mock.MagicMock(tanjun.abc.Client, rest=mock.AsyncMock(hikari.api.RESTClient)) + return mock.MagicMock(tanjun.abc.Client, rest=mock.AsyncMock(hikari.api.RESTClient), injector=alluka.Client()) @pytest.fixture @@ -146,25 +146,29 @@ def test_set_command(self, context: tanjun.context.MessageContext): assert context.get_type_dependency(tanjun.abc.MessageCommand) is mock_command def test_set_command_when_none(self, context: tanjun.context.MessageContext): - assert isinstance(context.client.injector.get_type_dependency, mock.Mock) - context.client.injector.get_type_dependency.return_value = alluka.abc.UNDEFINED context.set_command(None) context.set_command(None) assert context.command is None - assert context.get_type_dependency(tanjun.abc.ExecutableCommand) is alluka.abc.UNDEFINED - assert context.get_type_dependency(tanjun.abc.MessageCommand) is alluka.abc.UNDEFINED + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.ExecutableCommand) + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.MessageCommand) def test_set_command_when_none_and_previously_set(self, context: tanjun.context.MessageContext): - assert isinstance(context.client.injector.get_type_dependency, mock.Mock) - context.client.injector.get_type_dependency.return_value = alluka.abc.UNDEFINED mock_command = mock.Mock() context.set_command(mock_command) context.set_command(None) assert context.command is None - assert context.get_type_dependency(tanjun.abc.ExecutableCommand) is alluka.abc.UNDEFINED - assert context.get_type_dependency(tanjun.abc.MessageCommand) is alluka.abc.UNDEFINED + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.ExecutableCommand) + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.MessageCommand) def test_set_command_when_finalised(self, context: tanjun.context.MessageContext): context.finalise() diff --git a/tests/context/test_slash.py b/tests/context/test_slash.py index cd3eac5af..e7fb4afbd 100644 --- a/tests/context/test_slash.py +++ b/tests/context/test_slash.py @@ -69,7 +69,7 @@ def stub_class( @pytest.fixture def mock_client() -> tanjun.abc.Client: - return mock.MagicMock(tanjun.abc.Client, rest=mock.AsyncMock(hikari.api.RESTClient)) + return mock.MagicMock(tanjun.abc.Client, rest=mock.AsyncMock(hikari.api.RESTClient), injector=alluka.Client()) @pytest.fixture @@ -1137,29 +1137,41 @@ def test_set_command(self, context: tanjun.context.SlashContext): assert context.get_type_dependency(tanjun.abc.SlashCommand) is mock_command def test_set_command_when_none(self, context: tanjun.context.SlashContext): - assert isinstance(context.injection_client.get_type_dependency, mock.Mock) - context.injection_client.get_type_dependency.return_value = alluka.abc.UNDEFINED context.set_command(None) context.set_command(None) assert context.command is None - assert context.get_type_dependency(tanjun.abc.ExecutableCommand) is alluka.abc.UNDEFINED - assert context.get_type_dependency(tanjun.abc.AppCommand) is alluka.abc.UNDEFINED - assert context.get_type_dependency(tanjun.abc.BaseSlashCommand) is alluka.abc.UNDEFINED - assert context.get_type_dependency(tanjun.abc.SlashCommand) is alluka.abc.UNDEFINED + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.ExecutableCommand) + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.AppCommand) + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.BaseSlashCommand) + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.SlashCommand) def test_set_command_when_none_and_previously_set(self, context: tanjun.context.SlashContext): - assert isinstance(context.injection_client.get_type_dependency, mock.Mock) - context.injection_client.get_type_dependency.return_value = alluka.abc.UNDEFINED mock_command = mock.Mock() context.set_command(mock_command) context.set_command(None) assert context.command is None - assert context.get_type_dependency(tanjun.abc.ExecutableCommand) is alluka.abc.UNDEFINED - assert context.get_type_dependency(tanjun.abc.AppCommand) is alluka.abc.UNDEFINED - assert context.get_type_dependency(tanjun.abc.BaseSlashCommand) is alluka.abc.UNDEFINED - assert context.get_type_dependency(tanjun.abc.SlashCommand) is alluka.abc.UNDEFINED + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.ExecutableCommand) + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.AppCommand) + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.BaseSlashCommand) + + with pytest.raises(KeyError): + assert context.get_type_dependency(tanjun.abc.SlashCommand) def test_set_command_when_finalised(self, context: tanjun.context.SlashContext): context.finalise() diff --git a/tests/dependencies/test_limiters.py b/tests/dependencies/test_limiters.py index fd10ba226..f71041041 100644 --- a/tests/dependencies/test_limiters.py +++ b/tests/dependencies/test_limiters.py @@ -37,7 +37,6 @@ import re import typing -import alluka import freezegun import hikari import mock @@ -482,7 +481,7 @@ async def test__get_ctx_target_when_parent_channel_when_async_channel_cache_retu mock_channel_cache.get.assert_awaited_once_with(mock_context.channel_id, default=None) mock_context.fetch_channel.assert_not_called() mock_context.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel] + tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel], default=None ) @@ -501,7 +500,7 @@ async def test__get_ctx_target_when_parent_channel_when_async_channel_cache_retu mock_channel_cache.get.assert_awaited_once_with(mock_context.channel_id, default=None) mock_context.fetch_channel.assert_not_called() mock_context.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel] + tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel], default=None ) @@ -524,8 +523,8 @@ async def test__get_ctx_target_when_parent_channel_when_async_thread_cache_retur mock_context.fetch_channel.assert_not_called() mock_context.get_type_dependency.assert_has_calls( [ - mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel]), - mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel]), + mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel], default=None), + mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel], default=None), ] ) @@ -550,8 +549,8 @@ async def test__get_ctx_target_when_parent_channel_when_async_caches_returns_non mock_context.fetch_channel.assert_awaited_once() mock_context.get_type_dependency.assert_has_calls( [ - mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel]), - mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel]), + mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel], default=None), + mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel], default=None), ] ) mock_channel_cache.get.assert_awaited_once_with(mock_context.channel_id, default=None) @@ -576,8 +575,8 @@ async def test__get_ctx_target_when_parent_channel_when_async_caches_returns_non mock_context.fetch_channel.assert_awaited_once() mock_context.get_type_dependency.assert_has_calls( [ - mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel]), - mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel]), + mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel], default=None), + mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel], default=None), ] ) mock_channel_cache.get.assert_awaited_once_with(mock_context.channel_id, default=None) @@ -591,7 +590,7 @@ async def test__get_ctx_target_when_parent_channel_and_no_async_caches_falls_bac mock_context.fetch_channel = mock.AsyncMock( return_value=mock.Mock(hikari.GuildChannel, parent_id=1235234, id=123321) ) - mock_context.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_context.get_type_dependency.return_value = None result = await tanjun.dependencies.limiters._get_ctx_target(mock_context, tanjun.BucketResource.PARENT_CHANNEL) @@ -600,8 +599,8 @@ async def test__get_ctx_target_when_parent_channel_and_no_async_caches_falls_bac mock_context.fetch_channel.assert_awaited_once() mock_context.get_type_dependency.assert_has_calls( [ - mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel]), - mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel]), + mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel], default=None), + mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel], default=None), ] ) @@ -611,7 +610,7 @@ async def test__get_ctx_target_when_parent_channel_and_no_async_caches_falls_bac mock_context = mock.Mock(base_context.BaseContext) mock_context.get_channel.return_value = None mock_context.fetch_channel = mock.AsyncMock(return_value=mock.Mock(hikari.GuildChannel, parent_id=None, id=123)) - mock_context.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_context.get_type_dependency.return_value = None result = await tanjun.dependencies.limiters._get_ctx_target(mock_context, tanjun.BucketResource.PARENT_CHANNEL) @@ -620,8 +619,8 @@ async def test__get_ctx_target_when_parent_channel_and_no_async_caches_falls_bac mock_context.fetch_channel.assert_awaited_once() mock_context.get_type_dependency.assert_has_calls( [ - mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel]), - mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel]), + mock.call(tanjun.dependencies.SfCache[hikari.PermissibleGuildChannel], default=None), + mock.call(tanjun.dependencies.SfCache[hikari.GuildThreadChannel], default=None), ] ) @@ -674,7 +673,7 @@ async def test__get_ctx_target_when_top_role_and_async_cache(): mock_context.member.get_roles.assert_called_once_with() mock_context.member.fetch_roles.assert_not_called() - mock_context.get_type_dependency.assert_called_once_with(tanjun.dependencies.SfCache[hikari.Role]) + mock_context.get_type_dependency.assert_called_once_with(tanjun.dependencies.SfCache[hikari.Role], default=None) mock_cache.get.assert_has_awaits([mock.call(674345), mock.call(123876), mock.call(7643), mock.call(9999999)]) @@ -699,7 +698,7 @@ async def test__get_ctx_target_when_top_role_falls_back_to_rest_when_async_cache mock_context.member.get_roles.assert_called_once_with() mock_context.member.fetch_roles.assert_awaited_once_with() - mock_context.get_type_dependency.assert_called_once_with(tanjun.dependencies.SfCache[hikari.Role]) + mock_context.get_type_dependency.assert_called_once_with(tanjun.dependencies.SfCache[hikari.Role], default=None) mock_cache.get.assert_has_awaits([mock.call(123), mock.call(312)]) @@ -716,13 +715,13 @@ async def test__get_ctx_target_when_top_role_falls_back_to_rest(): mock_context.member.role_ids = [123, 312] mock_context.member.get_roles = mock.Mock(return_value=[]) mock_context.member.fetch_roles = mock.AsyncMock(return_value=mock_roles) - mock_context.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_context.get_type_dependency.return_value = None assert await tanjun.dependencies.limiters._get_ctx_target(mock_context, tanjun.BucketResource.TOP_ROLE) == 431 mock_context.member.get_roles.assert_called_once_with() mock_context.member.fetch_roles.assert_awaited_once_with() - mock_context.get_type_dependency.assert_called_once_with(tanjun.dependencies.SfCache[hikari.Role]) + mock_context.get_type_dependency.assert_called_once_with(tanjun.dependencies.SfCache[hikari.Role], default=None) @pytest.mark.asyncio diff --git a/tests/dependencies/test_owners.py b/tests/dependencies/test_owners.py index 5bc943dfb..91a1f4c21 100644 --- a/tests/dependencies/test_owners.py +++ b/tests/dependencies/test_owners.py @@ -37,7 +37,6 @@ import time import typing -import alluka import hikari import mock import pytest @@ -55,7 +54,6 @@ def test_init_with_invalid_expire_after(self, value: typing.Union[int, float, da async def test_check_ownership_when_user_in_owner_ids(self): check = tanjun.dependencies.Owners(owners=[123, 7634]) mock_client = mock.Mock(tanjun.Client) - mock_client.get_type_dependency.return_value = alluka.abc.UNDEFINED result = await check.check_ownership(mock_client, mock.Mock(id=7634)) @@ -67,8 +65,6 @@ async def test_check_ownership_when_user_in_owner_ids(self): async def test_check_ownership_when_not_falling_back_to_application(self): check = tanjun.dependencies.Owners(owners=[123, 7634], fallback_to_application=False) mock_client = mock.Mock(tanjun.Client) - mock_client.get_type_dependency.return_value = alluka.abc.UNDEFINED - result = await check.check_ownership(mock_client, mock.Mock(id=54123123)) assert result is False @@ -89,7 +85,7 @@ async def test_check_ownership_when_async_cache_and_application_owner(self): assert result is True mock_client.rest.fetch_application.assert_not_called() mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) mock_cache.get.assert_awaited_once_with(default=None) @@ -107,7 +103,7 @@ async def test_check_ownership_when_async_cache_but_not_application_owner(self): assert result is False mock_client.rest.fetch_application.assert_not_called() mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) mock_cache.get.assert_awaited_once_with(default=None) @@ -127,7 +123,7 @@ async def test_check_ownership_when_when_async_cache_and_application_team_member assert result is True mock_client.rest.fetch_application.assert_not_called() mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) mock_cache.get.assert_awaited_once_with(default=None) @@ -148,7 +144,7 @@ async def test_check_ownership_when_async_cache_but_not_team_member(self): assert result is False mock_client.rest.fetch_application.assert_not_called() mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) mock_cache.get.assert_awaited_once_with(default=None) @@ -167,7 +163,7 @@ async def test_check_ownership_when_async_cache_returns_none_application_owner(s assert result is True mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) mock_cache.get.assert_awaited_once_with(default=None) mock_client.rest.fetch_application.assert_awaited_once_with() @@ -176,7 +172,7 @@ async def test_check_ownership_when_async_cache_returns_none_application_owner(s async def test_check_ownership_when_token_type_is_not_bot(self): check = tanjun.dependencies.Owners(owners=[123, 7634]) mock_client = mock.Mock(tanjun.Client) - mock_client.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_client.get_type_dependency.return_value = None mock_client.rest.token_type = hikari.TokenType.BEARER result = await check.check_ownership(mock_client, mock.Mock(id=54123123)) @@ -184,14 +180,14 @@ async def test_check_ownership_when_token_type_is_not_bot(self): assert result is False mock_client.rest.fetch_application.assert_not_called() mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) @pytest.mark.asyncio async def test_check_ownership_when_application_owner(self): check = tanjun.dependencies.Owners(owners=[123, 7634]) mock_client = mock.Mock(tanjun.Client) - mock_client.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_client.get_type_dependency.return_value = None application = mock.Mock(owner=mock.Mock(id=654234), team=None) mock_client.rest.fetch_application = mock.AsyncMock(return_value=application) mock_client.rest.token_type = hikari.TokenType.BOT @@ -201,14 +197,14 @@ async def test_check_ownership_when_application_owner(self): assert result is True mock_client.rest.fetch_application.assert_awaited_once_with() mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) @pytest.mark.asyncio async def test_check_ownership_when_not_application_owner(self): check = tanjun.dependencies.Owners(owners=[123, 7634]) mock_client = mock.Mock(tanjun.Client) - mock_client.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_client.get_type_dependency.return_value = None application = mock.Mock(owner=mock.Mock(id=654234), team=None) mock_client.rest.fetch_application = mock.AsyncMock(return_value=application) mock_client.rest.token_type = hikari.TokenType.BOT @@ -218,14 +214,14 @@ async def test_check_ownership_when_not_application_owner(self): assert result is False mock_client.rest.fetch_application.assert_awaited_once_with() mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) @pytest.mark.asyncio async def test_check_ownership_when_application_team_member(self): check = tanjun.dependencies.Owners(owners=[123, 7634]) mock_client = mock.Mock(tanjun.Client) - mock_client.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_client.get_type_dependency.return_value = None application = mock.Mock( owner=mock.Mock(id=654234), team=mock.Mock(members={54123: mock.Mock(), 64123: mock.Mock()}) ) @@ -237,14 +233,14 @@ async def test_check_ownership_when_application_team_member(self): assert result is True mock_client.rest.fetch_application.assert_awaited_once_with() mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) @pytest.mark.asyncio async def test_check_ownership_when_not_team_member(self): check = tanjun.dependencies.Owners(owners=[123, 7634]) mock_client = mock.Mock(tanjun.Client) - mock_client.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_client.get_type_dependency.return_value = None application = mock.Mock( owner=mock.Mock(id=654234), team=mock.Mock(members={54123: mock.Mock(), 64123: mock.Mock()}) ) @@ -256,14 +252,14 @@ async def test_check_ownership_when_not_team_member(self): assert result is False mock_client.rest.fetch_application.assert_awaited_once_with() mock_client.get_type_dependency.assert_called_once_with( - tanjun.dependencies.SingleStoreCache[hikari.Application] + tanjun.dependencies.SingleStoreCache[hikari.Application], default=None ) @pytest.mark.asyncio async def test_check_ownership_application_caching_behaviour(self): check = tanjun.dependencies.Owners(owners=[123, 7634]) mock_client = mock.Mock(tanjun.Client) - mock_client.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_client.get_type_dependency.return_value = None application = mock.Mock( owner=mock.Mock(id=654234), team=mock.Mock(members={54123: mock.Mock(), 64123: mock.Mock()}) ) @@ -282,7 +278,7 @@ async def test_check_ownership_application_expires_cache( ): check = tanjun.dependencies.Owners(expire_after=expire_after) mock_client = mock.Mock(tanjun.Client) - mock_client.get_type_dependency.return_value = alluka.abc.UNDEFINED + mock_client.get_type_dependency.return_value = None application_1 = mock.Mock(team=mock.Mock(members={54123: mock.Mock(), 64123: mock.Mock()})) application_2 = mock.Mock(team=mock.Mock(members={64123: mock.Mock()})) mock_client.rest.fetch_application = mock.AsyncMock(side_effect=[application_1, application_2]) diff --git a/tests/test_clients.py b/tests/test_clients.py index f95b642f5..78c70aa0d 100644 --- a/tests/test_clients.py +++ b/tests/test_clients.py @@ -1116,12 +1116,16 @@ def test_remove_listener_when_event_type_not_present(self): def test_remove_listener_when_callback_not_present(self): mock_other_callback = mock.Mock() - client = tanjun.Client(mock.Mock()).add_listener(hikari.GuildTypingEvent, mock_other_callback) + mock_injector = mock.Mock() + client = tanjun.Client(mock.Mock(), injector=mock_injector).add_listener( + hikari.GuildTypingEvent, mock_other_callback + ) with pytest.raises(ValueError, match=".+"): client.remove_listener(hikari.GuildTypingEvent, mock.Mock()) - assert client.listeners[hikari.GuildTypingEvent] == [mock_other_callback] + assert client.listeners[hikari.GuildTypingEvent] == [mock_injector.auto_inject_async.return_value] + mock_injector.auto_inject_async.assert_called_once_with(mock_other_callback) def test_remove_listener_when_last_listener(self): mock_callback = mock.Mock() @@ -1152,9 +1156,7 @@ def test_remove_listener_when_alive_and_events(self): client.remove_listener(hikari.RoleEvent, mock_callback) assert hikari.RoleEvent not in client.listeners - mock_events.unsubscribe.assert_called_once_with( - hikari.RoleEvent, mock_injector.as_async_self_injecting.return_value.__call__ - ) + mock_events.unsubscribe.assert_called_once_with(hikari.RoleEvent, mock_injector.auto_inject_async.return_value) def test_remove_listener_when_events(self): mock_events = mock.Mock() diff --git a/tests/test_injecting.py b/tests/test_injecting.py index d44bc93c7..c04c9c150 100644 --- a/tests/test_injecting.py +++ b/tests/test_injecting.py @@ -33,6 +33,7 @@ import alluka import mock +import pytest import tanjun @@ -41,7 +42,8 @@ def test_as_self_injecting(): mock_callback = mock.Mock() mock_client = mock.Mock() - result = tanjun.injecting.as_self_injecting(mock_client)(mock_callback) + with pytest.warns(DeprecationWarning): + result = tanjun.injecting.as_self_injecting(mock_client)(mock_callback) assert result.callback is mock_callback assert result._client is mock_client.injector @@ -55,20 +57,15 @@ def test_aliases(): "Injected", "InjectorClient", "SelfInjectingCallback", - "UNDEFINED", - "Undefined", - "UndefinedOr", "as_self_injecting", "inject", "injected", } - assert tanjun.injecting.SelfInjectingCallback is alluka.AsyncSelfInjecting + assert tanjun.injecting.SelfInjectingCallback is alluka.AsyncSelfInjecting # pyright: ignore[reportDeprecated] assert tanjun.injecting.BasicInjectionContext is alluka.BasicContext assert tanjun.injecting.InjectorClient is alluka.Client assert tanjun.injecting.Injected is alluka.Injected assert tanjun.injecting.inject is alluka.inject assert tanjun.injecting.injected is alluka.inject - assert tanjun.injecting.UNDEFINED is alluka.abc.UNDEFINED assert tanjun.injecting.CallbackSig is alluka.abc.CallbackSig assert tanjun.injecting.AbstractInjectionContext is alluka.abc.Context - assert tanjun.injecting.Undefined is alluka.abc.Undefined