@@ -58,51 +58,27 @@ def __gt__(self, other: Any) -> bool:
5858 return False
5959
6060
61- class ComparableDict :
61+ class ComparableDict ( ComparableTuple ) :
6262 """
6363 Allows comparison of dictionaries, allowing for missing/None values.
6464 """
6565
66- def __init__ (self , dict_ : Dict [Any , Any ]) -> None :
67- self ._dict = dict_
68-
69- def __lt__ (self , other : Any ) -> bool :
70- if not isinstance (other , ComparableDict ):
71- return True
72- keys = sorted (self ._dict .keys () | other ._dict .keys ())
73- return ComparableTuple (self ._dict .get (k ) for k in keys ) \
74- < ComparableTuple (other ._dict .get (k ) for k in keys )
75-
76- def __gt__ (self , other : Any ) -> bool :
77- if not isinstance (other , ComparableDict ):
78- return False
79- keys = sorted (self ._dict .keys () | other ._dict .keys ())
80- return ComparableTuple (self ._dict .get (k ) for k in keys ) \
81- > ComparableTuple (other ._dict .get (k ) for k in keys )
82-
83- def __eq__ (self , other ):
84- if not isinstance (other , ComparableDict ):
85- return False
86- return self ._dict == other ._dict
87-
88- def __hash__ (self ) -> int :
89- return hash (tuple (sorted (self ._dict .items ())))
90-
91- def __repr__ (self ) -> str :
92- return f'<ComparableDict { self ._dict !r} >'
66+ def __new__ (cls , d :dict ) -> 'ComparableDict' :
67+ data = tuple (sorted (d .items ()))
68+ return super (ComparableDict , cls ).__new__ (cls , data )
9369
9470
9571class ComparablePackageURL (ComparableTuple ):
9672 """
9773 Allows comparison of PackageURL, allowing for qualifiers.
9874 """
9975
100- def __new__ (cls , purl : 'PackageURL' ) -> 'ComparablePackageURL' :
101- parts = (
102- purl .type ,
103- purl .namespace ,
104- purl .version ,
105- ComparableDict (purl .qualifiers ) if isinstance (purl .qualifiers , dict ) else purl .qualifiers ,
106- purl .subpath
76+ def __new__ (cls , p : 'PackageURL' ) -> 'ComparablePackageURL' :
77+ data = (
78+ p .type ,
79+ p .namespace ,
80+ p .version ,
81+ ComparableDict (p .qualifiers ) if isinstance (p .qualifiers , dict ) else p .qualifiers ,
82+ p .subpath
10783 )
108- return super (ComparablePackageURL , cls ).__new__ (cls , parts )
84+ return super (ComparablePackageURL , cls ).__new__ (cls , data )
0 commit comments