Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typehinting across the Novus rewrite #81

Open
wants to merge 8 commits into
base: rewrite
Choose a base branch
from
12 changes: 8 additions & 4 deletions novus/api/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import logging
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, overload
from typing import TYPE_CHECKING, Any, TypeVar, overload

from ..models.channel import Channel
from ..models.guild import Guild
Expand All @@ -38,13 +38,17 @@
log = logging.getLogger("novus.api.cache")


class MaxLenDict(OrderedDict):
K = TypeVar("K")
V = TypeVar("V")


class MaxLenDict(OrderedDict[K, V]):

def __init__(self, *, max_size: int):
self.max_size = max_size
super().__init__()

def __setitem__(self, __key: Any, __value: Any) -> None:
def __setitem__(self, __key: K, __value: V) -> None:
super().__setitem__(__key, __value)
while len(self) > self.max_size:
self.popitem(last=False)
Expand All @@ -65,7 +69,7 @@ def __init__(self, parent: HTTPConnection):
self.emojis: dict[int, Emoji] = {}
self.stickers: dict[int, Sticker] = {}
self.events: dict[int, ScheduledEvent] = {}
self.messages: dict[int, Message] = MaxLenDict(max_size=1_000)
self.messages: MaxLenDict[int, Message] = MaxLenDict(max_size=1_000)

def __repr__(self) -> str:
return f"<{self.__class__.__name__} " + (
Expand Down
5 changes: 2 additions & 3 deletions novus/api/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,10 @@ def request_params(

# Set headers
headers = {
"Authorization": self._prefixed_token,
"User-Agent": self._user_agent,
}
if self._token is None:
del headers["Authorization"]
if self._prefixed_token is not None:
headers["Authorization"] = self._prefixed_token
if reason:
headers['X-Audit-Log-Reason'] = reason

Expand Down
5 changes: 3 additions & 2 deletions novus/api/audit_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ async def get_guild_audit_log(
"""

params: dict[str, Any] = {}
params['limit'] = limit

if user_id is not None:
params['user_id'] = user_id
if action_type is not None:
Expand All @@ -58,8 +60,7 @@ async def get_guild_audit_log(
params['before'] = before
if after is not None:
params['after'] = after
if limit is not None:
params['limit'] = limit

route = Route(
"GET",
"/guilds/{guild_id}/audit-logs",
Expand Down
10 changes: 5 additions & 5 deletions novus/api/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ async def create_reaction(
emoji_str: str
if isinstance(emoji, str):
emoji_str = quote_plus(emoji)
elif isinstance(emoji, PartialEmoji):
elif isinstance(emoji, PartialEmoji): # pyright: ignore[reportUnnecessaryIsInstance]
emoji_str = f"{emoji.name}:{emoji.id}"
else:
raise ValueError
Expand All @@ -293,7 +293,7 @@ async def delete_own_reaction(
emoji_str: str
if isinstance(emoji, str):
emoji_str = quote_plus(emoji)
elif isinstance(emoji, PartialEmoji):
elif isinstance(emoji, PartialEmoji): # pyright: ignore[reportUnnecessaryIsInstance]
emoji_str = f"{emoji.name}:{emoji.id}"
else:
raise ValueError
Expand All @@ -319,7 +319,7 @@ async def delete_user_reaction(
emoji_str: str
if isinstance(emoji, str):
emoji_str = quote_plus(emoji)
elif isinstance(emoji, PartialEmoji):
elif isinstance(emoji, PartialEmoji): # pyright: ignore[reportUnnecessaryIsInstance]
emoji_str = f"{emoji.name}:{emoji.id}"
else:
raise ValueError
Expand All @@ -345,7 +345,7 @@ async def get_reactions(
emoji_str: str
if isinstance(emoji, str):
emoji_str = quote_plus(emoji)
elif isinstance(emoji, PartialEmoji):
elif isinstance(emoji, PartialEmoji): # pyright: ignore[reportUnnecessaryIsInstance]
emoji_str = f"{emoji.name}:{emoji.id}"
else:
raise ValueError
Expand Down Expand Up @@ -389,7 +389,7 @@ async def delete_all_reactions_for_emoji(
emoji_str: str
if isinstance(emoji, str):
emoji_str = quote_plus(emoji)
elif isinstance(emoji, PartialEmoji):
elif isinstance(emoji, PartialEmoji): # pyright: ignore[reportUnnecessaryIsInstance]
emoji_str = f"{emoji.name}:{emoji.id}"
else:
raise ValueError
Expand Down
9 changes: 6 additions & 3 deletions novus/api/gateway/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import json
import logging
from copy import copy
from typing import TYPE_CHECKING, Any, Awaitable, Callable
from typing import TYPE_CHECKING, Any, Awaitable, Callable, cast

from ...models import ( # Emoji,; Object,; Sticker,; ThreadMember,
AuditLogEntry,
Expand Down Expand Up @@ -168,10 +168,13 @@ async def handle_dispatch(self, event_name: str, data: dict) -> None:
elif event_name == "RESUMED":
return None

coro: Callable[..., Awaitable[None]] | None
coro: Callable[[Any], Awaitable[None]] | None
if self.parent.gateway.guild_ids_only:
if event_name == "GUILD_CREATE":
coro = self._handle_guild_create_id_only
# Cast to avoid coro from being typed too specifically
# This is a compromise to avoid 50 different @overload's for every single
# dispatch handler's individual data type
coro = cast(Callable[[Any], Awaitable[None]], self._handle_guild_create_id_only)
elif event_name == "GUILD_DELETE":
coro = self.EVENT_HANDLER["GUILD_DELETE"]
else:
Expand Down
2 changes: 1 addition & 1 deletion novus/models/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(
elif isinstance(data, (str, pathlib.Path)):
with open(data, 'rb') as a:
self.data = a.read()
elif isinstance(data, io.IOBase):
elif isinstance(data, io.IOBase): # pyright: ignore[reportUnnecessaryIsInstance]
pointer = data.tell()
self.data = data.read()
data.seek(pointer)
Expand Down
2 changes: 1 addition & 1 deletion novus/models/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@ class Guild(Hashable, BaseGuild):
approximate_member_count: int | None
welcome_screen: WelcomeScreen | None

def __init__(self, *, state: HTTPConnection, data: payloads.Guild):
def __init__(self, *, state: HTTPConnection, data: payloads.Guild | payloads.GatewayGuild):
self.state = state
self.id = try_snowflake(data['id'])
self._emojis: dict[int, Emoji] = {}
Expand Down
2 changes: 1 addition & 1 deletion novus/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ def only(cls, target: Role | User | GuildMember) -> Self:

if isinstance(target, (User, GuildMember,)):
return cls(users=[target])
elif isinstance(target, Role):
elif isinstance(target, Role): # pyright: ignore[reportUnnecessaryIsInstance]
return cls(roles=[target])
else:
raise TypeError("Only role and user types are permitted.")
Expand Down
2 changes: 1 addition & 1 deletion novus/utils/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def flatten_localization(d: LocType) -> Localization:
return Localization()
elif isinstance(d, Localization):
return d
elif isinstance(d, dict):
elif isinstance(d, dict): # pyright: ignore[reportUnnecessaryIsInstance]
return Localization(d)
else:
raise TypeError()
Expand Down
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ build-backend = "setuptools.build_meta"
[tool.setuptools]
packages.find.where = [ ".", "./novus/ext", ]

[tool.pyright]
reportPrivateUsage = false
reportImportCycles = false
reportUnknownMemberType = false
reportUnknownParameterType = false
reportUnknownArgumentType = false
reportUnknownVariableType = false
reportMissingParameterType = false
reportMissingTypeArgument = false
reportMissingTypeStubs = false
reportUnknownLambdaType = false
reportIncompatibleVariableOverride = false
reportConstantRedefinition = false

[project]
name = "novus"
version = "1.0.0"
Expand Down