Skip to content
Open
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
23 changes: 18 additions & 5 deletions discord/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

from __future__ import annotations

from typing import TYPE_CHECKING, SupportsInt
from typing import TYPE_CHECKING, Any, Generic, SupportsInt, overload

from typing_extensions import TypeVar, override

from . import utils
from .mixins import Hashable
Expand All @@ -39,8 +41,10 @@

__all__ = ("Object",)

T = TypeVar("T", bound="Snowflake", default="Snowflake", covariant=True)


class Object(Hashable):
class Object(Hashable, Generic[T]):
"""Represents a generic Discord object.

The purpose of this class is to allow you to create 'miniature'
Expand Down Expand Up @@ -76,17 +80,26 @@ class Object(Hashable):
The model this object's ID is based off.
"""

def __init__(self, id: SupportsIntCast, type: type[Snowflake] = utils.MISSING):
@overload
def __init__(
self: Object[Any], id: SupportsIntCast
) -> None: ... # pyright: ignore[reportExplicitAny]

@overload
def __init__(self, id: SupportsIntCast, type: type[T] = utils.MISSING) -> None: ...

def __init__(self, id: SupportsIntCast, type: type[T] = utils.MISSING) -> None:
try:
id = int(id)
except ValueError:
raise TypeError(
f"id parameter must be convertible to int not {id.__class__!r}"
) from None
else:
self.id = id
self.type: type[Snowflake] = type or self.__class__
self.id: int = id
Comment thread
Paillat-dev marked this conversation as resolved.
Resolved
self.type: type[T] = type or self.__class__

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

Expand Down
Loading