Skip to content

Commit 840aa84

Browse files
Replace usages of type vars for Self return with typing_extensions.Self
1 parent 807da66 commit 840aa84

File tree

12 files changed

+275
-284
lines changed

12 files changed

+275
-284
lines changed

tanjun/abc.py

Lines changed: 61 additions & 52 deletions
Large diffs are not rendered by default.

tanjun/clients.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@
6565

6666
import typing_extensions
6767

68-
_ClientT = typing.TypeVar("_ClientT", bound="Client")
69-
7068
class _MessageContextMakerProto(typing.Protocol):
7169
def __call__(
7270
self,
@@ -1104,7 +1102,7 @@ async def declare_application_commands(
11041102

11051103
return responses
11061104

1107-
def set_auto_defer_after(self: _ClientT, time: typing.Optional[float], /) -> _ClientT:
1105+
def set_auto_defer_after(self, time: typing.Optional[float], /) -> typing_extensions.Self:
11081106
"""Set when this client should automatically defer execution of commands.
11091107
11101108
.. warning::
@@ -1119,7 +1117,7 @@ def set_auto_defer_after(self: _ClientT, time: typing.Optional[float], /) -> _Cl
11191117
self._auto_defer_after = float(time) if time is not None else None
11201118
return self
11211119

1122-
def set_ephemeral_default(self: _ClientT, state: bool, /) -> _ClientT:
1120+
def set_ephemeral_default(self, state: bool, /) -> typing_extensions.Self:
11231121
"""Set whether slash contexts spawned by this client should default to ephemeral responses.
11241122
11251123
Parameters
@@ -1139,7 +1137,7 @@ def set_ephemeral_default(self: _ClientT, state: bool, /) -> _ClientT:
11391137
self._defaults_to_ephemeral = state
11401138
return self
11411139

1142-
def set_hikari_trait_injectors(self: _ClientT, bot: hikari_traits.RESTAware, /) -> _ClientT:
1140+
def set_hikari_trait_injectors(self, bot: hikari_traits.RESTAware, /) -> typing_extensions.Self:
11431141
"""Set type based dependency injection based on the hikari traits found in `bot`.
11441142
11451143
This is a short hand for calling `Client.add_type_dependency` for all
@@ -1156,7 +1154,7 @@ def set_hikari_trait_injectors(self: _ClientT, bot: hikari_traits.RESTAware, /)
11561154

11571155
return self
11581156

1159-
def set_interaction_not_found(self: _ClientT, message: typing.Optional[str], /) -> _ClientT:
1157+
def set_interaction_not_found(self, message: typing.Optional[str], /) -> typing_extensions.Self:
11601158
"""Set the response message for when an interaction command is not found.
11611159
11621160
.. warning::
@@ -1172,7 +1170,7 @@ def set_interaction_not_found(self: _ClientT, message: typing.Optional[str], /)
11721170
self._interaction_not_found = message
11731171
return self
11741172

1175-
def set_message_accepts(self: _ClientT, accepts: MessageAcceptsEnum, /) -> _ClientT:
1173+
def set_message_accepts(self, accepts: MessageAcceptsEnum, /) -> typing_extensions.Self:
11761174
"""Set the kind of messages commands should be executed based on.
11771175
11781176
Parameters
@@ -1186,7 +1184,9 @@ def set_message_accepts(self: _ClientT, accepts: MessageAcceptsEnum, /) -> _Clie
11861184
self._accepts = accepts
11871185
return self
11881186

1189-
def set_message_ctx_maker(self: _ClientT, maker: _MessageContextMakerProto = context.MessageContext, /) -> _ClientT:
1187+
def set_message_ctx_maker(
1188+
self, maker: _MessageContextMakerProto = context.MessageContext, /
1189+
) -> typing_extensions.Self:
11901190
"""Set the message context maker to use when creating context for a message.
11911191
11921192
.. warning::
@@ -1209,7 +1209,7 @@ def set_message_ctx_maker(self: _ClientT, maker: _MessageContextMakerProto = con
12091209
self._make_message_context = maker
12101210
return self
12111211

1212-
def set_slash_ctx_maker(self: _ClientT, maker: _SlashContextMakerProto = context.SlashContext, /) -> _ClientT:
1212+
def set_slash_ctx_maker(self, maker: _SlashContextMakerProto = context.SlashContext, /) -> typing_extensions.Self:
12131213
"""Set the slash context maker to use when creating context for a slash command.
12141214
12151215
.. warning::
@@ -1232,7 +1232,7 @@ def set_slash_ctx_maker(self: _ClientT, maker: _SlashContextMakerProto = context
12321232
self._make_slash_context = maker
12331233
return self
12341234

1235-
def set_human_only(self: _ClientT, value: bool = True) -> _ClientT:
1235+
def set_human_only(self, value: bool = True) -> typing_extensions.Self:
12361236
"""Set whether or not message commands execution should be limited to "human" users.
12371237
12381238
.. note::
@@ -1258,7 +1258,7 @@ def set_human_only(self: _ClientT, value: bool = True) -> _ClientT:
12581258

12591259
return self
12601260

1261-
def add_check(self: _ClientT, check: _CheckSig, /) -> _ClientT:
1261+
def add_check(self, check: _CheckSig, /) -> typing_extensions.Self:
12621262
"""Add a generic check to this client.
12631263
12641264
This will be applied to both message and slash command execution.
@@ -1280,7 +1280,7 @@ def add_check(self: _ClientT, check: _CheckSig, /) -> _ClientT:
12801280

12811281
return self
12821282

1283-
def remove_check(self: _ClientT, check: _CheckSig, /) -> _ClientT:
1283+
def remove_check(self, check: _CheckSig, /) -> typing_extensions.Self:
12841284
"""Remove a check from the client.
12851285
12861286
Parameters
@@ -1317,7 +1317,9 @@ def with_check(self, check: _CheckSigT, /) -> _CheckSigT:
13171317
async def check(self, ctx: tanjun_abc.Context, /) -> bool:
13181318
return await utilities.gather_checks(ctx, self._checks)
13191319

1320-
def add_component(self: _ClientT, component: tanjun_abc.Component, /, *, add_injector: bool = False) -> _ClientT:
1320+
def add_component(
1321+
self, component: tanjun_abc.Component, /, *, add_injector: bool = False
1322+
) -> typing_extensions.Self:
13211323
"""Add a component to this client.
13221324
13231325
Parameters
@@ -1354,7 +1356,7 @@ def get_component_by_name(self, name: str, /) -> typing.Optional[tanjun_abc.Comp
13541356
# <<inherited docstring from tanjun.abc.Client>>.
13551357
return self._components.get(name)
13561358

1357-
def remove_component(self: _ClientT, component: tanjun_abc.Component, /) -> _ClientT:
1359+
def remove_component(self, component: tanjun_abc.Component, /) -> typing_extensions.Self:
13581360
# <<inherited docstring from tanjun.abc.Client>>.
13591361
stored_component = self._components.get(component.name)
13601362
if not stored_component or stored_component != component:
@@ -1373,13 +1375,13 @@ def remove_component(self: _ClientT, component: tanjun_abc.Component, /) -> _Cli
13731375

13741376
return self
13751377

1376-
def remove_component_by_name(self: _ClientT, name: str, /) -> _ClientT:
1378+
def remove_component_by_name(self, name: str, /) -> typing_extensions.Self:
13771379
# <<inherited docstring from tanjun.abc.Client>>.
13781380
return self.remove_component(self._components[name])
13791381

13801382
def add_client_callback(
1381-
self: _ClientT, name: typing.Union[str, tanjun_abc.ClientCallbackNames], callback: _MetaEventSig, /
1382-
) -> _ClientT:
1383+
self, name: typing.Union[str, tanjun_abc.ClientCallbackNames], callback: _MetaEventSig, /
1384+
) -> typing_extensions.Self:
13831385
# <<inherited docstring from tanjun.abc.Client>>.
13841386
descriptor = injecting.CallbackDescriptor(callback)
13851387
name = name.casefold()
@@ -1415,8 +1417,8 @@ def get_client_callbacks(
14151417
return ()
14161418

14171419
def remove_client_callback(
1418-
self: _ClientT, name: typing.Union[str, tanjun_abc.ClientCallbackNames], callback: _MetaEventSig, /
1419-
) -> _ClientT:
1420+
self, name: typing.Union[str, tanjun_abc.ClientCallbackNames], callback: _MetaEventSig, /
1421+
) -> typing_extensions.Self:
14201422
# <<inherited docstring from tanjun.abc.Client>>.
14211423
name = name.casefold()
14221424
self._client_callbacks[name].remove(typing.cast("injecting.CallbackDescriptor[None]", callback))
@@ -1435,7 +1437,9 @@ def decorator(callback: _MetaEventSigT, /) -> _MetaEventSigT:
14351437

14361438
return decorator
14371439

1438-
def add_listener(self: _ClientT, event_type: type[_EventT], callback: _ListenerCallbackSig[_EventT], /) -> _ClientT:
1440+
def add_listener(
1441+
self, event_type: type[_EventT], callback: _ListenerCallbackSig[_EventT], /
1442+
) -> typing_extensions.Self:
14391443
# <<inherited docstring from tanjun.abc.Client>>.
14401444
injected: injecting.SelfInjectingCallback[None] = injecting.SelfInjectingCallback(self, callback)
14411445
try:
@@ -1453,8 +1457,8 @@ def add_listener(self: _ClientT, event_type: type[_EventT], callback: _ListenerC
14531457
return self
14541458

14551459
def remove_listener(
1456-
self: _ClientT, event_type: type[_EventT], callback: _ListenerCallbackSig[_EventT], /
1457-
) -> _ClientT:
1460+
self, event_type: type[_EventT], callback: _ListenerCallbackSig[_EventT], /
1461+
) -> typing_extensions.Self:
14581462
# <<inherited docstring from tanjun.abc.Client>>.
14591463
index = self._listeners[event_type].index(typing.cast("injecting.SelfInjectingCallback[None]", callback))
14601464
registered_callback = self._listeners[event_type].pop(index)
@@ -1477,7 +1481,7 @@ def decorator(callback: _ListenerCallbackSigT, /) -> _ListenerCallbackSigT:
14771481

14781482
return decorator
14791483

1480-
def add_prefix(self: _ClientT, prefixes: typing.Union[collections.Iterable[str], str], /) -> _ClientT:
1484+
def add_prefix(self, prefixes: typing.Union[collections.Iterable[str], str], /) -> typing_extensions.Self:
14811485
"""Add a prefix used to filter message command calls.
14821486
14831487
This will be matched against the first character(s) in a message's
@@ -1504,7 +1508,7 @@ def add_prefix(self: _ClientT, prefixes: typing.Union[collections.Iterable[str],
15041508

15051509
return self
15061510

1507-
def remove_prefix(self: _ClientT, prefix: str, /) -> _ClientT:
1511+
def remove_prefix(self, prefix: str, /) -> typing_extensions.Self:
15081512
"""Remove a message content prefix from the client.
15091513
15101514
Parameters
@@ -1525,7 +1529,7 @@ def remove_prefix(self: _ClientT, prefix: str, /) -> _ClientT:
15251529
self._prefixes.remove(prefix)
15261530
return self
15271531

1528-
def set_prefix_getter(self: _ClientT, getter: typing.Optional[_PrefixGetterSig], /) -> _ClientT:
1532+
def set_prefix_getter(self, getter: typing.Optional[_PrefixGetterSig], /) -> typing_extensions.Self:
15291533
"""Set the callback used to retrieve message prefixes set for the relevant guild.
15301534
15311535
Parameters
@@ -1753,7 +1757,7 @@ async def fetch_rest_application_id(self) -> hikari.Snowflake:
17531757

17541758
return self._cached_application_id
17551759

1756-
def set_hooks(self: _ClientT, hooks: typing.Optional[tanjun_abc.AnyHooks], /) -> _ClientT:
1760+
def set_hooks(self, hooks: typing.Optional[tanjun_abc.AnyHooks], /) -> typing_extensions.Self:
17571761
"""Set the general command execution hooks for this client.
17581762
17591763
The callbacks within this hook will be added to every slash and message
@@ -1774,7 +1778,7 @@ def set_hooks(self: _ClientT, hooks: typing.Optional[tanjun_abc.AnyHooks], /) ->
17741778
self._hooks = hooks
17751779
return self
17761780

1777-
def set_slash_hooks(self: _ClientT, hooks: typing.Optional[tanjun_abc.SlashHooks], /) -> _ClientT:
1781+
def set_slash_hooks(self, hooks: typing.Optional[tanjun_abc.SlashHooks], /) -> typing_extensions.Self:
17781782
"""Set the slash command execution hooks for this client.
17791783
17801784
The callbacks within this hook will be added to every slash command
@@ -1796,7 +1800,7 @@ def set_slash_hooks(self: _ClientT, hooks: typing.Optional[tanjun_abc.SlashHooks
17961800
self._slash_hooks = hooks
17971801
return self
17981802

1799-
def set_message_hooks(self: _ClientT, hooks: typing.Optional[tanjun_abc.MessageHooks], /) -> _ClientT:
1803+
def set_message_hooks(self, hooks: typing.Optional[tanjun_abc.MessageHooks], /) -> typing_extensions.Self:
18001804
"""Set the message command execution hooks for this client.
18011805
18021806
The callbacks within this hook will be added to every message command
@@ -1842,7 +1846,7 @@ def _load_module(self, module: types.ModuleType, module_repr: str) -> None:
18421846
if not found:
18431847
raise RuntimeError(f"Didn't find any loaders in {module_repr}")
18441848

1845-
def load_modules(self: _ClientT, *modules: typing.Union[str, pathlib.Path], _log: bool = True) -> _ClientT:
1849+
def load_modules(self, *modules: typing.Union[str, pathlib.Path], _log: bool = True) -> typing_extensions.Self:
18461850
# <<inherited docstring from tanjun.abc.Client>>.
18471851
for module_path in modules:
18481852
if isinstance(module_path, str):
@@ -1889,7 +1893,7 @@ def _unload_module(self, module: types.ModuleType, module_repr: str) -> None:
18891893
if not found:
18901894
raise RuntimeError(f"Didn't find any unloaders in {module_repr}")
18911895

1892-
def unload_modules(self: _ClientT, *modules: typing.Union[str, pathlib.Path], _log: bool = True) -> _ClientT:
1896+
def unload_modules(self, *modules: typing.Union[str, pathlib.Path], _log: bool = True) -> typing_extensions.Self:
18931897
# <<inherited docstring from tanjun.abc.Client>>.
18941898
for module_path in modules:
18951899
if isinstance(module_path, str):
@@ -1909,7 +1913,7 @@ def unload_modules(self: _ClientT, *modules: typing.Union[str, pathlib.Path], _l
19091913

19101914
return self
19111915

1912-
def reload_modules(self: _ClientT, *modules: typing.Union[str, pathlib.Path]) -> _ClientT:
1916+
def reload_modules(self, *modules: typing.Union[str, pathlib.Path]) -> typing_extensions.Self:
19131917
# <<inherited docstring from tanjun.abc.Client>>.
19141918
for module_path in modules:
19151919
if isinstance(module_path, str):

0 commit comments

Comments
 (0)