From 017302075db54735d187b9fe87e8f11c92ea2d5e Mon Sep 17 00:00:00 2001 From: Paillat-dev Date: Tue, 17 Nov 2026 10:13:51 +0100 Subject: [PATCH 1/4] refactor: Make Object generic --- discord/object.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/discord/object.py b/discord/object.py index fe5bfd86be..ff4b14e825 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, Generic, SupportsInt, overload + +from typing_extensions import override, TypeVar 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,13 @@ 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[Snowflake], id: SupportsIntCast) -> None: ... + + @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 +94,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"" From c775510aa208f1a35a40dc48933ece405090e55d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:17:03 +0000 Subject: [PATCH 2/4] style(pre-commit): auto fixes from pre-commit.com hooks --- discord/object.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/object.py b/discord/object.py index ff4b14e825..afe7d5a22c 100644 --- a/discord/object.py +++ b/discord/object.py @@ -27,7 +27,7 @@ from typing import TYPE_CHECKING, Generic, SupportsInt, overload -from typing_extensions import override, TypeVar +from typing_extensions import TypeVar, override from . import utils from .mixins import Hashable From 95b2d1eabb65f18bf72161ffcbe2b03d8a02a10d Mon Sep 17 00:00:00 2001 From: Paillat-dev Date: Tue, 17 Nov 2026 12:31:25 +0100 Subject: [PATCH 3/4] chore: Allow bare Object --- discord/object.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/discord/object.py b/discord/object.py index afe7d5a22c..6f31bb4e22 100644 --- a/discord/object.py +++ b/discord/object.py @@ -25,7 +25,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Generic, SupportsInt, overload +from typing import TYPE_CHECKING, Any, Generic, SupportsInt, overload from typing_extensions import TypeVar, override @@ -81,7 +81,7 @@ class Object(Hashable, Generic[T]): """ @overload - def __init__(self: Object[Snowflake], id: SupportsIntCast) -> None: ... + def __init__(self: Object[Any], id: SupportsIntCast) -> None: ... # pyright: ignore[reportExplicitAny] @overload def __init__(self, id: SupportsIntCast, type: type[T] = utils.MISSING) -> None: ... From bc17ecafd60950a2eac1032dc2cebf747aa2cfe9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:32:04 +0000 Subject: [PATCH 4/4] style(pre-commit): auto fixes from pre-commit.com hooks --- discord/object.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/discord/object.py b/discord/object.py index 6f31bb4e22..db8f2182fa 100644 --- a/discord/object.py +++ b/discord/object.py @@ -81,7 +81,9 @@ class Object(Hashable, Generic[T]): """ @overload - def __init__(self: Object[Any], id: SupportsIntCast) -> None: ... # pyright: ignore[reportExplicitAny] + def __init__( + self: Object[Any], id: SupportsIntCast + ) -> None: ... # pyright: ignore[reportExplicitAny] @overload def __init__(self, id: SupportsIntCast, type: type[T] = utils.MISSING) -> None: ...