Skip to content

feat: make context have generic client types #1699

New issue

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

By clicking “No 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? No Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2024

Conversation

AstreaTSS
Copy link
Member

Pull Request Type

  • Feature addition
  • Bugfix
  • Documentation update
  • Code refactor
  • Tests improvement
  • CI/CD pipeline enhancement
  • Other: [Replace with a description]

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

  • 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

  • I've ensured my code works on Python 3.10.x
  • I've ensured my code works on Python 3.11.x

Checklist

  • I've run the pre-commit code linter over all edited files
  • 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

@silasary silasary merged commit de8c91d into interactions-py:unstable Jun 20, 2024
2 checks passed
No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants