@@ -101,12 +101,20 @@ class MetadataFile:
101
101
102
102
hashes : Optional [Dict [str , str ]]
103
103
104
- # TODO: Do we care about stripping out unsupported hash methods?
105
- def __init__ (self , hashes : Optional [Dict [str , str ]]):
106
- if hashes :
107
- hashes = {n : v for n , v in hashes .items () if n in _SUPPORTED_HASHES }
108
- # We need to use this as this is a frozen dataclass
109
- object .__setattr__ (self , "hashes" , hashes )
104
+ def __post_init__ (self ) -> None :
105
+ if self .hashes is not None :
106
+ assert all (name in _SUPPORTED_HASHES for name in self .hashes )
107
+
108
+
109
+ def supported_hashes (hashes : Optional [Dict [str , str ]]) -> Optional [Dict [str , str ]]:
110
+ # Remove any unsupported hash types from the mapping. If this leaves no
111
+ # supported hashes, return None
112
+ if hashes is None :
113
+ return None
114
+ hashes = {n : v for n , v in hashes .items () if n in _SUPPORTED_HASHES }
115
+ if len (hashes ) > 0 :
116
+ return hashes
117
+ return None
110
118
111
119
112
120
def _clean_url_path_part (part : str ) -> str :
@@ -273,7 +281,7 @@ def from_json(
273
281
metadata_info = file_data .get ("dist-info-metadata" , False )
274
282
if isinstance (metadata_info , dict ):
275
283
# The file exists, and hashes have been supplied
276
- metadata_file_data = MetadataFile (metadata_info )
284
+ metadata_file_data = MetadataFile (supported_hashes ( metadata_info ) )
277
285
elif metadata_info :
278
286
# The file exists, but there are no hashes
279
287
metadata_file_data = MetadataFile (None )
@@ -328,7 +336,7 @@ def from_element(
328
336
# The file exists, and hashes have been supplied
329
337
hashname , sep , hashval = metadata_info .partition ("=" )
330
338
if sep == "=" :
331
- metadata_file_data = MetadataFile ({hashname : hashval })
339
+ metadata_file_data = MetadataFile (supported_hashes ( {hashname : hashval }) )
332
340
else :
333
341
# Error - data is wrong. Treat as no hashes supplied.
334
342
logger .debug (
0 commit comments