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

fix(hansbug): fix problem in AutoIntEnum #29

Merged
merged 1 commit into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(hansbug): fix problem in AutoIntEnum
  • Loading branch information
HansBug committed Mar 9, 2022
commit 698a46791cf4a143caf2bb00a8f844f10e0c9dc5
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