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 | |
parent | Bump action step versions in CI (diff) |
Apply fixes for ruff linting
Diffstat (limited to 'pydis_core')
-rw-r--r-- | pydis_core/__init__.py | 2 | ||||
-rw-r--r-- | pydis_core/_bot.py | 23 | ||||
-rw-r--r-- | pydis_core/async_stats.py | 9 | ||||
-rw-r--r-- | pydis_core/exts/__init__.py | 2 | ||||
-rw-r--r-- | pydis_core/site_api.py | 19 | ||||
-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 |
14 files changed, 51 insertions, 63 deletions
diff --git a/pydis_core/__init__.py b/pydis_core/__init__.py index a09feeaa..94f4ac10 100644 --- a/pydis_core/__init__.py +++ b/pydis_core/__init__.py @@ -12,4 +12,4 @@ __all__ = [ StartupError, ] -__all__ = list(map(lambda module: module.__name__, __all__)) +__all__ = [module.__name__ for module in __all__] diff --git a/pydis_core/_bot.py b/pydis_core/_bot.py index b45647f3..57336460 100644 --- a/pydis_core/_bot.py +++ b/pydis_core/_bot.py @@ -3,7 +3,6 @@ import socket import types import warnings from contextlib import suppress -from typing import Optional import aiohttp import discord @@ -19,7 +18,7 @@ try: from async_rediscache import RedisSession REDIS_AVAILABLE = True except ImportError: - RedisSession = None + RedisSession = object REDIS_AVAILABLE = False log = get_logger() @@ -42,9 +41,9 @@ class BotBase(commands.Bot): guild_id: int, allowed_roles: list, http_session: aiohttp.ClientSession, - redis_session: Optional[RedisSession] = None, - api_client: Optional[APIClient] = None, - statsd_url: Optional[str] = None, + redis_session: RedisSession | None = None, + api_client: APIClient | None = None, + statsd_url: str | None = None, **kwargs, ): """ @@ -77,16 +76,16 @@ class BotBase(commands.Bot): elif redis_session: self.redis_session = redis_session - self._resolver: Optional[aiohttp.AsyncResolver] = None - self._connector: Optional[aiohttp.TCPConnector] = None + self._resolver: aiohttp.AsyncResolver | None = None + self._connector: aiohttp.TCPConnector | None = None - self._statsd_timerhandle: Optional[asyncio.TimerHandle] = None - self._guild_available: Optional[asyncio.Event] = None + self._statsd_timerhandle: asyncio.TimerHandle | None = None + self._guild_available: asyncio.Event | None = None self._extension_loading_task: asyncio.Task | None = None - self.stats: Optional[AsyncStatsClient] = None + self.stats: AsyncStatsClient | None = None - self.all_extensions: Optional[frozenset[str]] = None + self.all_extensions: frozenset[str] | None = None def _connect_statsd( self, @@ -176,7 +175,7 @@ class BotBase(commands.Bot): super().add_command(command) self._add_root_aliases(command) - def remove_command(self, name: str) -> Optional[commands.Command]: + def remove_command(self, name: str) -> commands.Command | None: """ Remove a command/alias as normal and then remove its root aliases from the bot. diff --git a/pydis_core/async_stats.py b/pydis_core/async_stats.py index 411325e3..ae409467 100644 --- a/pydis_core/async_stats.py +++ b/pydis_core/async_stats.py @@ -2,7 +2,6 @@ import asyncio import socket -from typing import Optional from statsd.client.base import StatsClientBase @@ -15,7 +14,7 @@ class AsyncStatsClient(StatsClientBase): def __init__( self, loop: asyncio.AbstractEventLoop, - host: str = 'localhost', + host: str = "localhost", port: int = 8125, prefix: str = None ): @@ -35,7 +34,7 @@ class AsyncStatsClient(StatsClientBase): self._addr = addr self._prefix = prefix self._loop = loop - self._transport: Optional[asyncio.DatagramTransport] = None + self._transport: asyncio.DatagramTransport | None = None async def create_socket(self) -> None: """Use :obj:`asyncio.loop.create_datagram_endpoint` from the loop given on init to create a socket.""" @@ -51,7 +50,7 @@ class AsyncStatsClient(StatsClientBase): async def _async_send(self, data: str) -> None: """Send data to the statsd server using the async transport.""" - self._transport.sendto(data.encode('ascii'), self._addr) + self._transport.sendto(data.encode("ascii"), self._addr) -__all__ = ['AsyncStatsClient'] +__all__ = ["AsyncStatsClient"] diff --git a/pydis_core/exts/__init__.py b/pydis_core/exts/__init__.py index afd56166..9d59e8ad 100644 --- a/pydis_core/exts/__init__.py +++ b/pydis_core/exts/__init__.py @@ -1,4 +1,4 @@ """Reusable Discord cogs.""" __all__ = [] -__all__ = list(map(lambda module: module.__name__, __all__)) +__all__ = [module.__name__ for module in __all__] diff --git a/pydis_core/site_api.py b/pydis_core/site_api.py index c17d2642..80eeff2b 100644 --- a/pydis_core/site_api.py +++ b/pydis_core/site_api.py @@ -1,7 +1,6 @@ """An API wrapper around the Site API.""" import asyncio -from typing import Optional from urllib.parse import quote as quote_url import aiohttp @@ -17,8 +16,8 @@ class ResponseCodeError(ValueError): def __init__( self, response: aiohttp.ClientResponse, - response_json: Optional[dict] = None, - response_text: Optional[str] = None + response_json: dict | None = None, + response_text: str | None = None ): """ Initialize a new :obj:`ResponseCodeError` instance. @@ -42,7 +41,7 @@ class ResponseCodeError(ValueError): class APIClient: """A wrapper for the Django Site API.""" - session: Optional[aiohttp.ClientSession] = None + session: aiohttp.ClientSession | None = None loop: asyncio.AbstractEventLoop = None def __init__(self, site_api_url: str, site_api_token: str, **session_kwargs): @@ -57,13 +56,13 @@ class APIClient: self.site_api_url = site_api_url auth_headers = { - 'Authorization': f"Token {site_api_token}" + "Authorization": f"Token {site_api_token}" } - if 'headers' in session_kwargs: - session_kwargs['headers'].update(auth_headers) + if "headers" in session_kwargs: + session_kwargs["headers"].update(auth_headers) else: - session_kwargs['headers'] = auth_headers + session_kwargs["headers"] = auth_headers # aiohttp will complain if APIClient gets instantiated outside a coroutine. Thankfully, we # don't and shouldn't need to do that, so we can avoid scheduling a task to create it. @@ -134,7 +133,7 @@ class APIClient: """Equivalent to :meth:`APIClient.request` with PUT passed as the method.""" return await self.request("PUT", endpoint, raise_for_status=raise_for_status, **kwargs) - async def delete(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> Optional[dict]: + async def delete(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None: """ Send a DELETE request to the site API and return the JSON response. @@ -154,4 +153,4 @@ class APIClient: return await resp.json() -__all__ = ['APIClient', 'ResponseCodeError'] +__all__ = ["APIClient", "ResponseCodeError"] 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]: """ |