Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StructuredEnum with str or int Mixin Breaking Change in Python 3.11 #190

Closed
narasux opened this issue Oct 14, 2024 · 1 comment · Fixed by #192
Closed

StructuredEnum with str or int Mixin Breaking Change in Python 3.11 #190

narasux opened this issue Oct 14, 2024 · 1 comment · Fixed by #192

Comments

@narasux
Copy link
Collaborator

narasux commented Oct 14, 2024

目前业务代码中使用 blue_krill.data_types.enum.StructuredEnum 的枚举类,大量使用 int, str 作为 mixin,
以便在编写业务逻辑时,无需使用 .value 即可进行比较,字符串格式化(format / f-string),例子如下:

class StoreType(str, StructuredEnum):
    S3 = EnumField("s3")
    BKREPO = EnumField("bkrepo")

class PluginRole(int, StructuredEnum):
    ADMINISTRATOR = EnumField(2)
    DEVELOPER = EnumField(3)

但是在 Python 3.11 中,基础 Enum 的 str() 行为发生了变化,例如:

from enum import Enum


class Foo(str, Enum):
    BAR = "bar"

# Python 3.10
f"{Foo.BAR}"  # > bar

# Python 3.11
f"{Foo.BAR}"  # > Foo.BAR

由于 blue_krill 需要兼容存量的非 3.11+ 的项目,不能直接替换基础的 OrigEnum,需要评估如何处理

相关链接:

@piglei
Copy link
Collaborator

piglei commented Oct 16, 2024

遵循 3.11 中内置 enum 模块的使用标准,并参考其实现,提供 StrStructuredEnum / IntStructuredEnum 两个类,行为类似内置模块的 StrEnum 和 IntEnum 怎么样?

ref:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants