extract raise_expected_coro further

This commit is contained in:
chillymosh
2021-08-28 22:52:58 +01:00
parent 1829048c80
commit 73a4996aa9
3 changed files with 30 additions and 36 deletions

View File

@@ -22,6 +22,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from TwitchIO.twitchio.ext.commands import utils
from __future__ import annotations
@@ -43,6 +44,7 @@ from .context import Context
from . import errors
from .help import HelpCommand, DefaultHelpCommand
from .cog import Cog
from discord.utils import raise_expected_coro
if TYPE_CHECKING:
import importlib.machinery
@@ -379,7 +381,7 @@ class BotBase(GroupMixin):
TypeError
The coroutine passed is not actually a coroutine.
"""
return self._raise_expected_coro(
return raise_expected_coro(
coro, 'The pre-invoke hook must be a coroutine.'
)
@@ -410,17 +412,11 @@ class BotBase(GroupMixin):
TypeError
The coroutine passed is not actually a coroutine.
"""
return self._raise_expected_coro(
return raise_expected_coro(
coro, 'The post-invoke hook must be a coroutine.'
)
def _raise_expected_coro(self, coro, arg1):
if not asyncio.iscoroutinefunction(coro):
raise TypeError(arg1)
return coro
# listener registration
def add_listener(self, func: CoroFunc, name: str = MISSING) -> None:

View File

@@ -46,7 +46,9 @@ import traceback
from collections.abc import Sequence
from discord.backoff import ExponentialBackoff
from discord.utils import MISSING
from discord.utils import MISSING, raise_expected_coro
#from discord.utils import raise_expected_coro
__all__ = (
'loop',
@@ -488,7 +490,7 @@ class Loop(Generic[LF]):
The function was not a coroutine.
"""
return self._raise_expected_coro(coro)
return raise_expected_coro(coro, f'Expected coroutine function, received {coro.__class__.__name__!r}.')
def after_loop(self, coro: FT) -> FT:
"""A decorator that register a coroutine to be called after the loop finished running.
@@ -512,7 +514,7 @@ class Loop(Generic[LF]):
The function was not a coroutine.
"""
return self._raise_expected_coro(coro)
return raise_expected_coro(coro, f'Expected coroutine function, received {coro.__class__.__name__!r}.')
def error(self, coro: ET) -> ET:
"""A decorator that registers a coroutine to be called if the task encounters an unhandled exception.
@@ -534,15 +536,7 @@ class Loop(Generic[LF]):
TypeError
The function was not a coroutine.
"""
return self._raise_expected_coro(coro)
def _raise_expected_coro(self, coro):
if not inspect.iscoroutinefunction(coro):
raise TypeError(
f'Expected coroutine function, received {coro.__class__.__name__!r}.'
)
return coro
return raise_expected_coro(coro, f'Expected coroutine function, received {coro.__class__.__name__!r}.')
def _get_next_sleep_time(self) -> datetime.datetime:
if self._sleep is not MISSING: