77import unittest
88import re
99import contextlib
10+ import functools
1011import operator
1112import pickle
1213import ipaddress
@@ -552,6 +553,20 @@ def test_ip_network(self):
552553 self .assertFactoryError (ipaddress .ip_network , "network" )
553554
554555
556+ @functools .total_ordering
557+ class LargestObject :
558+ def __eq__ (self , other ):
559+ return isinstance (other , LargestObject )
560+ def __lt__ (self , other ):
561+ return False
562+
563+ @functools .total_ordering
564+ class SmallestObject :
565+ def __eq__ (self , other ):
566+ return isinstance (other , SmallestObject )
567+ def __gt__ (self , other ):
568+ return False
569+
555570class ComparisonTests (unittest .TestCase ):
556571
557572 v4addr = ipaddress .IPv4Address (1 )
@@ -605,6 +620,28 @@ def test_mixed_type_ordering(self):
605620 self .assertRaises (TypeError , lambda : lhs <= rhs )
606621 self .assertRaises (TypeError , lambda : lhs >= rhs )
607622
623+ def test_foreign_type_ordering (self ):
624+ other = object ()
625+ smallest = SmallestObject ()
626+ largest = LargestObject ()
627+ for obj in self .objects :
628+ with self .assertRaises (TypeError ):
629+ obj < other
630+ with self .assertRaises (TypeError ):
631+ obj > other
632+ with self .assertRaises (TypeError ):
633+ obj <= other
634+ with self .assertRaises (TypeError ):
635+ obj >= other
636+ self .assertTrue (obj < largest )
637+ self .assertFalse (obj > largest )
638+ self .assertTrue (obj <= largest )
639+ self .assertFalse (obj >= largest )
640+ self .assertFalse (obj < smallest )
641+ self .assertTrue (obj > smallest )
642+ self .assertFalse (obj <= smallest )
643+ self .assertTrue (obj >= smallest )
644+
608645 def test_mixed_type_key (self ):
609646 # with get_mixed_type_key, you can sort addresses and network.
610647 v4_ordered = [self .v4addr , self .v4net , self .v4intf ]
@@ -625,7 +662,7 @@ def test_incompatible_versions(self):
625662 v4addr = ipaddress .ip_address ('1.1.1.1' )
626663 v4net = ipaddress .ip_network ('1.1.1.1' )
627664 v6addr = ipaddress .ip_address ('::1' )
628- v6net = ipaddress .ip_address ('::1' )
665+ v6net = ipaddress .ip_network ('::1' )
629666
630667 self .assertRaises (TypeError , v4addr .__lt__ , v6addr )
631668 self .assertRaises (TypeError , v4addr .__gt__ , v6addr )
@@ -1383,10 +1420,10 @@ def testNetworkComparison(self):
13831420 unsorted = [ip4 , ip1 , ip3 , ip2 ]
13841421 unsorted .sort ()
13851422 self .assertEqual (sorted , unsorted )
1386- self .assertRaises ( TypeError , ip1 .__lt__ ,
1387- ipaddress . ip_address ( '10.10.10.0' ) )
1388- self .assertRaises ( TypeError , ip2 .__lt__ ,
1389- ipaddress . ip_address ( '10.10.10.0' ) )
1423+ self .assertIs ( ip1 .__lt__ ( ipaddress . ip_address ( '10.10.10.0' )) ,
1424+ NotImplemented )
1425+ self .assertIs ( ip2 .__lt__ ( ipaddress . ip_address ( '10.10.10.0' )) ,
1426+ NotImplemented )
13901427
13911428 # <=, >=
13921429 self .assertTrue (ipaddress .ip_network ('1.1.1.1' ) <=
0 commit comments