This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Enumeration.__eq__
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: CliffM, barry, eli.bendersky, ethan.furman, ncoghlan
Priority: normal Keywords: patch

Created on 2013-10-13 19:38 by CliffM, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue19249.stoneleaf.01.patch ethan.furman, 2013-11-09 05:15 review
issue19249.stoneleaf.02.patch ethan.furman, 2013-11-11 02:17 review
Messages (8)
msg199774 - (view) Author: CliffM (CliffM) Date: 2013-10-13 19:38
Given that enumeration members are Singletons, can we not rely on the default
__eq__() method ?  (FWIW the existing tests all pass if you delete it)

This might be confusing for a reader later, so needs documenting.  Although I think it should be documented (in enum.py) either way.

Maybe also add a singletoness test ?

def test_singleton(self):
        class A(Enum):
            X=1
            Y=2

        self.assertTrue(id(A.X)==id(A.X))


Of course there is good chance that there is a test-case I have not thought of that will defeat this.  I am thinking about :

a) weak-ref handling of Enums 

b) unpickling, say by a user of the ZODB

Which might break the singletonicity. ??  Any offers ??
msg199829 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-10-14 00:51
Here's the current __eq__ method:

    def __eq__(self, other):
        if type(other) is self.__class__:
            return self is other
        return NotImplemented

It is, in fact, using identity and not value equality.

I'm thinking we should either remove and go with the default __eq__, or change it to value equality.  One possible reason to use value equality is that pickle protocols below 2 will actually create duplicate Enum members, breaking identity tests; if someone is stuck using pickle that way, they could still use Enum with `==` and not `is`.

Identity tests are already incorporated in many of the existing tests.

Gentlemen, what do you think?
msg200391 - (view) Author: CliffM (CliffM) Date: 2013-10-19 09:48
It is appropriate to modify the pickle-module to trap (a potential) the singletonicity-breaking event and raise a warning ? (I'm guessing an exception would be far too rude)

I like the idea of using identity-equality, but without the above trap one might get really weird bugs without a deep reading of the docs and/or code.
msg200509 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-10-19 22:33
Given the rarity of singletons, I don't think changing pickle in that way is appropriate.  Besides, pickle protocol 2 and above don't have the problem.
msg202460 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-11-09 05:15
Given that __eq__ isn't adding anything, I think removing it is a fine option.
msg202509 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-11-10 08:47
Since the default eq implementation handles ducktyping correctly, dropping the Enum specific __eq__ implementation should be fine.

Just make sure this still works:

>>> class AlwaysEqual:
...     def __eq__(self, other):
...         return True
... 
>>> from enum import Enum
>>> class MyEnum(Enum):
...     a = 1
... 
>>> MyEnum.a == AlwaysEqual()
True
>>> AlwaysEqual() == MyEnum.a
True
msg202599 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-11-11 02:17
Done and done.
msg202790 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-11-13 22:27
changeset ca909a3728d3
History
Date User Action Args
2022-04-11 14:57:51adminsetgithub: 63448
2013-11-13 22:27:41ethan.furmansetstatus: open -> closed
resolution: fixed
messages: + msg202790

stage: patch review -> resolved
2013-11-11 02:17:44ethan.furmansetfiles: + issue19249.stoneleaf.02.patch

messages: + msg202599
stage: test needed -> patch review
2013-11-10 08:47:31ncoghlansetnosy: + ncoghlan

messages: + msg202509
stage: test needed
2013-11-09 05:15:30ethan.furmansetfiles: + issue19249.stoneleaf.01.patch
keywords: + patch
messages: + msg202460
2013-10-19 22:33:02ethan.furmansetmessages: + msg200509
2013-10-19 09:48:25CliffMsetmessages: + msg200391
2013-10-14 00:51:11ethan.furmansetnosy: + barry, eli.bendersky, ethan.furman
messages: + msg199829

assignee: ethan.furman
type: behavior
2013-10-13 19:38:20CliffMcreate