Skip to content

Commit

Permalink
Merge pull request #29 from HansBug/fix/enum
Browse files Browse the repository at this point in the history
fix(hansbug): fix problem in AutoIntEnum
  • Loading branch information
HansBug authored Mar 9, 2022
2 parents 528ce55 + 698a467 commit 24270cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hbutils/model/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions test/model/test_enum.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import IntEnum, Enum
from random import shuffle

import pytest

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 24270cb

Please sign in to comment.