Repository navigation
enum with inherited type won't pickle #88508
Description
Activity
The following script runs without error in 3.8.5 and raises an error in 3.8.6, 3.9.5 and 3.10.0b1.
Source:
import enum, pickle class MyInt(int): pass # work-around: __reduce_ex__ = int.__reduce_ex__ class MyEnum(MyInt, enum.Enum): A = 1 pickle.dumps(MyEnum.A)Error (same in 3.8.6, 3.9.5 and 3.10.0b1):
Traceback (most recent call last): File "/home/thecap/projects/covid-data-model/./enum-pickle.py", line 12, in <module> pickle.dumps(MyEnum.A) File "/home/thecap/.pyenv/versions/3.10.0b1/lib/python3.10/enum.py", line 83, in _break_on_call_reduce raise TypeError('%r cannot be pickled' % self) TypeError: MyEnum.A cannot be pickledLike https://bugs.python.org/issue41889 this seems to be related to the fix for https://bugs.python.org/issue39587 which changes member_type from int to MyInt. A work-around is in the comment above.
- added3.10 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Jun 8, 2021 Looking into this I think the root of the problem is the way
reduceis handled -- currently, Enum's__reduce_ex__works by returning the class, and the value to use to lookup the member. Because that lookup can fail with complex enums, EnumType will sabotagereduceif it can't find support in the new enum class.However, if
__reduce_ex__working by returning`getattr, (self.__class, self._name_)`then we should be fine, as that should never fail.
Changing
__reduce_ex__is too big a change for 3.9, so making the search for pickle support more robust there.Also fixing the search for the data type so that in
A -> B -> C
the data type is C and not B.
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jun 19, 2021 - addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jun 19, 2021
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: