-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewtype_enums.py
More file actions
137 lines (98 loc) · 3.95 KB
/
newtype_enums.py
File metadata and controls
137 lines (98 loc) · 3.95 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from enum import Enum
import pytest
from newtype import NewType, newtype_exclude
class ENV(NewType(str), Enum):
LOCAL = "LOCAL"
DEV = "DEV"
SIT = "SIT"
UAT = "UAT"
PREPROD = "PREPROD"
PROD = "PROD"
class RegularENV(str, Enum):
LOCAL = "LOCAL"
DEV = "DEV"
SIT = "SIT"
UAT = "UAT"
PREPROD = "PREPROD"
PROD = "PROD"
class ENVVariant(str):
__VALID_MEMBERS__ = ["LOCAL", "DEV", "SIT", "UAT", "PREPROD", "PROD"]
def __new__(cls, value: str):
members = ENVVariant.__VALID_MEMBERS__
# if isinstance(value, RollYourOwnNewTypeEnum):
# value_as_str = str(value.value)
# else:
value_as_str = str(value)
if value_as_str not in members:
raise ValueError(f"`value` = {value} must be one of `{members}`; `value_as_str` = {value_as_str}")
return super().__new__(cls, value_as_str)
# why not i write my own `.replace(..)`
# yes, you can but how?
def my_replace(self, old: "ENVVariant", new: "ENVVariant", count: int=-1):
return ENVVariant(str(self).replace(str(old), str(new), count))
class RollYourOwnNewTypeEnum(ENVVariant, Enum):
LOCAL = "LOCAL"
DEV = "DEV"
SIT = "SIT"
UAT = "UAT"
PREPROD = "PREPROD"
PROD = "PROD"
def test_nt_env_replace():
env = ENV.LOCAL
assert env is ENV.LOCAL
assert env is not ENV.DEV
assert isinstance(env, ENV)
# let's say now we want to replace the environment
# nevermind about the reason why we want to do so
env = env.replace(ENV.LOCAL, ENV.DEV)
# replacement is successful
assert env is ENV.DEV
assert env is not ENV.LOCAL
# still an `ENV`
assert isinstance(env, ENV)
assert isinstance(env, str)
with pytest.raises(ValueError):
# cannot replace with something that is not a `ENV`
env = env.replace(ENV.DEV, "NotAnEnv")
with pytest.raises(ValueError):
# cannot even make 'DEV' -> 'dev'
env = env.lower()
def test_reg_env_replace():
env = RegularENV.LOCAL
# expected outcomes
assert env is RegularENV.LOCAL # pass
assert env is not RegularENV.DEV # pass
assert isinstance(env, RegularENV) # pass
# now we try to replace
env = env.replace(RegularENV.LOCAL, RegularENV.DEV)
# we are hoping that it will continue to be a `RegularENV.DEV` but it is not
assert env is not RegularENV.DEV # pass, no longer a `RegularENV`
assert env is not RegularENV.LOCAL # pass, no longer a `RegularENV`
assert not isinstance(env, RegularENV)
assert isinstance(env, str) # 'downcast' (?) to `str`
def test_ryont_env_replace():
env = RollYourOwnNewTypeEnum.LOCAL
# expected outcomes
assert env is RollYourOwnNewTypeEnum.LOCAL # pass
assert env is not RollYourOwnNewTypeEnum.DEV # pass
assert isinstance(env, RollYourOwnNewTypeEnum) # pass
# now we try to replace
env = env.replace(RollYourOwnNewTypeEnum.LOCAL, RollYourOwnNewTypeEnum.DEV)
# we are hoping that it will continue to be a `RollYourOwnNewTypeEnum.DEV` but it is not
assert env is not RollYourOwnNewTypeEnum.DEV # pass, no longer a `RollYourOwnNewTypeEnum`
assert env is not RollYourOwnNewTypeEnum.LOCAL # pass, no longer a `RollYourOwnNewTypeEnum`
assert not isinstance(env, RollYourOwnNewTypeEnum)
assert isinstance(env, str) # 'downcast' (?) to `str`
with pytest.raises(AssertionError):
assert env is RollYourOwnNewTypeEnum.DEV
with pytest.raises(AssertionError):
assert env is RollYourOwnNewTypeEnum.DEV
with pytest.raises(AssertionError):
assert isinstance(env, RollYourOwnNewTypeEnum)
env = env.replace("DEV", "NotAnEnv")
assert env == "NotAnEnv" # this 'shouldn't' pass but it does
env = RollYourOwnNewTypeEnum.LOCAL
# env = env.my_replace(RollYourOwnNewTypeEnum.LOCAL, RollYourOwnNewTypeEnum.PREPROD)
assert isinstance(env, str)
assert env is not RollYourOwnNewTypeEnum.PREPROD
assert isinstance(env, RollYourOwnNewTypeEnum)