mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 12:18:59 +00:00
Add support for specifying the type of a generic discord.Object
Co-authored-by: Danny <1695103+Rapptz@users.noreply.github.com>
This commit is contained in:
parent
842d6b4fbb
commit
1787867320
@ -34,12 +34,9 @@ class EqualityComparable:
|
|||||||
id: int
|
id: int
|
||||||
|
|
||||||
def __eq__(self, other: object) -> bool:
|
def __eq__(self, other: object) -> bool:
|
||||||
return isinstance(other, self.__class__) and other.id == self.id
|
|
||||||
|
|
||||||
def __ne__(self, other: object) -> bool:
|
|
||||||
if isinstance(other, self.__class__):
|
if isinstance(other, self.__class__):
|
||||||
return other.id != self.id
|
return other.id == self.id
|
||||||
return True
|
return NotImplemented
|
||||||
|
|
||||||
|
|
||||||
class Hashable(EqualityComparable):
|
class Hashable(EqualityComparable):
|
||||||
|
@ -25,16 +25,18 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from .mixins import Hashable
|
from .mixins import Hashable
|
||||||
from .utils import snowflake_time
|
from .utils import snowflake_time, MISSING
|
||||||
|
|
||||||
from typing import (
|
from typing import (
|
||||||
SupportsInt,
|
SupportsInt,
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
|
Type,
|
||||||
Union,
|
Union,
|
||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
import datetime
|
import datetime
|
||||||
|
from . import abc
|
||||||
|
|
||||||
SupportsIntCast = Union[SupportsInt, str, bytes, bytearray]
|
SupportsIntCast = Union[SupportsInt, str, bytes, bytearray]
|
||||||
|
|
||||||
@ -77,18 +79,34 @@ class Object(Hashable):
|
|||||||
-----------
|
-----------
|
||||||
id: :class:`int`
|
id: :class:`int`
|
||||||
The ID of the object.
|
The ID of the object.
|
||||||
|
type: Type[:class:`abc.Snowflake`]
|
||||||
|
The discord.py model type of the object, if not specified, defaults to this class.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
In instances where there are multiple applicable types, use a shared base class.
|
||||||
|
for example, both :class:`Member` and :class:`User` are subclasses of :class:`abc.User`.
|
||||||
|
|
||||||
|
.. versionadded:: 2.0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, id: SupportsIntCast):
|
def __init__(self, id: SupportsIntCast, *, type: Type[abc.Snowflake] = MISSING):
|
||||||
try:
|
try:
|
||||||
id = int(id)
|
id = int(id)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise TypeError(f'id parameter must be convertible to int not {id.__class__!r}') from None
|
raise TypeError(f'id parameter must be convertible to int not {id.__class__!r}') from None
|
||||||
else:
|
self.id: int = id
|
||||||
self.id = id
|
self.type: Type[abc.Snowflake] = type or self.__class__
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
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
|
@property
|
||||||
def created_at(self) -> datetime.datetime:
|
def created_at(self) -> datetime.datetime:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user