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
delegation of != to the right-hand side argument is not always done #65607
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:
assignee='https://github.com/serhiy-storchaka'closed_at=<Date2015-01-26.08:16:19.111>created_at=<Date2014-05-01.14:31:27.553>labels= ['interpreter-core', 'type-bug']
title='delegation of `!=` to the right-hand side argument is not always done'updated_at=<Date2015-01-31.10:24:05.075>user='https://bugs.python.org/exarkun'
That is, even when the type of the left-hand argument is not a base class of the type of the right-hand argument, delegation to the right-hand argument is sensible if the left-hand argument does not implement the comparison.
Note that the output also demonstrates that this is already the behavior for ==. Only != seems to suffer from this issue.
That's because the implicit default __ne__ on Bar returns the opposite of __eq__.
MojoVampire commented on May 1, 2014
MojoVampiremannequin
Mannequin
More actions
Why would an subclass of object that doesn't redefine either __eq__ or __ne__ have a different behavior for inequality than object itself? Bar never defined __eq__, so it shouldn't have an implicit __ne__ any more than object itself does...
Saying that Bar has an implicit __ne__ that object doesn't is answering how this happens, but it's not a why; is there a reason why this should be the case, or is this a bug (either in spec or in code) that should be fixed?
The reason object() != Foo() "works" is that Foo is a subtype of object(), so the specialized ne of Foo is called immediately without trying object.ne.
I don't think it's a bug. The subclass-goes-first behavior is very intentional. The implicit __ne__ returning the boolean inverse of __eq__ is what fooled me when I looked at it.
Or did you mean that following the subclass rule in the case where object is the other class is possibly suspect?
There is a bit of analysis of the object.__ne__() implementation in bpo-4395. If my understanding is correct, I think it is a bug that object.__ne__(self, other) evaluates “not self == other”. It should evaluate “not self.__eq__(other)” instead, so that NotImplemented can be caught, allowing the reflected other.__ne__(self) method to be tried.
There are few incorrect implementations of __ne__ in the stdlib. Updated patch removes them. May be we should remove all implementations of __ne__ which are redundant now.
Activity
exarkun commentedon May 1, 2014
The output I would expect from this is
That is, even when the type of the left-hand argument is not a base class of the type of the right-hand argument, delegation to the right-hand argument is sensible if the left-hand argument does not implement the comparison.
Note that the output also demonstrates that this is already the behavior for
==. Only!=seems to suffer from this issue.benjaminp commentedon May 1, 2014
That's because the implicit default __ne__ on Bar returns the opposite of __eq__.
MojoVampire commentedon May 1, 2014
Why would an subclass of object that doesn't redefine either __eq__ or __ne__ have a different behavior for inequality than object itself? Bar never defined __eq__, so it shouldn't have an implicit __ne__ any more than object itself does...
Saying that Bar has an implicit __ne__ that object doesn't is answering how this happens, but it's not a why; is there a reason why this should be the case, or is this a bug (either in spec or in code) that should be fixed?
benjaminp commentedon May 1, 2014
The reason
object() != Foo()"works" is that Foo is a subtype of object(), so the specialized ne of Foo is called immediately without trying object.ne.I don't know whether it's a bug.
bitdancer commentedon May 1, 2014
I don't think it's a bug. The subclass-goes-first behavior is very intentional. The implicit __ne__ returning the boolean inverse of __eq__ is what fooled me when I looked at it.
Or did you mean that following the subclass rule in the case where object is the other class is possibly suspect?
benjaminp commentedon May 1, 2014
The subclass behavior is a red herring.
I meant maybe object.ne should check if the other object has a ne method before falling back on
not object.__eq__().bitdancer commentedon May 1, 2014
Oh, I see. Yes, that would seem more consistent.
vadmium commentedon Jan 11, 2015
There is a bit of analysis of the object.__ne__() implementation in bpo-4395. If my understanding is correct, I think it is a bug that object.__ne__(self, other) evaluates “not self == other”. It should evaluate “not self.__eq__(other)” instead, so that NotImplemented can be caught, allowing the reflected other.__ne__(self) method to be tried.
vadmium commentedon Jan 11, 2015
This patch should fix the problem I think. Before the __ne__() implementation was calling the “==” operator; now it calls the __eq__() method instead.
Also includes extra test for bpo-4395 to avoid having conficting patches.
serhiy-storchaka commentedon Jan 24, 2015
Particular case of this bug:
The second result should be NotImplemented.
Martin's patch LGTM except few style nitpicks to tests.
serhiy-storchaka commentedon Jan 24, 2015
There are few incorrect implementations of __ne__ in the stdlib. Updated patch removes them. May be we should remove all implementations of __ne__ which are redundant now.
vadmium commentedon Jan 25, 2015
I looked over your __ne__ removals from the library, and they all seem sensible to me.
python-dev commentedon Jan 26, 2015
New changeset e516badfd3b2 by Serhiy Storchaka in branch '3.4':
Issue bpo-21408: The default __ne__() now returns NotImplemented if __eq__()
https://hg.python.org/cpython/rev/e516badfd3b2
New changeset 7e9880052401 by Serhiy Storchaka in branch 'default':
Issue bpo-21408: The default __ne__() now returns NotImplemented if __eq__()
https://hg.python.org/cpython/rev/7e9880052401
serhiy-storchaka commentedon Jan 26, 2015
Thank you for your contribution Martin.
python-dev commentedon Jan 31, 2015
New changeset 3603bae63c13 by Serhiy Storchaka in branch 'default':
Issue bpo-23326: Removed __ne__ implementations. Since fixing default __ne__
https://hg.python.org/cpython/rev/3603bae63c13