File tree Expand file tree Collapse file tree 2 files changed +9
-3
lines changed Expand file tree Collapse file tree 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):
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
Original file line number Diff line number Diff 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' )
You can’t perform that action at this time.
0 commit comments