Skip to content

Commit 5ea9120

Browse files
committed
🐛 Fix typings
1 parent 5f852aa commit 5ea9120

20 files changed

+80
-47
lines changed

discord/app/event_emitter.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,40 @@ class Event(ABC):
4343
@abstractmethod
4444
async def __load__(cls, data: Any, state: "ConnectionState") -> Self | None: ...
4545

46+
def _populate_from_slots(self, obj: Any) -> None:
47+
"""
48+
Populate this event instance with attributes from another object.
49+
50+
Handles both __slots__ and __dict__ based objects.
51+
52+
Parameters
53+
----------
54+
obj: Any
55+
The object to copy attributes from.
56+
"""
57+
# Collect all slots from the object's class hierarchy
58+
slots = set()
59+
for klass in type(obj).__mro__:
60+
if hasattr(klass, "__slots__"):
61+
slots.update(klass.__slots__)
62+
63+
# Copy slot attributes
64+
for slot in slots:
65+
if hasattr(obj, slot):
66+
try:
67+
setattr(self, slot, getattr(obj, slot))
68+
except AttributeError:
69+
# Some slots might be read-only or not settable
70+
pass
71+
72+
# Also copy __dict__ if it exists
73+
if hasattr(obj, "__dict__"):
74+
for key, value in obj.__dict__.items():
75+
try:
76+
setattr(self, key, value)
77+
except AttributeError:
78+
pass
79+
4680

4781
ListenerCallback: TypeAlias = Callable[[Event], Any]
4882

discord/channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ def _get_voice_client_key(self) -> tuple[int, str]:
15601560
def _get_voice_state_pair(self) -> tuple[int, int]:
15611561
return self.guild.id, self.id
15621562

1563-
def _update(self, data: VoiceChannelPayload | StageChannelPayload) -> None:
1563+
async def _update(self, data: VoiceChannelPayload | StageChannelPayload) -> None:
15641564
# This data will always exist
15651565
self.name: str = data["name"]
15661566
self.category_id: int | None = get_as_snowflake(data, "parent_id")

discord/events/audit_log.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"""
2424

2525
import logging
26-
from typing import Any, Self
26+
from typing import Any
2727

28-
from typing_extensions import override
28+
from typing_extensions import override, Self
2929

3030
from discord.app.event_emitter import Event
3131
from discord.app.state import ConnectionState

discord/events/automod.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25-
from typing import Any, Self
25+
from typing import Any
2626

27-
from typing_extensions import override
27+
from typing_extensions import override, Self
2828

2929
from discord.app.state import ConnectionState
3030
from discord.automod import AutoModRule

discord/events/channel.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
from copy import copy
2626
from datetime import datetime
27-
from typing import Any, Self, TypeVar, cast
27+
from typing import Any, TypeVar, cast
2828

29-
from typing_extensions import override
29+
from typing_extensions import override, Self
3030

3131
from discord.abc import GuildChannel, PrivateChannel
3232
from discord.app.event_emitter import Event
@@ -53,15 +53,14 @@ async def __load__(cls, data: dict[str, Any], state: ConnectionState) -> Self |
5353

5454
guild_id = get_as_snowflake(data, "guild_id")
5555
guild = await state._get_guild(guild_id)
56-
if guild is not None:
57-
# the factory can't be a DMChannel or GroupChannel here
58-
channel = factory(guild=guild, state=self, data=data) # type: ignore # noqa: F821 # self is unbound
59-
guild._add_channel(channel) # type: ignore
60-
self = cls()
61-
self.__dict__.update(channel.__dict__)
62-
return self
63-
else:
56+
if guild is None:
6457
return
58+
# the factory can't be a DMChannel or GroupChannel here
59+
channel = factory(guild=guild, state=state, data=data) # type: ignore
60+
guild._add_channel(channel) # type: ignore
61+
self = cls()
62+
self._populate_from_slots(channel)
63+
return self
6564

6665

6766
class PrivateChannelUpdate(Event, PrivateChannel):
@@ -76,7 +75,7 @@ def __init__(self) -> None: ...
7675
async def __load__(cls, data: tuple[PrivateChannel | None, PrivateChannel], state: ConnectionState) -> Self | None:
7776
self = cls()
7877
self.old = data[0]
79-
self.__dict__.update(data[1].__dict__)
78+
self._populate_from_slots(data[1])
8079
return self
8180

8281

@@ -92,7 +91,7 @@ def __init__(self) -> None: ...
9291
async def __load__(cls, data: tuple[GuildChannel | None, GuildChannel], state: ConnectionState) -> Self | None:
9392
self = cls()
9493
self.old = data[0]
95-
self.__dict__.update(data[1].__dict__)
94+
self._populate_from_slots(data[1])
9695
return self
9796

9897

@@ -139,7 +138,7 @@ async def __load__(cls, data: dict[str, Any], state: ConnectionState) -> Self |
139138
if channel is not None:
140139
guild._remove_channel(channel)
141140
self = cls()
142-
self.__dict__.update(channel.__dict__)
141+
self._populate_from_slots(channel)
143142
return self
144143

145144

discord/events/entitlement.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25-
from typing import Any, Self
25+
from typing import Any
2626

27-
from typing_extensions import override
27+
from typing_extensions import override, Self
2828

2929
from discord.types.monetization import Entitlement as EntitlementPayload
3030

discord/events/gateway.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25-
from typing import Any, Self, cast
25+
from typing import Any, cast
2626

27-
from typing_extensions import override
27+
from typing_extensions import override, Self
2828

2929
from discord.emoji import Emoji
3030
from discord.flags import ApplicationFlags

discord/events/integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"""
2424

2525
import logging
26-
from typing import Any, Self
26+
from typing import Any
2727

28-
from typing_extensions import override
28+
from typing_extensions import override, Self
2929

3030
from discord.app.event_emitter import Event
3131
from discord.app.state import ConnectionState

discord/events/interaction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25-
from typing import Any, Self
25+
from typing import Any
2626

27-
from typing_extensions import override
27+
from typing_extensions import override, Self
2828

2929
from discord.enums import InteractionType
3030
from discord.types.interactions import Interaction as InteractionPayload

discord/events/invite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25-
from typing import Any, Self
25+
from typing import Any
2626

27-
from typing_extensions import override
27+
from typing_extensions import override, Self
2828

2929
from discord.abc import GuildChannel
3030
from discord.app.event_emitter import Event

0 commit comments

Comments
 (0)