Skip to content

Commit b014d52

Browse files
authored
feat: add StrStructuredEnum & IntStructuredEnum class (TencentBlueKing#192)
1 parent 0ff708a commit b014d52

5 files changed

Lines changed: 75 additions & 3 deletions

File tree

sdks/blue-krill/CHANGE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Change logs
22

3+
### 2.0.7
4+
5+
- Feature: (py 3.11+) add StrStructuredEnum, IntStructuredEnum class
6+
37
### 2.0.6
48

59
- Fix: update urllib3 version

sdks/blue-krill/blue_krill/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
99
* specific language governing permissions and limitations under the License.
1010
"""
11-
__version__ = "2.0.6"
11+
__version__ = "2.0.7"

sdks/blue-krill/blue_krill/data_types/enum.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,16 @@ def get_field_members(cls) -> Dict:
192192

193193

194194
class StructuredEnum(OrigEnum, metaclass=StructuredEnumMeta):
195-
"""Structured Enum type, providing extra features such as getting enum members as choices tuple"""
195+
"""
196+
Structured Enum type, providing extra features such as getting enum members as choices tuple
197+
198+
NOTE: XEnum(str / int, StructuredEnum) not working in string formatting since python 3.11,
199+
please use StrStructuredEnum / IntStructuredEnum instead of StructuredEnum
200+
refs:
201+
- https://github.com/python/cpython/issues/100458
202+
- https://github.com/TencentBlueKing/bkpaas-python-sdk/issues/190
203+
- https://blog.pecar.me/python-enum
204+
"""
196205

197206
@classmethod
198207
def get_django_choices(cls) -> List[Tuple[Any, str]]:
@@ -227,3 +236,26 @@ def get_choices(cls) -> List[Tuple[Any, str]]:
227236
"""Get Choices for all field members."""
228237
members = cls.get_field_members()
229238
return [(field.real_value, field.label) for field in members.values()]
239+
240+
241+
try:
242+
# python 3.11+ required
243+
from enum import StrEnum, IntEnum
244+
except ImportError:
245+
pass
246+
else:
247+
class StrStructuredEnum(StructuredEnum, StrEnum):
248+
"""
249+
StrStructuredEnum ensures the literals in f-string / str.format() is real_value
250+
251+
Important: Use XEnum(StrStructuredEnum) instead of XEnum(str, StructuredEnum) since python 3.11
252+
"""
253+
pass
254+
255+
class IntStructuredEnum(StructuredEnum, IntEnum):
256+
"""
257+
IntStructuredEnum ensures the literals in f-string / str.format() is real_value
258+
259+
Important: Use XEnum(IntStructuredEnum) instead of XEnum(int, StructuredEnum) since python 3.11
260+
"""
261+
pass

sdks/blue-krill/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "blue-krill"
3-
version = "2.0.6"
3+
version = "2.0.7"
44
description = "Tools and common packages for blueking PaaS platform."
55
include = ["blue_krill/py.typed"]
66
readme = "README.md"

sdks/blue-krill/tests/data_types/test_enum.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,39 @@ def test_get_labels(self):
220220

221221
def test_get_values(self):
222222
assert UserType.get_values() == [1, 2, 3, 4]
223+
224+
225+
try:
226+
from blue_krill.data_types.enum import StrStructuredEnum, IntStructuredEnum
227+
except ImportError:
228+
pass
229+
else:
230+
class StrDemoEnum(StrStructuredEnum):
231+
FOO = "foo"
232+
BAR = "bar"
233+
234+
class TestStrStructuredEnum:
235+
def test_value_compare(self):
236+
assert StrDemoEnum.FOO == "foo"
237+
assert StrDemoEnum.BAR.value == "bar"
238+
239+
def test_string_formatting(self):
240+
assert str(StrDemoEnum.BAR) == "bar"
241+
assert "%s" % StrDemoEnum.FOO == "foo"
242+
assert "{}".format(StrDemoEnum.BAR) == "bar"
243+
assert f"{StrDemoEnum.FOO}-{StrDemoEnum.BAR}" == "foo-bar"
244+
245+
class IntDemoEnum(IntStructuredEnum):
246+
FOO = 1
247+
BAR = 2
248+
249+
class TestIntStructuredEnum:
250+
def test_value_compare(self):
251+
assert IntDemoEnum.FOO == 1
252+
assert IntDemoEnum.BAR.value == 2
253+
254+
def test_string_formatting(self):
255+
assert str(IntDemoEnum.BAR) == "2"
256+
assert "%s" % IntDemoEnum.FOO == "1"
257+
assert "{}".format(IntDemoEnum.BAR) == "2"
258+
assert f"{IntDemoEnum.FOO}-{IntDemoEnum.BAR}" == "1-2"

0 commit comments

Comments
 (0)