Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclasses of enum.Enum can't be dumped #511

Closed
VOsakosa opened this issue Mar 25, 2021 · 3 comments
Closed

Subclasses of enum.Enum can't be dumped #511

VOsakosa opened this issue Mar 25, 2021 · 3 comments
Assignees

Comments

@VOsakosa
Copy link

Steps to reproduce:

import yaml
import enum
class Spam(enum.Enum):
    Value = 1
yaml.dump(Spam)

gives
TypeError: __reduce_ex__() missing 1 required positional argument: 'proto'
in represent_object(self, data).

My analysis:
The dumping code is calling Spam's __reduce_ex__ method on the class itself and therefore supplies one argument less than needed. Since enum.Enum has a meta class, the normal dumping for type instances is not run.

Adding a representer for enum.EnumMeta is a simple workaround, but I suppose the problem could also be solved by making
the representer for type a multi representer.

@nitzmahone
Copy link
Member

nitzmahone commented Mar 26, 2021

One way or another, this shouldn't be too hard to fix for pyyaml 6.0...

That said- I am curious what you're doing that you're serializing actual types- care to share? Serializing instances of custom enums makes total sense (and works fine), but the tests around serializing language infra are ancient (ie, long-predating enum). I'm sure there are a number of other cases like these (off the top of my head, dataclasses, namedtuples, and anything involving abc probably don't work right either), but they're also not done by the vast majority of users.

@VOsakosa
Copy link
Author

I was dumping a quick attempt at an ECS, where types were used as keys in a dict. Essentially, a value is saved in a dict with its type as key.
I was a bit hesitant to open this issue because I knew it is an obscure case.:)

@Thom1729 Thom1729 moved this from To Do to Assigned in PyYAML 6.0 Release Planning Sep 22, 2021
@Thom1729 Thom1729 moved this from Assigned to In Progress in PyYAML 6.0 Release Planning Sep 22, 2021
@Thom1729 Thom1729 self-assigned this Sep 22, 2021
@Thom1729 Thom1729 moved this from In Progress to Review in PyYAML 6.0 Release Planning Sep 22, 2021
@ingydotnet ingydotnet moved this from Review to To Do in PyYAML 6.0 Release Planning Sep 22, 2021
@nitzmahone
Copy link
Member

fixed by #556

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants