-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenums.py
51 lines (39 loc) · 1.42 KB
/
enums.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from type import NTypeVars, apply_generics_to
class EnumType(NTypeVars):
def __init__(self, name, variants, typevars=None, original=None):
super(EnumType, self).__init__(name, typevars, original=original)
self.variants = variants
def get_types(self, variant_name):
for variant, types in self.variants:
if variant == variant_name:
return apply_generics_to(
types, dict(zip(self.base_type.typevars, self.typevars))
)
return None
def new_child(self, typevars):
return type(self)(self.name, self.variants, typevars, original=self.base_type)
class EnumValue:
def __init__(self, variant, values=None):
self.variant = variant
self.values = values or []
def __repr__(self):
return (
"<"
+ self.variant
+ "".join(" " + repr(value) for value in self.values)
+ ">"
)
def __eq__(self, other):
return self.variant == other.variant and self.values == other.values
def __hash__(self):
out = hash(self.variant)
for val in self.values:
out += hash(val)
return out
@classmethod
def construct(cls, variant):
return lambda *values: cls(variant, values)
class EnumPattern:
def __init__(self, variant, patterns=None):
self.variant = variant
self.patterns = patterns or []