From d87c56e7bd264a701395a9929c4f28af82d9ebf3 Mon Sep 17 00:00:00 2001 From: Jordan Adler Date: Wed, 8 May 2019 12:17:50 -0400 Subject: [PATCH] Update behavior of newstr.__eq__() to match str.__eq__() as per reference docs --- src/future/types/newstr.py | 2 +- tests/test_future/test_str.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/future/types/newstr.py b/src/future/types/newstr.py index 76374656..d41ea969 100644 --- a/src/future/types/newstr.py +++ b/src/future/types/newstr.py @@ -291,7 +291,7 @@ def __eq__(self, other): isinstance(other, bytes) and not isnewbytes(other)): return super(newstr, self).__eq__(other) else: - return False + return NotImplemented def __hash__(self): if (isinstance(self, unicode) or diff --git a/tests/test_future/test_str.py b/tests/test_future/test_str.py index 5563abf3..51085481 100644 --- a/tests/test_future/test_str.py +++ b/tests/test_future/test_str.py @@ -363,18 +363,24 @@ def test_eq(self): self.assertFalse(b'ABCD' == s) self.assertFalse(bytes(b'ABCD') == s) + # We want to ensure comparison against unknown types return + # NotImplemented so that the interpreter can rerun the test with the + # other class. We expect the operator to return False if both return + # NotImplemented. class OurCustomString(object): def __init__(self, string): self.string = string - def __str__(self): - return self.string + def __eq__(self, other): + return NotImplemented our_str = OurCustomString("foobar") new_str = str("foobar") self.assertFalse(our_str == new_str) self.assertFalse(new_str == our_str) + self.assertIs(new_str.__eq__(our_str), NotImplemented) + self.assertIs(our_str.__eq__(new_str), NotImplemented) def test_hash(self): s = str('ABCD')