Repository navigation
Enum equality across modules: comparing objects instead of values #74730
Description
Activity
The problem is described with an example in this StackOverflow question (https://stackoverflow.com/questions/26589805/python-enums-across-modules). Like in C and other languages, I would expect Enum equality to work across modules and not compare enum states/values, instead of just checking for the same object.
A possible simple fix for this problem would be to override the __eq__() function by default in the enum.Enum class with the following:
def __eq__(self, other): if isinstance(other, self.__class__): return self.value == other.value return False
I would be happy to create a GitHub pull request to fix this, however, I do not have the experience or knowledge to know if
- the current behavior is by design;
- whether this is worth fixing; and
- whether fixing this will break anything else.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jun 2, 2017 Two points:
-
Python 2.7 was the version marked, but 2.7 does not come with Enum
(wasn't introduced until 3.4 -- the third-party backport does work
on 2.7) -
the problem in the SO question is not caused by Enum, but by
re-importing a module under a different name which results in two
different Enum classes that happen to look identical, but are not --
so the change you propose would not help; also, since Enum members
with the same value are mapped to the same member your change does not
provide any new behavior.
So, in summary, the bug here is in the user's code.
-
Thanks a lot for those points Ethan. I feel I haven't done a very good job of explaining the bug, but let me use an example. Let's say we have an Enum called MyEnum, which is in a Python module called ModuleA. ModuleB imports ModuleA, and ModuleC imports both, ModuleA and ModuleB. Now, in ModuleC, I have ModuleB.some_function() return a MyEnum state, which I pass as a parameter to ModuleA.other_function() where it is compared to MyEnum states. Here the comparison fails even though it should not have. Obviously, this problem would not arise without such imports, and so is pretty specific, but I hope this makes explains it a little better.
Can you provide actual code that demonstrates the issue you are talking about?
If your example code is the same as the code in the SO problem, then my previous points stand.
According to the plain-English description you provided the comparison would succeed, so if you have example code which:
- doesn't involve ModuleA being the __main__ script module, and
- has the comparison fail, then
please share it. ;) (As a comment/message here is fine.)
No test code has been provided, so lacking any evidence of this problem I am closing this issue.
Do not reopen without testable code to show the failure.
11 remaining items
- addeddocsDocumentation in the Doc dirDocumentation in the Doc dirand removedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory3.7 (EOL)end of lifeend of lifetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on May 4, 2022 Hi. I stumbled on this issue once more so coded an example that triggers the weird behaviour whilst NOT using any reloading mechanism
Pls find the code at this gist: https://gist.github.com/ffissore/e882955f8046d6c35821ba53d2134bdb#file-readme-md
Also, I was checking the docs and I see no mentions of the different way enums equality work
@ffissore I had similar problem. The underlying root cause was... due to imports.
In your example,MyEnumis imported twice (you can add below snippet to__main__.pyto verify it)import sys print([mod for mod in sys.modules.keys() if "myenum" in mod])
If you want to fix your issue, make sure that
MyEnumis imported uniformly across your project.Cheers!
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
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: