@@ -702,6 +702,19 @@ def _verify_length(
702
702
f"expected length { expected_length } "
703
703
)
704
704
705
+ @staticmethod
706
+ def _validate_hashes (hashes : Dict [str , str ]) -> None :
707
+ if not hashes :
708
+ raise ValueError ("Hashes must be a non empty dictionary" )
709
+ for key , value in hashes .items ():
710
+ if not (isinstance (key , str ) and isinstance (value , str )):
711
+ raise TypeError ("Hashes items must be strings" )
712
+
713
+ @staticmethod
714
+ def _validate_length (length : int ) -> None :
715
+ if length <= 0 :
716
+ raise ValueError (f"Length must be > 0, got { length } " )
717
+
705
718
706
719
class MetaFile (BaseFile ):
707
720
"""A container with information about a particular metadata file.
@@ -730,6 +743,14 @@ def __init__(
730
743
hashes : Optional [Dict [str , str ]] = None ,
731
744
unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
732
745
) -> None :
746
+
747
+ if version <= 0 :
748
+ raise ValueError (f"Metafile version must be > 0, got { version } " )
749
+ if length is not None :
750
+ self ._validate_length (length )
751
+ if hashes is not None :
752
+ self ._validate_hashes (hashes )
753
+
733
754
self .version = version
734
755
self .length = length
735
756
self .hashes = hashes
@@ -742,12 +763,6 @@ def from_dict(cls, meta_dict: Dict[str, Any]) -> "MetaFile":
742
763
length = meta_dict .pop ("length" , None )
743
764
hashes = meta_dict .pop ("hashes" , None )
744
765
745
- # Do some basic input validation
746
- if version <= 0 :
747
- raise ValueError (f"Metafile version must be > 0, got { version } " )
748
- if length is not None and length <= 0 :
749
- raise ValueError (f"Metafile length must be > 0, got { length } " )
750
-
751
766
# All fields left in the meta_dict are unrecognized.
752
767
return cls (version , length , hashes , meta_dict )
753
768
@@ -778,8 +793,7 @@ def verify_length_and_hashes(self, data: Union[bytes, BinaryIO]):
778
793
if self .length is not None :
779
794
self ._verify_length (data , self .length )
780
795
781
- # Skip the check in case of an empty dictionary too
782
- if self .hashes :
796
+ if self .hashes is not None :
783
797
self ._verify_hashes (data , self .hashes )
784
798
785
799
@@ -1033,6 +1047,10 @@ def __init__(
1033
1047
hashes : Dict [str , str ],
1034
1048
unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
1035
1049
) -> None :
1050
+
1051
+ self ._validate_length (length )
1052
+ self ._validate_hashes (hashes )
1053
+
1036
1054
self .length = length
1037
1055
self .hashes = hashes
1038
1056
self .unrecognized_fields = unrecognized_fields or {}
@@ -1049,12 +1067,6 @@ def from_dict(cls, target_dict: Dict[str, Any]) -> "TargetFile":
1049
1067
length = target_dict .pop ("length" )
1050
1068
hashes = target_dict .pop ("hashes" )
1051
1069
1052
- # Do some basic validation checks
1053
- if length <= 0 :
1054
- raise ValueError (f"Targetfile length must be > 0, got { length } " )
1055
- if not hashes :
1056
- raise ValueError ("Missing targetfile hashes" )
1057
-
1058
1070
# All fields left in the target_dict are unrecognized.
1059
1071
return cls (length , hashes , target_dict )
1060
1072
0 commit comments