Browse Source

Remove regular context manager support from Typing

pull/7494/head
Rapptz 3 years ago
parent
commit
0ed745f5ae
  1. 9
      discord/abc.py
  2. 22
      discord/context_managers.py

9
discord/abc.py

@ -1391,15 +1391,10 @@ class Messageable:
await self._state.http.send_typing(channel.id) await self._state.http.send_typing(channel.id)
def typing(self) -> Typing: def typing(self) -> Typing:
"""Returns a context manager that allows you to type for an indefinite period of time. """Returns an asynchronous context manager that allows you to type for an indefinite period of time.
This is useful for denoting long computations in your bot. This is useful for denoting long computations in your bot.
.. note::
This is both a regular context manager and an async context manager.
This means that both ``with`` and ``async with`` work with this.
Example Usage: :: Example Usage: ::
async with channel.typing(): async with channel.typing():
@ -1408,6 +1403,8 @@ class Messageable:
await channel.send('done!') await channel.send('done!')
.. versionchanged:: 2.0
This no longer works with the ``with`` syntax, ``async with`` must be used instead.
""" """
return Typing(self) return Typing(self)

22
discord/context_managers.py

@ -25,15 +25,13 @@ DEALINGS IN THE SOFTWARE.
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from typing import TYPE_CHECKING, TypeVar, Optional, Type from typing import TYPE_CHECKING, Optional, Type
if TYPE_CHECKING: if TYPE_CHECKING:
from .abc import Messageable from .abc import Messageable
from types import TracebackType from types import TracebackType
TypingT = TypeVar('TypingT', bound='Typing')
# fmt: off # fmt: off
__all__ = ( __all__ = (
'Typing', 'Typing',
@ -66,23 +64,11 @@ class Typing:
await typing(channel.id) await typing(channel.id)
await asyncio.sleep(5) await asyncio.sleep(5)
def __enter__(self: TypingT) -> TypingT: async def __aenter__(self) -> None:
self.task: asyncio.Task = self.loop.create_task(self.do_typing())
self.task.add_done_callback(_typing_done_callback)
return self
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
self.task.cancel()
async def __aenter__(self: TypingT) -> TypingT:
self._channel = channel = await self.messageable._get_channel() self._channel = channel = await self.messageable._get_channel()
await channel._state.http.send_typing(channel.id) await channel._state.http.send_typing(channel.id)
return self.__enter__() self.task: asyncio.Task = self.loop.create_task(self.do_typing())
self.task.add_done_callback(_typing_done_callback)
async def __aexit__( async def __aexit__(
self, self,

Loading…
Cancel
Save