Skip to content

Commit a415ff6

Browse files
committed
make Enum generic
1 parent ffd7320 commit a415ff6

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

discord/enums.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727

2828
import types
2929
from enum import Enum as EnumBase
30-
from typing import Any, Self, TypeVar, Union
30+
from typing import TYPE_CHECKING, Any, Generic, Self, Union
31+
32+
from typing_extensions import TypeVar
3133

3234
E = TypeVar("E", bound="Enum")
35+
V = TypeVar("V", default=Any)
3336

3437
__all__ = (
3538
"Enum",
@@ -85,11 +88,17 @@
8588
)
8689

8790

88-
class Enum(EnumBase):
91+
class Enum(EnumBase, Generic[V]):
8992
"""An :class:`enum.Enum` subclass that implements a missing value creation behavior if it is
9093
not present in any of the members of it.
9194
"""
9295

96+
if TYPE_CHECKING:
97+
_value_: V
98+
99+
@property
100+
def value(self) -> V: ...
101+
93102
def __init_subclass__(cls, *, comparable: bool = False) -> None:
94103
super().__init_subclass__()
95104

@@ -100,7 +109,7 @@ def __init_subclass__(cls, *, comparable: bool = False) -> None:
100109
cls.__ge__ = lambda self, other: isinstance(other, self.__class__) and self.value >= other.value
101110

102111
@classmethod
103-
def _missing_(cls, value: Any) -> Self:
112+
def _missing_(cls, value: V) -> Self:
104113
name = f"unknown_{value}"
105114
if name in cls.__members__:
106115
return cls.__members__[name]

0 commit comments

Comments
 (0)