From 830fe38fb959be21d4b8987a6bdef18db27ff6a7 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Sun, 24 Sep 2023 00:23:39 +0200 Subject: [PATCH] Fix IntEnum for python 3.11 (#205) --- src/superqt/combobox/_enum_combobox.py | 6 ++---- tests/test_enum_comb_box.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/superqt/combobox/_enum_combobox.py b/src/superqt/combobox/_enum_combobox.py index fb127936..0ba38e5f 100644 --- a/src/superqt/combobox/_enum_combobox.py +++ b/src/superqt/combobox/_enum_combobox.py @@ -12,10 +12,8 @@ def _get_name(enum_value: Enum): """Create human readable name if user does not implement `__str__`.""" - if ( - enum_value.__str__.__module__ != "enum" - and not enum_value.__str__.__module__.startswith("shibokensupport") - ): + str_module = getattr(enum_value.__str__, "__module__", "enum") + if str_module != "enum" and not str_module.startswith("shibokensupport"): # check if function was overloaded name = str(enum_value) else: diff --git a/tests/test_enum_comb_box.py b/tests/test_enum_comb_box.py index 82c23f98..6c743c85 100644 --- a/tests/test_enum_comb_box.py +++ b/tests/test_enum_comb_box.py @@ -1,4 +1,4 @@ -from enum import Enum +from enum import Enum, IntEnum import pytest @@ -36,6 +36,12 @@ class Enum4(Enum): c_3 = 3 +class IntEnum1(IntEnum): + a = 1 + b = 2 + c = 5 + + def test_simple_create(qtbot): enum = QEnumComboBox(enum_class=Enum1) qtbot.addWidget(enum) @@ -129,3 +135,10 @@ def test_optional(qtbot): enum.setCurrentEnum(None) assert enum.currentText() == NONE_STRING assert enum.currentEnum() is None + + +def test_simple_create_int_enum(qtbot): + enum = QEnumComboBox(enum_class=IntEnum1) + qtbot.addWidget(enum) + assert enum.count() == 3 + assert [enum.itemText(i) for i in range(enum.count())] == ["a", "b", "c"]