1

Using this snippet:

import sys
import yaml
try:
    from enum import ReprEnum
except ImportError:
    from enum import Enum as ReprEnum


class MyEnum(int, ReprEnum):
    foo = 123


print('Python version:', ''.join(map(str, sys.version_info[:3])))
print(yaml.dump({'test': MyEnum.foo}))

I get very different output on Python 3.10 and 3.11:

3.10 output:

Python version: 3.10.12
test: !!python/object/apply:__main__.MyEnum
- 123

3.11 output:

Python version: 3.11.4
test: !!python/object/apply:builtins.getattr
- !!python/name:__main__.MyEnum ''
- foo

My guess would be that it's related to the many changes in Python 3.11's enum module, but I'd like to understand why this changed...

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636

1 Answers1

1

Changing from by-value to by-name serialization happened in this Python PR.

However, it has later been reverted to by-value serialization in this PR which I assume will end up in 3.11.5.

A workaround until 3.11.5 (assuming it will be in there) is out, or when you want to support earlier 3.11 version as well is adding this to your Enum subclass:

def __reduce_ex__(self, proto):
    return self.__class__, (self._value_,)
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636