interactions-py / interactions.py

A highly extensible, easy to use, and feature complete bot framework for Discord
https://interactions-py.github.io/interactions.py/
MIT License
833 stars 185 forks source link

feat: make context have generic client types #1699

Closed AstreaTSS closed 2 months ago

AstreaTSS commented 3 months ago

Pull Request Type

Description

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

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

Checklist