-
-
Notifications
You must be signed in to change notification settings - Fork 494
feat: add proper typing support for bridge contexts #3251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “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? Sign in to your account
Open
AstreaTSS
wants to merge
8
commits into
Pycord-Development:master
Choose a base branch
from
AstreaTSS:hybrid-typing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−2
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
85552aa
feat: add proper typing support for bridge contexts
AstreaTSS 28e4f06
refactor: use __enter__ for __aenter__
AstreaTSS 87997ea
fix: make async context manager cancel on exit
AstreaTSS f310aaf
fix: add DeferTyping to `__all__`
AstreaTSS 6e7baf5
chore: update changelog
AstreaTSS 1e86184
Merge branch 'master' into hybrid-typing
Lulalaby a3c2834
Merge branch 'master' into hybrid-typing
Lulalaby e7c0eeb
fix: add default for DeferTyping
AstreaTSS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,23 +25,62 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from abc import ABC, abstractmethod | ||
| from typing import TYPE_CHECKING, Any, Union, overload | ||
|
|
||
| from typing_extensions import Self | ||
|
|
||
| from discord.commands import ApplicationContext | ||
| from discord.context_managers import Typing | ||
| from discord.interactions import Interaction, InteractionMessage | ||
| from discord.message import Message | ||
| from discord.webhook import WebhookMessage | ||
|
|
||
| from ..commands import Context | ||
|
|
||
| if TYPE_CHECKING: | ||
| from types import TracebackType | ||
|
|
||
| from .core import BridgeExtCommand, BridgeSlashCommand | ||
|
|
||
|
|
||
| __all__ = ("BridgeContext", "BridgeExtContext", "BridgeApplicationContext", "Context") | ||
|
|
||
|
|
||
| class DeferTyping: | ||
| def __init__(self, ctx: BridgeApplicationContext, ephemeral: bool) -> None: | ||
|
AstreaTSS marked this conversation as resolved.
Outdated
|
||
| self.loop: asyncio.AbstractEventLoop = ctx._state.loop | ||
| self.ctx = ctx | ||
| self.ephemeral = ephemeral | ||
|
|
||
| async def do_defer(self) -> None: | ||
| await self.ctx.defer(ephemeral=self.ephemeral) | ||
|
|
||
| def __enter__(self) -> Self: | ||
| self.task: asyncio.Task = self.loop.create_task(self.do_defer()) | ||
| return self | ||
|
|
||
| def __exit__( | ||
| self, | ||
| exc_type: type[BaseException] | None, | ||
| exc_value: BaseException | None, | ||
| traceback: TracebackType | None, | ||
| ) -> None: | ||
| self.task.cancel() | ||
|
|
||
| async def __aenter__(self) -> Self: | ||
| return self.__enter__() | ||
|
|
||
| async def __aexit__( | ||
| self, | ||
| exc_type: type[BaseException] | None, | ||
| exc_value: BaseException | None, | ||
| traceback: TracebackType | None, | ||
| ) -> None: | ||
| pass | ||
|
AstreaTSS marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| class BridgeContext(ABC): | ||
| """ | ||
| The base context class for compatibility commands. This class is an :term:`abstract base class` (also known as an | ||
|
|
@@ -74,6 +113,9 @@ async def _respond( | |
| @abstractmethod | ||
| async def _defer(self, *args, **kwargs) -> None: ... | ||
|
|
||
| @abstractmethod | ||
| def _typing(self, *args, **kwargs) -> Typing | DeferTyping: ... | ||
|
|
||
| @abstractmethod | ||
| async def _edit(self, *args, **kwargs) -> InteractionMessage | Message: ... | ||
|
|
||
|
|
@@ -111,6 +153,14 @@ async def defer(self, *args, **kwargs) -> None: | |
| """ | ||
| return await self._defer(*args, **kwargs) | ||
|
|
||
| def typing(self, *args, **kwargs) -> Typing | DeferTyping: | ||
| """ | ||
| Returns a context manager that allows you to type for an indefinite period of time. | ||
| In :class:`BridgeExtContext`, this will be :meth:`~Context.typing` while in :class:`BridgeApplicationContext`, | ||
| this is equivalent to a defer() call and does not do any typing calls. | ||
| """ | ||
| return self._typing(*args, **kwargs) | ||
|
Comment on lines
+162
to
+168
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need both typing and _typing ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the template method pattern already used by |
||
|
|
||
| async def edit(self, *args, **kwargs) -> InteractionMessage | Message: | ||
| """|coro| | ||
|
|
||
|
|
@@ -147,6 +197,9 @@ async def _respond(self, *args, **kwargs) -> Interaction | WebhookMessage: | |
| async def _defer(self, *args, **kwargs) -> None: | ||
| return await self._get_super("defer")(*args, **kwargs) | ||
|
|
||
| def _typing(self, *args, **kwargs) -> DeferTyping: | ||
| return DeferTyping(self, *args, **kwargs) | ||
|
|
||
| async def _edit(self, *args, **kwargs) -> InteractionMessage: | ||
| return await self._get_super("edit")(*args, **kwargs) | ||
|
|
||
|
|
@@ -174,6 +227,10 @@ async def _defer(self, *args, **kwargs) -> None: | |
| kwargs.pop("ephemeral", None) | ||
| return await self._get_super("trigger_typing")(*args, **kwargs) | ||
|
|
||
| def _typing(self, *args, **kwargs) -> Typing: | ||
| kwargs.pop("ephemeral", None) | ||
| return self._get_super("typing")(*args, **kwargs) | ||
|
|
||
| async def _edit(self, *args, **kwargs) -> Message | None: | ||
| if self._original_response_message: | ||
| return await self._original_response_message.edit(*args, **kwargs) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.