55
66
77# ## TEnum ############################################################################################################
8+ class EnumValue :
9+ """
10+ This is a helper class for adding documentation to an enum value.
11+ """
12+
13+ def __init__ (self , value : int , doc : t .Optional [str ] = None ) -> None :
14+ self .value = value
15+ self .__doc__ = doc if doc else ""
16+
17+ def __int__ (self ) -> int :
18+ return self .value
19+
20+
821class EnumBase (enum .IntEnum ):
922 """
1023 Base class for an Enum used in `construct_typed.TEnum`.
1124
12- This class extends the standard `enum.IntEnum`, so that missing values are automatically generated.
25+ This class extends the standard `enum.IntEnum` by.
26+ - missing values are automatically generated
27+ - possibility to add documentation for each enum value (see `EnumValue`)
28+
29+ Example::
30+
31+ >>> class State(EnumBase):
32+ ... Idle = 1
33+ ... Running = EnumValue(2, "This is the running state.")
34+
35+ >>> State(1)
36+ <State.Idle: 1>
37+
38+ >>> State["Idle"]
39+ <State.Idle: 1>
40+
41+ >>> State.Idle
42+ <State.Idle: 1>
43+
44+ >>> State(3) # missing value
45+ <State.3: 3>
46+
47+ >>> State.Running.__doc__ # documentation
48+ 'This is the running state.'
1349 """
1450
51+ def __init__ (self , val : t .Union [EnumValue , int ]):
52+ if isinstance (val , EnumValue ):
53+ self .__doc__ = val .__doc__
54+ else :
55+ self .__doc__ = ""
56+
1557 # Extend the enum type with _missing_ method. So if a enum value
1658 # not found in the enum, a new pseudo member is created.
1759 # The idea is taken from: https://stackoverflow.com/a/57179436
@@ -25,6 +67,7 @@ def _missing_(cls, value: t.Any) -> t.Optional[enum.Enum]:
2567 # However, new_member._name_ = value works, too
2668 new_member ._name_ = str (value )
2769 new_member ._value_ = value
70+ new_member .__doc__ = "missing value"
2871 pseudo_member = cls ._value2member_map_ .setdefault (value , new_member )
2972 return pseudo_member
3073 return None # will raise the ValueError in Enum.__new__
@@ -75,7 +118,51 @@ def _encode(
75118
76119# ## TFlagsEnum #######################################################################################################
77120class FlagsEnumBase (enum .IntFlag ):
78- pass
121+ """
122+ Base class for an Enum used in `construct_typed.TFlagsEnum`.
123+
124+ This class extends the standard `enum.IntFlag` by.
125+ - possibility to add documentation for each enum value (see `EnumValue`)
126+
127+ Example::
128+
129+ >>> class Option(FlagsEnumBase):
130+ ... OptOne = 1
131+ ... OptTwo = EnumValue(2, "This is option two.")
132+
133+ >>> Option(1)
134+ <Option.OptOne: 1>
135+
136+ >>> Option["OptOne"]
137+ <Option.OptOne: 1>
138+
139+ >>> Option.OptOne
140+ <Option.OptOne: 1>
141+
142+ >>> Option(3)
143+ <Option.OptTwo|OptOne: 3>
144+
145+ >>> Option(4)
146+ <Option.4: 4>
147+
148+ >>> Option.OptTwo.__doc__ # documentation
149+ 'This is option two.'
150+ """
151+
152+ def __init__ (self , val : t .Union [EnumValue , int ]):
153+ if isinstance (val , EnumValue ):
154+ self .__doc__ = val .__doc__
155+ else :
156+ self .__doc__ = ""
157+
158+ @classmethod
159+ def _missing_ (cls , value : t .Any ) -> t .Any :
160+ """
161+ Returns member (possibly creating it) if one can be found for value.
162+ """
163+ new_member = super ()._missing_ (value )
164+ new_member .__doc__ = "missing value"
165+ return new_member
79166
80167
81168FlagsEnumType = t .TypeVar ("FlagsEnumType" , bound = FlagsEnumBase )
0 commit comments