Skip to content

Commit

Permalink
Adjust to enum changes in Python 3.11.0b3
Browse files Browse the repository at this point in the history
There are two changes:

Changes in the actual code:

 - _member_names changed from a list to a dict in python/cpython#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 python/cpython#93910
But the fix is backwards compatible.

Fixes #326
  • Loading branch information
hroncok committed Jun 28, 2022
1 parent 25172e0 commit 468c93e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions proto/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion tests/test_enum_total_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 468c93e

Please sign in to comment.