From 698a46791cf4a143caf2bb00a8f844f10e0c9dc5 Mon Sep 17 00:00:00 2001 From: HansBug Date: Wed, 9 Mar 2022 15:34:11 +0800 Subject: [PATCH] fix(hansbug): fix problem in AutoIntEnum --- hbutils/model/enum.py | 2 +- test/model/test_enum.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/hbutils/model/enum.py b/hbutils/model/enum.py index e705f263ca9..fc68ea7866b 100644 --- a/hbutils/model/enum.py +++ b/hbutils/model/enum.py @@ -42,7 +42,7 @@ class AutoIntEnum(IntEnum): def __new__(cls, *args, **kwargs): value = len(cls.__members__) + 1 - obj = int.__new__(cls) + obj = int.__new__(cls, value) obj._value_ = value return obj diff --git a/test/model/test_enum.py b/test/model/test_enum.py index c57ad8348f7..13638901f40 100644 --- a/test/model/test_enum.py +++ b/test/model/test_enum.py @@ -1,4 +1,5 @@ from enum import IntEnum, Enum +from random import shuffle import pytest @@ -158,6 +159,37 @@ def __init__(self, v): assert MyEnum.C.value == 3 assert MyEnum.C.v == 'c_vvv' + assert MyEnum.A == MyEnum.A + assert MyEnum.A != MyEnum.B + assert MyEnum.A != MyEnum.C + assert MyEnum.B != MyEnum.A + assert MyEnum.B == MyEnum.B + assert MyEnum.B != MyEnum.C + assert MyEnum.C != MyEnum.A + assert MyEnum.C != MyEnum.B + assert MyEnum.C == MyEnum.C + + for i in range(100): + l = [MyEnum.A, MyEnum.B, MyEnum.C] + shuffle(l) + assert sorted(l) == [MyEnum.A, MyEnum.B, MyEnum.C] + + d = {} + d[MyEnum.A] = 1 + d[MyEnum.B] = 2 + d[MyEnum.C] = 3 + assert len(d.items()) == 3 + assert d[MyEnum.A] == 1 + assert d[MyEnum.B] == 2 + assert d[MyEnum.C] == 3 + + d[MyEnum.C] = 4 + assert d == { + MyEnum.A: 1, + MyEnum.B: 2, + MyEnum.C: 4, + } + def test_auto_int_enum_with_int_enum_loads(self): @int_enum_loads(name_preprocess=str.upper) class MyEnum(AutoIntEnum):