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

[FEATURE] Should Enum be considered immutable by default? #98

Closed
kenodegard opened this issue Apr 16, 2024 · 2 comments
Closed

[FEATURE] Should Enum be considered immutable by default? #98

kenodegard opened this issue Apr 16, 2024 · 2 comments

Comments

@kenodegard
Copy link
Contributor

Is your feature request related to a problem? Please describe.

I was surprised to learn that deepfreeze(Enum.VALUE) produced a frozendict(Enum.VALUE.__dict__). I expected it to return the enum. E.g.:

>>> from enum import Enum
>>> from frozendict import deepfreeze

>>> Color = Enum('Color', ['RED', 'GREEN', 'BLUE'])

>>> deepfreeze(Color.RED)
frozendict.frozendict({'_value_': 1, '_name_': 'RED', '__objclass__': <enum 'Color'>})

Describe the solution you'd like

Consider making Enum.VALUE an immutable scalar by default.

Describe alternatives you've considered

I know I can register a custom conversion:

>>> from enum import Enum
>>> from frozendict import deepfreeze

>>> Color = Enum('Color', ['RED', 'GREEN', 'BLUE'])

>>> deepfreeze(Color.RED, custom_converters={Enum: lambda x: x})
Color.RED

or

>>> from enum import Enum
>>> from frozendict import deepfreeze, register

>>> Color = Enum('Color', ['RED', 'GREEN', 'BLUE'])
>>> register(Enum, lambda x: x)

>>> deepfreeze(Color.RED)
Color.RED

Additional context

While I understand this can be easily configured on my end via the register function I'm wondering if it makes more sense to treat enums as immutable scalars by default.

@Marco-Sulla
Copy link
Owner

Good catch, ty.

@Marco-Sulla
Copy link
Owner

Fix released in 2.4.3

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

2 participants