You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enum equality across modules: comparing objects instead of values #74730
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:
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.
@ffissore I had similar problem. The underlying root cause was... due to imports.
In your example, MyEnum is imported twice (you can add below snippet to __main__.py to verify it)
Activity
MadhavDatt commentedon Jun 2, 2017
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:
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
ethanfurman commentedon 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.
MadhavDatt commentedon Jun 2, 2017
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.
bitdancer commentedon Jun 2, 2017
Can you provide actual code that demonstrates the issue you are talking about?
ethanfurman commentedon Jun 2, 2017
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:
please share it. ;) (As a comment/message here is fine.)
ethanfurman commentedon Jun 8, 2017
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
gh-98298, gh-74730: [Enum] update docs (GH-103163)
pythongh-98298, pythongh-74730: [Enum] update docs (pythonGH-103163)
gh-98298, gh-74730: [Enum] update docs (GH-103163)
pythongh-98298, pythongh-74730: [Enum] update docs (pythonGH-103163)
pythongh-98298, pythongh-74730: [Enum] update docs (pythonGH-103163)
ffissore commentedon Nov 10, 2023
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://github.com/proxy/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
dybi commentedon Sep 2, 2024
@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)If you want to fix your issue, make sure that
MyEnumis imported uniformly across your project.Cheers!