Skip to content

Commit

Permalink
More type safty and small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexFlipnote committed Dec 22, 2024
1 parent 5230202 commit 54f8fe5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion discord_http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ async def sync_commands(self) -> None:
int(gid) for gid in cmd.guild_ids
])

guild_ids = list(set(guild_ids))
guild_ids: list[int] = list(set(guild_ids))

for g in guild_ids:
await self.state.update_commands(
Expand Down
6 changes: 4 additions & 2 deletions discord_http/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def __init__(
self._converters: dict[str, Type[Converter]] = {}

self.__list_choices: list[str] = []
self.__user_objects: dict[str, Type[Union[Member, User]]] = {}
self.__user_objects: dict[str, Type[Member | User]] = {}

if self.type == ApplicationCommandType.chat_input:
if self.description is None:
Expand Down Expand Up @@ -337,7 +337,9 @@ def __init__(
else:
# Just a regular channel type
option.update({
"channel_types": [int(i) for i in channel_types[origin]]
"channel_types": [
int(i) for i in channel_types[origin]

Check failure on line 341 in discord_http/commands.py

View workflow job for this annotation

GitHub Actions / pyright

Argument of type "Any | type[Union]" cannot be assigned to parameter "key" of type "type[BaseChannel] | type[TextChannel] | type[VoiceChannel] | type[CategoryChannel] | type[NewsChannel] | type[StoreChannel] | type[NewsThread] | type[PublicThread] | type[PrivateThread] | type[StageChannel] | type[DirectoryChannel] | type[ForumChannel] | type[Thread]" in function "__getitem__"   Type "Any | type[Union]" is not assignable to type "type[BaseChannel] | type[TextChannel] | type[VoiceChannel] | type[CategoryChannel] | type[NewsChannel] | type[StoreChannel] | type[NewsThread] | type[PublicThread] | type[PrivateThread] | type[StageChannel] | type[DirectoryChannel] | type[ForumChannel] | type[Thread]"     Type "type[Union]" is not assignable to type "type[BaseChannel] | type[TextChannel] | type[VoiceChannel] | type[CategoryChannel] | type[NewsChannel] | type[StoreChannel] | type[NewsThread] | type[PublicThread] | type[PrivateThread] | type[StageChannel] | type[DirectoryChannel] | type[ForumChannel] | type[Thread]"       Type "type[Union]" is not assignable to type "type[BaseChannel]"       Type "type[Union]" is not assignable to type "type[TextChannel]"       Type "type[Union]" is not assignable to type "type[VoiceChannel]"       Type "type[Union]" is not assignable to type "type[CategoryChannel]"       Type "type[Union]" is not assignable to type "type[NewsChannel]"       Type "type[Union]" is not assignable to type "type[StoreChannel]" (reportArgumentType)

Check failure on line 341 in discord_http/commands.py

View workflow job for this annotation

GitHub Actions / pyright

Argument of type "Any | type[Union]" cannot be assigned to parameter "key" of type "type[BaseChannel] | type[TextChannel] | type[VoiceChannel] | type[CategoryChannel] | type[NewsChannel] | type[StoreChannel] | type[NewsThread] | type[PublicThread] | type[PrivateThread] | type[StageChannel] | type[DirectoryChannel] | type[ForumChannel] | type[Thread]" in function "__getitem__"   Type "Any | type[Union]" is not assignable to type "type[BaseChannel] | type[TextChannel] | type[VoiceChannel] | type[CategoryChannel] | type[NewsChannel] | type[StoreChannel] | type[NewsThread] | type[PublicThread] | type[PrivateThread] | type[StageChannel] | type[DirectoryChannel] | type[ForumChannel] | type[Thread]"     Type "type[Union]" is not assignable to type "type[BaseChannel] | type[TextChannel] | type[VoiceChannel] | type[CategoryChannel] | type[NewsChannel] | type[StoreChannel] | type[NewsThread] | type[PublicThread] | type[PrivateThread] | type[StageChannel] | type[DirectoryChannel] | type[ForumChannel] | type[Thread]"       Type "type[Union]" is not assignable to type "type[BaseChannel]"       Type "type[Union]" is not assignable to type "type[TextChannel]"       Type "type[Union]" is not assignable to type "type[VoiceChannel]"       Type "type[Union]" is not assignable to type "type[CategoryChannel]"       Type "type[Union]" is not assignable to type "type[NewsChannel]"       Type "type[Union]" is not assignable to type "type[StoreChannel]" (reportArgumentType)
]
})

case x if x in [Attachment]:
Expand Down
8 changes: 4 additions & 4 deletions discord_http/gateway/shard.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if TYPE_CHECKING:
from ..member import Member
from ..client import Client
from ..guild import Guild
from ..guild import Guild, PartialGuild

DEFAULT_GATEWAY = yarl.URL("wss://gateway.discord.gg/")
_log = logging.getLogger("discord_http")
Expand Down Expand Up @@ -570,7 +570,7 @@ async def _socket_manager(self) -> None:
self._reset_instance()
_log.error(f"Shard {self.shard_id} crashed completly", exc_info=e)

def _guild_needs_chunking(self, guild: "Guild") -> bool:
def _guild_needs_chunking(self, guild: "Guild | PartialGuild") -> bool:
return (
self.bot.chunk_guilds_on_startup and
not guild.chunked and
Expand All @@ -580,7 +580,7 @@ def _guild_needs_chunking(self, guild: "Guild") -> bool:
)
)

def _chunk_timeout(self, guild: "Guild") -> float:
def _chunk_timeout(self, guild: "Guild | PartialGuild") -> float:
return max(5.0, (guild.member_count or 0) / 10_000)

async def _delay_ready(self) -> None:
Expand All @@ -589,7 +589,7 @@ async def _delay_ready(self) -> None:
Then make shard ready when last GUILD_CREATE is received
"""
try:
states: list[tuple[Guild, asyncio.Future[list[Member]]]] = []
states: list[tuple[Guild | PartialGuild, asyncio.Future[list[Member]]]] = []
while True:
try:
guild_data = await asyncio.wait_for(
Expand Down
18 changes: 9 additions & 9 deletions discord_http/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ def __init__(self, *, state: "DiscordAPI", id: int):
def __repr__(self) -> str:
return f"<PartialGuild id={self.id}>"

@property
def large(self) -> bool:
""" `bool`: Whether the guild is considered large """
if self._large is None:
if self.member_count is not None:
return self.member_count >= 250
return len(self.members) >= 250
return self.large

@property
def chunked(self) -> bool:
""" `bool`: Whether the guild is chunked or not """
Expand Down Expand Up @@ -2556,15 +2565,6 @@ def _update(self, data: dict) -> None:
self.widget_channel_id: Optional[int] = utils.get_int(data, "widget_channel_id")
self.widget_enabled: bool = data.get("widget_enabled", False)

@property
def large(self) -> bool:
""" `bool`: Whether the guild is considered large """
if self._large is None:
if self.member_count is not None:
return self.member_count >= 250
return len(self.members) >= 250
return self.large

@property
def emojis_limit(self) -> int:
""" `int`: The maximum amount of emojis the guild can have """
Expand Down

0 comments on commit 54f8fe5

Please sign in to comment.