Skip to content

Commit d87c56e

Browse files
author
Jordan Adler
committed
Update behavior of newstr.__eq__() to match str.__eq__() as per reference docs
1 parent 923622a commit d87c56e

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/future/types/newstr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def __eq__(self, other):
291291
isinstance(other, bytes) and not isnewbytes(other)):
292292
return super(newstr, self).__eq__(other)
293293
else:
294-
return False
294+
return NotImplemented
295295

296296
def __hash__(self):
297297
if (isinstance(self, unicode) or

tests/test_future/test_str.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,24 @@ def test_eq(self):
363363
self.assertFalse(b'ABCD' == s)
364364
self.assertFalse(bytes(b'ABCD') == s)
365365

366+
# We want to ensure comparison against unknown types return
367+
# NotImplemented so that the interpreter can rerun the test with the
368+
# other class. We expect the operator to return False if both return
369+
# NotImplemented.
366370
class OurCustomString(object):
367371
def __init__(self, string):
368372
self.string = string
369373

370-
def __str__(self):
371-
return self.string
374+
def __eq__(self, other):
375+
return NotImplemented
372376

373377
our_str = OurCustomString("foobar")
374378
new_str = str("foobar")
375379

376380
self.assertFalse(our_str == new_str)
377381
self.assertFalse(new_str == our_str)
382+
self.assertIs(new_str.__eq__(our_str), NotImplemented)
383+
self.assertIs(our_str.__eq__(new_str), NotImplemented)
378384

379385
def test_hash(self):
380386
s = str('ABCD')

0 commit comments

Comments
 (0)