File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 11prospector == 1.7.*
2+ pylint == 2.15.6
23pytest == 7.1.*
Original file line number Diff line number Diff line change 1+ from enum import Enum
2+
3+
4+ class StringEnum (Enum ):
5+ @classmethod
6+ def _missing_ (cls , value ):
7+ if isinstance (value , str ):
8+ upper_value = value .upper ()
9+
10+ key = StringEnum ._key_from_str_ (upper_value )
11+ if key is not None :
12+ return key
13+
14+ lower_value = value .lower ()
15+
16+ key = StringEnum ._key_from_str_ (lower_value )
17+ if key is not None :
18+ return key
19+
20+ raise ValueError (f"{ value } is not a valid { cls .__name__ } " )
21+
22+ @classmethod
23+ def _key_from_str_ (cls , value : str ):
24+ if value in cls .__members__ :
25+ return cls (value )
26+
27+ return None
28+
29+ def __eq__ (self , o : object ) -> bool :
30+ return self .value == o .value
You can’t perform that action at this time.
0 commit comments