From 468c93e600f824878715fd9af67a8a2b3df2c505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Tue, 28 Jun 2022 22:39:51 +0200 Subject: [PATCH] Adjust to enum changes in Python 3.11.0b3 There are two changes: Changes in the actual code: - _member_names changed from a list to a dict in https://github.com/python/cpython/pull/28907 - we instance-check and remove by list-specific or dict-specific way Change in the tests only: - accessing other enum members via instance attributes is no longer possible - we access them via the class instead - we leave the original test in a try-except block Some of the Python enum changes might get reverted, see https://github.com/python/cpython/issues/93910 But the fix is backwards compatible. Fixes https://github.com/googleapis/proto-plus-python/issues/326 --- proto/enums.py | 7 +++++-- tests/test_enum_total_ordering.py | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/proto/enums.py b/proto/enums.py index 6f13d32e..6207ad7d 100644 --- a/proto/enums.py +++ b/proto/enums.py @@ -58,8 +58,11 @@ def __new__(mcls, name, bases, attrs): # In 3.7 onwards, we can define an _ignore_ attribute and do some # mucking around with that. if pb_options in attrs._member_names: - idx = attrs._member_names.index(pb_options) - attrs._member_names.pop(idx) + if isinstance(attrs._member_names, list): + idx = attrs._member_names.index(pb_options) + attrs._member_names.pop(idx) + else: # Python 3.11.0b3 + del attrs._member_names[pb_options] # Make the descriptor. enum_desc = descriptor_pb2.EnumDescriptorProto( diff --git a/tests/test_enum_total_ordering.py b/tests/test_enum_total_ordering.py index ad7a3691..584a1831 100644 --- a/tests/test_enum_total_ordering.py +++ b/tests/test_enum_total_ordering.py @@ -49,7 +49,11 @@ def test_total_ordering_w_other_enum_type(): for item in enums_test.OtherEnum: assert not to_compare == item - assert to_compare.SOME_VALUE != item + assert type(to_compare).SOME_VALUE != item + try: + assert to_compare.SOME_VALUE != item + except AttributeError: # Python 3.11.0b3 + pass with pytest.raises(TypeError): assert not to_compare < item with pytest.raises(TypeError):