-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewtype_enums.py
More file actions
149 lines (108 loc) · 4.7 KB
/
newtype_enums.py
File metadata and controls
149 lines (108 loc) · 4.7 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
138
139
140
141
142
143
144
145
146
147
148
149
from enum import Enum
from typing import Optional, Type
import pytest
from newtype import NewType
class ENV(NewType(str), Enum): # type: ignore[misc]
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"
RollYourOwnNewTypeEnum: "Optional[Type[RollYourOwnNewTypeEnum]]" = None
class ENVVariant(str):
__VALID_MEMBERS__ = ["LOCAL", "DEV", "SIT", "UAT", "PREPROD", "PROD"]
def __new__(cls, value: str) -> "ENVVariant":
members = ENVVariant.__VALID_MEMBERS__
value_as_str = str(value.value if hasattr(value, "value") else 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)
def my_replace(self, old: "ENVVariant", new: "ENVVariant", count: int=-1) -> "ENVVariant":
# Convert both old and new to their string values
old_str = str(old.value if hasattr(old, "value") else old)
new_str = str(new.value if hasattr(new, "value") else new)
# Do the replacement on string values
result = str(self.value if hasattr(self, "value") else self).replace(old_str, new_str, count)
# For enums, we need to look up the enum member by value
if issubclass(type(self), Enum):
return type(self)(result) # This will find the enum member
# For non-enum types, create new instance directly
return type(self)(result)
class RollYourOwnNewTypeEnum(ENVVariant, Enum): # type: ignore[no-redef]
LOCAL = "LOCAL"
DEV = "DEV"
SIT = "SIT"
UAT = "UAT"
PREPROD = "PREPROD"
PROD = "PROD"
# mypy doesn't raise errors here
def test_nt_env_replace() -> None:
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)
# reveal_type(env) # Revealed type is "newtype_enums.ENV"
# 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")
# reveal_type(env) # Revealed type is "newtype_enums.ENV"
with pytest.raises(ValueError):
# cannot even make 'DEV' -> 'dev'
env = env.lower()
def test_reg_env_replace() -> None:
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("LOCAL", "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() -> None:
assert RollYourOwnNewTypeEnum is not None
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 RollYourOwnNewTypeEnum.PREPROD
assert isinstance(env, RollYourOwnNewTypeEnum)