This PR makes the contexts a generic type holding ClientT, a TypeVar that represents clients and subclasses. Taking advantage of PEP 696, this PR ensures a grand total of 0 typings are broken by using default (thanks to typing_extensions).
This allows for people to type out their command functions in a pretty clean way if they make a subclass of Client - previously, you would have to make your own Context class that replaced the Client references with your own, but now it's as easy as the test scenario.
This doesn't cover every possible thing that could use ClientT, and especially not everything that could use TypeVar(default=X), but it's a start
Changes
Introduce ClientT in const. To get around circular imports, during runtime, all ClientT is is ClientT = TypeVar("ClientT"), but when type checking, it's far more accurate.
Use ClientT in the Contexts as a generic.
Adjust Client's custom contexts to use the new generic property.
Related Issues
Test Scenarios
import interactions as ipy
class MyBotClient(ipy.Client):
def say_hi(self) -> None:
print("Hello!")
@ipy.slash_command()
async def test(ctx: ipy.SlashContext[MyBotClient]):
ctx.bot.say_hi() # typehints perfectly, instead of being unknown
Python Compatibility
[x] I've ensured my code works on Python 3.10.x
[ ] I've ensured my code works on Python 3.11.x
Checklist
[x] I've run the pre-commit code linter over all edited files
[x] I've tested my changes on supported Python versions
[ ] I've added tests for my code, if applicable
[ ] I've updated / added documentation, where applicable
Pull Request Type
Description
This PR makes the contexts a generic type holding
ClientT
, aTypeVar
that represents clients and subclasses. Taking advantage of PEP 696, this PR ensures a grand total of 0 typings are broken by usingdefault
(thanks totyping_extensions
).This allows for people to type out their command functions in a pretty clean way if they make a subclass of
Client
- previously, you would have to make your ownContext
class that replaced theClient
references with your own, but now it's as easy as the test scenario.This doesn't cover every possible thing that could use
ClientT
, and especially not everything that could useTypeVar(default=X)
, but it's a startChanges
ClientT
inconst
. To get around circular imports, during runtime, allClientT
is isClientT = TypeVar("ClientT")
, but when type checking, it's far more accurate.ClientT
in theContext
s as a generic.Client
's custom contexts to use the new generic property.Related Issues
Test Scenarios
Python Compatibility
3.10.x
3.11.x
Checklist
pre-commit
code linter over all edited files