File tree 2 files changed +9
-3
lines changed
2 files changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -291,7 +291,7 @@ def __eq__(self, other):
291
291
isinstance (other , bytes ) and not isnewbytes (other )):
292
292
return super (newstr , self ).__eq__ (other )
293
293
else :
294
- return False
294
+ return NotImplemented
295
295
296
296
def __hash__ (self ):
297
297
if (isinstance (self , unicode ) or
Original file line number Diff line number Diff line change @@ -363,18 +363,24 @@ def test_eq(self):
363
363
self .assertFalse (b'ABCD' == s )
364
364
self .assertFalse (bytes (b'ABCD' ) == s )
365
365
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.
366
370
class OurCustomString (object ):
367
371
def __init__ (self , string ):
368
372
self .string = string
369
373
370
- def __str__ (self ):
371
- return self . string
374
+ def __eq__ (self , other ):
375
+ return NotImplemented
372
376
373
377
our_str = OurCustomString ("foobar" )
374
378
new_str = str ("foobar" )
375
379
376
380
self .assertFalse (our_str == new_str )
377
381
self .assertFalse (new_str == our_str )
382
+ self .assertIs (new_str .__eq__ (our_str ), NotImplemented )
383
+ self .assertIs (our_str .__eq__ (new_str ), NotImplemented )
378
384
379
385
def test_hash (self ):
380
386
s = str ('ABCD' )
You can’t perform that action at this time.
0 commit comments