diff options
author | 2023-05-06 20:09:19 +0100 | |
---|---|---|
committer | 2023-05-31 13:03:54 +0100 | |
commit | ed602405df8e4ddf9e7993e42eea9a5e9afd4856 (patch) | |
tree | 3c2284b9d1ef15fa423875be832207b2a06ca291 /pydis_core/utils | |
parent | Bump action step versions in CI (diff) |
Apply fixes for ruff linting
Diffstat (limited to 'pydis_core/utils')
-rw-r--r-- | pydis_core/utils/__init__.py | 14 | ||||
-rw-r--r-- | pydis_core/utils/_monkey_patches.py | 11 | ||||
-rw-r--r-- | pydis_core/utils/commands.py | 3 | ||||
-rw-r--r-- | pydis_core/utils/cooldown.py | 3 | ||||
-rw-r--r-- | pydis_core/utils/function.py | 2 | ||||
-rw-r--r-- | pydis_core/utils/interactions.py | 9 | ||||
-rw-r--r-- | pydis_core/utils/logging.py | 2 | ||||
-rw-r--r-- | pydis_core/utils/members.py | 2 | ||||
-rw-r--r-- | pydis_core/utils/scheduling.py | 13 |
9 files changed, 25 insertions, 34 deletions
diff --git a/pydis_core/utils/__init__.py b/pydis_core/utils/__init__.py index 0542231e..8a61082a 100644 --- a/pydis_core/utils/__init__.py +++ b/pydis_core/utils/__init__.py @@ -1,17 +1,7 @@ """Useful utilities and tools for Discord bot development.""" from pydis_core.utils import ( - _monkey_patches, - caching, - channel, - commands, - cooldown, - function, - interactions, - logging, - members, - regex, - scheduling, + _monkey_patches, caching, channel, commands, cooldown, function, interactions, logging, members, regex, scheduling ) from pydis_core.utils._extensions import unqualify @@ -47,4 +37,4 @@ __all__ = [ unqualify, ] -__all__ = list(map(lambda module: module.__name__, __all__)) +__all__ = [module.__name__ for module in __all__] diff --git a/pydis_core/utils/_monkey_patches.py b/pydis_core/utils/_monkey_patches.py index f0a8dc9c..2df56039 100644 --- a/pydis_core/utils/_monkey_patches.py +++ b/pydis_core/utils/_monkey_patches.py @@ -1,8 +1,7 @@ """Contains all common monkey patches, used to alter discord to fit our needs.""" import logging -import typing -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from functools import partial, partialmethod from discord import Forbidden, http @@ -24,7 +23,7 @@ class _Command(commands.Command): super().__init__(*args, **kwargs) self.root_aliases = kwargs.get("root_aliases", []) - if not isinstance(self.root_aliases, (list, tuple)): + if not isinstance(self.root_aliases, list | tuple): raise TypeError("Root aliases of a command must be a list or a tuple of strings.") @@ -47,17 +46,17 @@ def _patch_typing() -> None: log.debug("Patching send_typing, which should fix things breaking when Discord disables typing events. Stay safe!") original = http.HTTPClient.send_typing - last_403: typing.Optional[datetime] = None + last_403: datetime | None = None async def honeybadger_type(self: http.HTTPClient, channel_id: int) -> None: nonlocal last_403 - if last_403 and (datetime.utcnow() - last_403) < timedelta(minutes=5): + if last_403 and (datetime.now(tz=timezone.utc) - last_403) < timedelta(minutes=5): log.warning("Not sending typing event, we got a 403 less than 5 minutes ago.") return try: await original(self, channel_id) except Forbidden: - last_403 = datetime.utcnow() + last_403 = datetime.now(tz=timezone.utc) log.warning("Got a 403 from typing event!") http.HTTPClient.send_typing = honeybadger_type diff --git a/pydis_core/utils/commands.py b/pydis_core/utils/commands.py index 7afd8137..2bc5b668 100644 --- a/pydis_core/utils/commands.py +++ b/pydis_core/utils/commands.py @@ -1,10 +1,9 @@ -from typing import Optional from discord import Message from discord.ext.commands import BadArgument, Context, clean_content -async def clean_text_or_reply(ctx: Context, text: Optional[str] = None) -> str: +async def clean_text_or_reply(ctx: Context, text: str | None = None) -> str: """ Cleans a text argument or replied message's content. diff --git a/pydis_core/utils/cooldown.py b/pydis_core/utils/cooldown.py index 5129befd..0fe7102a 100644 --- a/pydis_core/utils/cooldown.py +++ b/pydis_core/utils/cooldown.py @@ -26,6 +26,7 @@ _HashableArgsTuple = tuple[Hashable, ...] if typing.TYPE_CHECKING: import typing_extensions + from pydis_core import BotBase P = typing.ParamSpec("P") @@ -34,7 +35,7 @@ R = typing.TypeVar("R") """The command's return value.""" -class CommandOnCooldown(CommandError, typing.Generic[P, R]): +class CommandOnCooldown(CommandError, typing.Generic[P, R]): # noqa: N818 """Raised when a command is invoked while on cooldown.""" def __init__( diff --git a/pydis_core/utils/function.py b/pydis_core/utils/function.py index d89163ec..98737af0 100644 --- a/pydis_core/utils/function.py +++ b/pydis_core/utils/function.py @@ -99,7 +99,7 @@ def command_wraps( Returns: A decorator that behaves like :func:`functools.wraps`, with the wrapper replaced with the function :func:`update_wrapper_globals` returned. - """ # noqa: D200 + """ def decorator(wrapper: Callable[_P, _R]) -> Callable[_P, _R]: return functools.update_wrapper( update_wrapper_globals(wrapper, wrapped, ignored_conflict_names=ignored_conflict_names), diff --git a/pydis_core/utils/interactions.py b/pydis_core/utils/interactions.py index a6746e1e..6e419342 100644 --- a/pydis_core/utils/interactions.py +++ b/pydis_core/utils/interactions.py @@ -1,4 +1,5 @@ -from typing import Literal, Optional, Sequence +from collections.abc import Sequence +from typing import Literal from discord import ButtonStyle, HTTPException, Interaction, Message, NotFound, ui @@ -43,8 +44,8 @@ class ViewWithUserAndRoleCheck(ui.View): *, allowed_users: Sequence[int], allowed_roles: Sequence[int], - timeout: Optional[float] = 180.0, - message: Optional[Message] = None + timeout: float | None = 180.0, + message: Message | None = None ) -> None: super().__init__(timeout=timeout) self.allowed_users = allowed_users @@ -97,7 +98,7 @@ class DeleteMessageButton(ui.Button): style (:literal-url:`ButtonStyle <https://discordpy.readthedocs.io/en/latest/interactions/api.html#discord.ButtonStyle>`): The style of the button, set to ``ButtonStyle.secondary`` if not specified. label: The label of the button, set to "Delete" if not specified. - """ # noqa: E501 + """ def __init__( self, diff --git a/pydis_core/utils/logging.py b/pydis_core/utils/logging.py index 7814f348..ecccb91a 100644 --- a/pydis_core/utils/logging.py +++ b/pydis_core/utils/logging.py @@ -32,7 +32,7 @@ class CustomLogger(LoggerClass): self.log(TRACE_LEVEL, msg, *args, **kwargs) -def get_logger(name: typing.Optional[str] = None) -> CustomLogger: +def get_logger(name: str | None = None) -> CustomLogger: """ Utility to make mypy recognise that logger is of type :obj:`CustomLogger`. diff --git a/pydis_core/utils/members.py b/pydis_core/utils/members.py index fa8481cc..542f945f 100644 --- a/pydis_core/utils/members.py +++ b/pydis_core/utils/members.py @@ -9,7 +9,7 @@ from pydis_core.utils import logging log = logging.get_logger(__name__) -async def get_or_fetch_member(guild: discord.Guild, member_id: int) -> typing.Optional[discord.Member]: +async def get_or_fetch_member(guild: discord.Guild, member_id: int) -> discord.Member | None: """ Attempt to get a member from cache; on failure fetch from the API. diff --git a/pydis_core/utils/scheduling.py b/pydis_core/utils/scheduling.py index d4458bc1..1e2e25e7 100644 --- a/pydis_core/utils/scheduling.py +++ b/pydis_core/utils/scheduling.py @@ -5,7 +5,7 @@ import contextlib import inspect import typing from collections import abc -from datetime import datetime +from datetime import datetime, timezone from functools import partial from pydis_core.utils import logging @@ -69,7 +69,8 @@ class Scheduler: self._log.trace(f"Scheduling task #{task_id}...") msg = f"Cannot schedule an already started coroutine for #{task_id}" - assert inspect.getcoroutinestate(coroutine) == "CORO_CREATED", msg + if inspect.getcoroutinestate(coroutine) != "CORO_CREATED": + raise ValueError(msg) if task_id in self._scheduled_tasks: self._log.debug(f"Did not schedule task #{task_id}; task was already scheduled.") @@ -99,7 +100,7 @@ class Scheduler: task_id: A unique ID to create the task with. coroutine: The function to be called. """ - now_datetime = datetime.now(time.tzinfo) if time.tzinfo else datetime.utcnow() + now_datetime = datetime.now(time.tzinfo) if time.tzinfo else datetime.now(tz=timezone.utc) delay = (time - now_datetime).total_seconds() if delay > 0: coroutine = self._await_later(delay, task_id, coroutine) @@ -108,7 +109,7 @@ class Scheduler: def schedule_later( self, - delay: typing.Union[int, float], + delay: int | float, task_id: abc.Hashable, coroutine: abc.Coroutine ) -> None: @@ -152,7 +153,7 @@ class Scheduler: async def _await_later( self, - delay: typing.Union[int, float], + delay: int | float, task_id: abc.Hashable, coroutine: abc.Coroutine ) -> None: @@ -218,7 +219,7 @@ def create_task( coro: abc.Coroutine[typing.Any, typing.Any, TASK_RETURN], *, suppressed_exceptions: tuple[type[Exception], ...] = (), - event_loop: typing.Optional[asyncio.AbstractEventLoop] = None, + event_loop: asyncio.AbstractEventLoop | None = None, **kwargs, ) -> asyncio.Task[TASK_RETURN]: """ |