yaml / pyyaml

Canonical source repository for PyYAML
MIT License
2.47k stars 507 forks source link

Enum member serialization different in Python 3.11 #720

Closed ThiefMaster closed 1 year ago

ThiefMaster commented 1 year ago

Not sure if this is expected, but if seems weird that it serializes differently depending on the Python version.

Script to reproduce (using PyYAML==6.0)

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}))

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

FWIW it also happens with a normal Enum instead of ReprEnum.

ThiefMaster commented 1 year ago

OK this looks like a bug in Python that got fixed in python/cpython#105348 (the __reduce_ex__ change in that PR), but isn't part of a 3.11 point release yet..