Skip to content

Conversation

@diceroll123
Copy link
Contributor

Summary

ty gives this,

image

Allows interaction.client to narrow to the bot class instead of the basic discord.Client.

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

@The-Red-Dragons
Copy link

👍 Great fix! This addresses a common pain point for type-safe Discord bot development.

We maintain a large production Discord bot and frequently encounter this issue when subclassing callbacks. Currently, we have to use type ignores or casts in views, modals, and error handlers:

class MyView(discord.ui.View):
    async def on_error(self, interaction: discord.Interaction, error: Exception, item):
        # Currently required workaround:
        bot: MyBot = interaction.client  # type: ignore[assignment]
        await bot.error_manager.handle(interaction, error)

With this PR, the generic parameter will properly narrow interaction.client to our custom bot class, eliminating the need for type ignores.

One minor suggestion: Is Modal.on_error() also affected by this issue? I noticed it's not included in the current changeset - might be worth checking if it needs the same treatment.

Thanks for the fix! 🎉

@diceroll123
Copy link
Contributor Author

Modal.on_error seems fine,

async def on_error(self, interaction: Interaction[ClientT], error: Exception, /) -> None:

@TheServerit
Copy link

Though Pyright on Strict mode complains even in places where the base function already does implement interaction: Interaction[ClientT]:

"Interaction[ClientT@callback]" is not assignable to "Interaction[Bot]"
  Type parameter "ClientT@Interaction" is covariant, but "ClientT@callback" is not a subtype of "Bot"
    "Client*" is not assignable to "Bot"

(Bot is my subclass of AutoShardedBot which subclasses Client)
So unless I misunderstand something, I assume this PR will not fix this issue in particular?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants