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) *  |
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) *  |
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) *  |
Date: 2013-11-09 05:15 |
Given that __eq__ isn't adding anything, I think removing it is a fine option. |
|
|
msg202509 - (view) |
Author: Alyssa Coghlan (ncoghlan) *  |
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) *  |
Date: 2013-11-11 02:17 |
Done and done. |
|
|
msg202790 - (view) |
Author: Ethan Furman (ethan.furman) *  |
Date: 2013-11-13 22:27 |
changeset ca909a3728d3 |
|
|