diff --git a/discord/object.py b/discord/object.py index fe5bfd86be..db8f2182fa 100644 --- a/discord/object.py +++ b/discord/object.py @@ -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 @@ -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' @@ -76,7 +80,15 @@ 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: @@ -84,9 +96,10 @@ def __init__(self, id: SupportsIntCast, type: type[Snowflake] = utils.MISSING): 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 + self.type: type[T] = type or self.__class__ + @override def __repr__(self) -> str: return f""