Skip to content

Commit e2f0fa9

Browse files
authored
feat(enum): add StringEnum to core (#17)
1 parent 1876125 commit e2f0fa9

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
prospector==1.7.*
2+
pylint==2.15.6
23
pytest==7.1.*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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

0 commit comments

Comments
 (0)