2727
2828import types
2929from 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
3234E = TypeVar ("E" , bound = "Enum" )
35+ V = TypeVar ("V" , default = Any )
3336
3437__all__ = (
3538 "Enum" ,
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