Skip to content

Commit

Permalink
refactor: add __slots__
Browse files Browse the repository at this point in the history
  • Loading branch information
null8626 committed Sep 28, 2024
1 parent 0cecb50 commit 5fa5aaa
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion topgg/autopost.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AutoPoster:
An instance of DBLClient.
"""

__slots__ = (
__slots__: t.Tuple[str, ...] = (
"_error",
"_success",
"_interval",
Expand Down
3 changes: 2 additions & 1 deletion topgg/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class DBLClient(DataContainerMixin):
Arbitrary kwargs to be passed to :class:`aiohttp.ClientSession` if session was not provided.
"""

__slots__ = ("http", "bot_id", "_token", "_is_closed", "_autopost")
__slots__: t.Tuple[str, ...] = ("http", "bot_id", "_token", "_is_closed", "_autopost")

http: HTTPClient

def __init__(
Expand Down
9 changes: 6 additions & 3 deletions topgg/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

__all__ = ("data", "DataContainerMixin",)
__all__ = (
"data",
"DataContainerMixin",
)

import inspect
import typing as t
Expand Down Expand Up @@ -61,7 +64,7 @@ def get_stats(client: Client = topgg.data(Client)):


class Data(t.Generic[T]):
__slots__ = ("type",)
__slots__: t.Tuple[str, ...] = ("type",)

def __init__(self, type_: t.Type[T]) -> None:
self.type: t.Type[T] = type_
Expand All @@ -75,7 +78,7 @@ class DataContainerMixin:
as arguments in your functions.
"""

__slots__ = ("_data",)
__slots__: t.Tuple[str, ...] = ("_data",)

def __init__(self) -> None:
self._data: t.Dict[t.Type, t.Any] = {type(self): self}
Expand Down
18 changes: 15 additions & 3 deletions topgg/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import sys
import warnings
from datetime import datetime
from typing import Any, Coroutine, Dict, Iterable, List, Optional, Sequence, Union, cast
from typing import Any, Coroutine, Dict, Iterable, List, Optional, Sequence, Union, cast, Tuple

import aiohttp
from aiohttp import ClientResponse
Expand All @@ -49,6 +49,9 @@ async def _json_or_text(
return text


BASE = "https://top.gg/api"


class HTTPClient:
"""Represents an HTTP client sending HTTP requests to the Top.gg API.
Expand All @@ -65,14 +68,23 @@ class HTTPClient:
Arbitrary kwargs to be passed to :class:`aiohttp.ClientSession`.
"""

__slots__: Tuple[str, ...] = (
"token",
"_own_session",
"session",
"global_rate_limiter",
"bot_rate_limiter",
"rate_limiters",
"user_agent",
)

def __init__(
self,
token: str,
*,
session: Optional[aiohttp.ClientSession] = None,
**kwargs: Any,
) -> None:
self.BASE = "https://top.gg/api"
self.token = token
self._own_session = session is None
self.session: aiohttp.ClientSession = session or aiohttp.ClientSession(**kwargs)
Expand All @@ -87,7 +99,7 @@ def __init__(
async def request(self, method: str, endpoint: str, **kwargs: Any) -> dict:
"""Handles requests to the API."""
rate_limiters = self.rate_limiters if endpoint.startswith("/bots") else self.global_rate_limiter
url = f"{self.BASE}{endpoint}"
url = BASE + endpoint

if not self.token:
raise errors.UnauthorizedDetected("Top.gg API token not provided")
Expand Down
6 changes: 5 additions & 1 deletion topgg/ratelimiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
import collections
from datetime import datetime
from types import TracebackType
from typing import Any, Awaitable, Callable, List, Optional, Type
from typing import Any, Awaitable, Callable, List, Optional, Type, Tuple


class AsyncRateLimiter:
"""
Provides rate limiting for an operation with a configurable number of requests for a time period.
"""

__slots__: Tuple[str, ...] = ("__lock", "callback", "max_calls", "period", "calls")

__lock: asyncio.Lock
callback: Optional[Callable[[float], Awaitable[Any]]]
max_calls: int
Expand Down Expand Up @@ -85,6 +87,8 @@ def _timespan(self) -> float:


class AsyncRateLimiterManager:
__slots__: Tuple[str, ...] = ("rate_limiters",)

rate_limiters: List[AsyncRateLimiter]

def __init__(self, rate_limiters: List[AsyncRateLimiter]):
Expand Down
18 changes: 18 additions & 0 deletions topgg/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ class WidgetOptions(DataDict[str, t.Any]):
"""Model that represents widget options that are passed to Top.gg widget URL generated via
:meth:`DBLClient.generate_widget`."""

__slots__: t.Tuple[str, ...] = ()

id: t.Optional[int]
"""ID of a bot to generate the widget for. Must resolve to an ID of a listed bot when converted to a string."""
colors: Colors
Expand Down Expand Up @@ -200,6 +202,8 @@ class BotData(DataDict[str, t.Any]):
"""Model that contains information about a listed bot on top.gg. The data this model contains can be found `here
<https://docs.top.gg/api/bot/#bot-structure>`__."""

__slots__: t.Tuple[str, ...] = ()

id: int
"""The ID of the bot."""

Expand Down Expand Up @@ -270,6 +274,8 @@ def __init__(self, **kwargs: t.Any):
class BotStatsData(DataDict[str, t.Any]):
"""Model that contains information about a listed bot's guild and shard count."""

__slots__: t.Tuple[str, ...] = ()

server_count: t.Optional[int]
"""The amount of servers the bot is in."""
shards: t.List[int]
Expand All @@ -284,6 +290,8 @@ def __init__(self, **kwargs: t.Any):
class BriefUserData(DataDict[str, t.Any]):
"""Model that contains brief information about a Top.gg user."""

__slots__: t.Tuple[str, ...] = ()

id: int
"""The Discord ID of the user."""
username: str
Expand All @@ -300,6 +308,8 @@ def __init__(self, **kwargs: t.Any):
class SocialData(DataDict[str, str]):
"""Model that contains social information about a top.gg user."""

__slots__: t.Tuple[str, ...] = ()

youtube: str
"""The YouTube channel ID of the user."""
reddit: str
Expand All @@ -316,6 +326,8 @@ class UserData(DataDict[str, t.Any]):
"""Model that contains information about a top.gg user. The data this model contains can be found `here
<https://docs.top.gg/api/user/#structure>`__."""

__slots__: t.Tuple[str, ...] = ()

id: int
"""The ID of the user."""

Expand Down Expand Up @@ -353,6 +365,8 @@ def __init__(self, **kwargs: t.Any):
class VoteDataDict(DataDict[str, t.Any]):
"""Base model that represents received information from Top.gg via webhooks."""

__slots__: t.Tuple[str, ...] = ()

type: str
"""Type of the action (``upvote`` or ``test``)."""
user: int
Expand All @@ -367,6 +381,8 @@ def __init__(self, **kwargs: t.Any):
class BotVoteData(VoteDataDict):
"""Model that contains information about a bot vote."""

__slots__: t.Tuple[str, ...] = ()

bot: int
"""ID of the bot the user voted for."""
is_weekend: bool
Expand All @@ -376,6 +392,8 @@ class BotVoteData(VoteDataDict):
class GuildVoteData(VoteDataDict):
"""Model that contains information about a guild vote."""

__slots__: t.Tuple[str, ...] = ()

guild: int
"""ID of the guild the user voted for."""

Expand Down
11 changes: 7 additions & 4 deletions topgg/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
class WebhookType(enum.Enum):
"""An enum that represents the type of an endpoint."""

__slots__: t.Tuple[str, ...] = ()

BOT = enum.auto()
"""Marks the endpoint as a bot webhook."""

Expand All @@ -62,10 +64,11 @@ class WebhookManager(DataContainerMixin):
A class for managing Top.gg webhooks.
"""

__slots__: t.Tuple[str, ...] = ("__app", "_webserver", "_is_running")

__app: web.Application
_webserver: web.TCPSite
_is_closed: bool
__slots__ = ("__app", "_webserver", "_is_running")
_is_running: bool

def __init__(self) -> None:
super().__init__()
Expand Down Expand Up @@ -173,7 +176,7 @@ class WebhookEndpoint:
A helper class to setup webhook endpoint.
"""

__slots__ = ("_callback", "_auth", "_route", "_type")
__slots__: t.Tuple[str, ...] = ("_callback", "_auth", "_route", "_type")

def __init__(self) -> None:
self._auth = ""
Expand Down Expand Up @@ -298,7 +301,7 @@ def on_vote(vote_data: topgg.BotVoteData): ...
endpoint.add_to_manager()
"""

__slots__ = ("manager",)
__slots__: t.Tuple[str, ...] = ("manager",)

def __init__(self, manager: WebhookManager):
super().__init__()
Expand Down

0 comments on commit 5fa5aaa

Please sign in to comment.