I’ve noticed in Python 3.8.9 and 3.9.6 that a dict comprehension of an Enum where two keys share the same value produces results which seem to be incorrect, and inconsistent with the same comprehension on the Enum __members__
mapping. Example below in iPython 3. 3.9.6:
In [1]: from enum import Enum
In [2]: E = Enum('E', {'a': 1, 'b': 1, 'c': 3})
In [3]: E.a
Out[3]: <E.a: 1>
In [4]: E.b
Out[4]: <E.a: 1>
In [5]: E.c
Out[5]: <E.c: 3>
In [6]: E.a.value
Out[6]: 1
In [7]: E.b.value
Out[7]: 1
In [8]: E.c.value
Out[8]: 3
In [9]: {k.name: k.value for k in E}
Out[9]: {'a': 1, 'c': 3}
In [10]: {k: v.value for k, v in E.__members__.items()}
Out[10]: {'a': 1, 'b': 1, 'c': 3}
It seems that in the first dict comprehension keys which share the same values are deduplicated. Why is this? Is this a bug in enum.Enum
?