[issue30545] Enum equality across modules: comparing objects instead of values

2017-06-01 Thread Madhav Datt

New submission from Madhav Datt:

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.

--
components: Library (Lib)
messages: 294983
nosy: Madhav Datt
priority: normal
severity: normal
status: open
title: Enum equality across modules: comparing objects instead of values
type: behavior
versions: Python 2.7

___
Python tracker 
<http://bugs.python.org/issue30545>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30545] Enum equality across modules: comparing objects instead of values

2017-06-02 Thread Madhav Datt

Madhav Datt added the comment:

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.

--
status: closed -> open

___
Python tracker 
<http://bugs.python.org/issue30545>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com