Patchwork upgrade: correct implementation of improvement.__ne__

login
register
mail settings
Submitter Benjamin Peterson
Date Dec. 14, 2018, 7:24 a.m.
Message ID <dace81bda054a90fa223.1544772284@germain>
Download mbox | patch
Permalink /patch/37236/
State Accepted
Headers show

Comments

Benjamin Peterson - Dec. 14, 2018, 7:24 a.m.
# HG changeset patch
# User Benjamin Peterson <benjamin@python.org>
# Date 1544772028 28800
#      Thu Dec 13 23:20:28 2018 -0800
# Branch ne-impl
# Node ID dace81bda054a90fa22312d4fc2a57280518a942
# Parent  4591c9791a829e927388ba92a64971ae9ab301ec
upgrade: correct implementation of improvement.__ne__

The "not" operator binds more closely than "==":
>>> not False == False
False
Yuya Nishihara - Dec. 19, 2018, 11:21 a.m.
On Thu, 13 Dec 2018 23:24:44 -0800, Benjamin Peterson wrote:
> # HG changeset patch
> # User Benjamin Peterson <benjamin@python.org>
> # Date 1544772028 28800
> #      Thu Dec 13 23:20:28 2018 -0800
> # Branch ne-impl
> # Node ID dace81bda054a90fa22312d4fc2a57280518a942
> # Parent  4591c9791a829e927388ba92a64971ae9ab301ec
> upgrade: correct implementation of improvement.__ne__
> 
> The "not" operator binds more closely than "==":
> >>> not False == False
> False

While I like the explicitness of this patch, it's wrong. "not x" is weaker
than "x == y".

>>> ast.dump(ast.parse('not 1 == 2', mode='eval'))
'Expression(body=UnaryOp(op=Not(), operand=Compare(left=Num(n=1), ops=[Eq()], comparators=[Num(n=2)])))'

(This one is already queued.)
Benjamin Peterson - Dec. 20, 2018, 5:27 a.m.
On Wed, Dec 19, 2018, at 03:26, Yuya Nishihara wrote:
> On Thu, 13 Dec 2018 23:24:44 -0800, Benjamin Peterson wrote:
> > # HG changeset patch
> > # User Benjamin Peterson <benjamin@python.org>
> > # Date 1544772028 28800
> > #      Thu Dec 13 23:20:28 2018 -0800
> > # Branch ne-impl
> > # Node ID dace81bda054a90fa22312d4fc2a57280518a942
> > # Parent  4591c9791a829e927388ba92a64971ae9ab301ec
> > upgrade: correct implementation of improvement.__ne__
> > 
> > The "not" operator binds more closely than "==":
> > >>> not False == False
> > False
> 
> While I like the explicitness of this patch, it's wrong. "not x" is weaker
> than "x == y".
> 
> >>> ast.dump(ast.parse('not 1 == 2', mode='eval'))
> 'Expression(body=UnaryOp(op=Not(), operand=Compare(left=Num(n=1), 
> ops=[Eq()], comparators=[Num(n=2)])))'
> 
> (This one is already queued.)

Yuya,
Thank you for salvaging something from my patch here. My thinking was clearly deeply muddled while I was writing this.

Patch

diff --git a/mercurial/upgrade.py b/mercurial/upgrade.py
--- a/mercurial/upgrade.py
+++ b/mercurial/upgrade.py
@@ -142,7 +142,7 @@  class improvement(object):
         return self.name == other.name
 
     def __ne__(self, other):
-        return not self == other
+        return not (self == other)
 
     def __hash__(self):
         return hash(self.name)