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

Adding type to objects: issue #908 #909

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions disnake/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
Type,
Union,
cast,
overload,
overload, TypeVar,
)

import disnake.abc

from . import utils
from . import utils, Object
from .asset import Asset
from .context_managers import Typing
from .enums import (
Expand Down Expand Up @@ -93,6 +93,7 @@
from .voice_region import VoiceRegion
from .webhook import Webhook

OverwriteKeyT = TypeVar('OverwriteKeyT', Role, Object, Union[Role, Member, Object])

async def _single_delete_strategy(messages: Iterable[Message]) -> None:
for m in messages:
Expand Down Expand Up @@ -353,7 +354,7 @@ async def edit(
default_thread_slowmode_delay: Optional[int] = ...,
default_auto_archive_duration: Optional[AnyThreadArchiveDuration] = ...,
type: ChannelType = ...,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = ...,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = ...,
flags: ChannelFlags = ...,
reason: Optional[str] = ...,
) -> TextChannel:
Expand All @@ -372,7 +373,7 @@ async def edit(
default_thread_slowmode_delay: Optional[int] = MISSING,
default_auto_archive_duration: Optional[AnyThreadArchiveDuration] = MISSING,
type: ChannelType = MISSING,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = MISSING,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = MISSING,
flags: ChannelFlags = MISSING,
reason: Optional[str] = None,
**kwargs: Never,
Expand Down Expand Up @@ -1313,7 +1314,7 @@ async def edit(
position: int = ...,
sync_permissions: bool = ...,
category: Optional[Snowflake] = ...,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = ...,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = ...,
rtc_region: Optional[Union[str, VoiceRegion]] = ...,
video_quality_mode: VideoQualityMode = ...,
nsfw: bool = ...,
Expand All @@ -1332,7 +1333,7 @@ async def edit(
position: int = MISSING,
sync_permissions: bool = MISSING,
category: Optional[Snowflake] = MISSING,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = MISSING,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = MISSING,
rtc_region: Optional[Union[str, VoiceRegion]] = MISSING,
video_quality_mode: VideoQualityMode = MISSING,
nsfw: bool = MISSING,
Expand Down Expand Up @@ -1957,7 +1958,7 @@ async def edit(
position: int = ...,
sync_permissions: bool = ...,
category: Optional[Snowflake] = ...,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = ...,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = ...,
rtc_region: Optional[Union[str, VoiceRegion]] = ...,
video_quality_mode: VideoQualityMode = ...,
bitrate: int = ...,
Expand All @@ -1973,7 +1974,7 @@ async def edit(
position: int = MISSING,
sync_permissions: bool = MISSING,
category: Optional[Snowflake] = MISSING,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = MISSING,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = MISSING,
rtc_region: Optional[Union[str, VoiceRegion]] = MISSING,
video_quality_mode: VideoQualityMode = MISSING,
bitrate: int = MISSING,
Expand Down Expand Up @@ -2184,7 +2185,7 @@ async def edit(
name: str = ...,
position: int = ...,
nsfw: bool = ...,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = ...,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = ...,
flags: ChannelFlags = ...,
reason: Optional[str] = ...,
) -> CategoryChannel:
Expand All @@ -2196,7 +2197,7 @@ async def edit(
name: str = MISSING,
position: int = MISSING,
nsfw: bool = MISSING,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = MISSING,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = MISSING,
flags: ChannelFlags = MISSING,
reason: Optional[str] = None,
**kwargs: Never,
Expand Down Expand Up @@ -2775,7 +2776,7 @@ async def edit(
slowmode_delay: Optional[int] = ...,
default_thread_slowmode_delay: Optional[int] = ...,
default_auto_archive_duration: Optional[AnyThreadArchiveDuration] = ...,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = ...,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = ...,
flags: ChannelFlags = ...,
require_tag: bool = ...,
available_tags: Sequence[ForumTag] = ...,
Expand All @@ -2798,7 +2799,7 @@ async def edit(
slowmode_delay: Optional[int] = MISSING,
default_thread_slowmode_delay: Optional[int] = MISSING,
default_auto_archive_duration: Optional[AnyThreadArchiveDuration] = MISSING,
overwrites: Mapping[Union[Role, Member], PermissionOverwrite] = MISSING,
overwrites: Mapping[OverwriteKeyT, PermissionOverwrite] = MISSING,
flags: ChannelFlags = MISSING,
require_tag: bool = MISSING,
available_tags: Sequence[ForumTag] = MISSING,
Expand Down
16 changes: 13 additions & 3 deletions disnake/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

from __future__ import annotations

from typing import TYPE_CHECKING, SupportsInt, Union
from typing import TYPE_CHECKING, SupportsInt, Union, Any, Type

from . import utils
from .mixins import Hashable

if TYPE_CHECKING:
import datetime

from . import abc

SupportsIntCast = Union[SupportsInt, str, bytes, bytearray]

__all__ = ("Object",)
Expand Down Expand Up @@ -49,7 +51,7 @@ class Object(Hashable):
The ID of the object.
"""

def __init__(self, id: SupportsIntCast) -> None:
def __init__(self, id: SupportsIntCast, *, type: Type[abc.Snowflake] = None) -> None:
try:
id = int(id)
except ValueError:
Expand All @@ -58,9 +60,17 @@ def __init__(self, id: SupportsIntCast) -> None:
) from None
else:
self.id = id
self.type: Type[abc.Snowflake] = type if type else self.__class__

def __repr__(self) -> str:
return f"<Object id={self.id!r}>"
return f"<Object id={self.id!r} type={self.type!r}>"

def __eq__(self, other: object) -> bool:
if isinstance(other, self.type):
return self.id == other.id
return NotImplemented

__hash__ = Hashable.__hash__

@property
def created_at(self) -> datetime.datetime:
Expand Down