@@ -28,8 +28,7 @@ class Hashes:
28
28
29
29
"""
30
30
31
- def __init__ (self , hashes = None ):
32
- # type: (Dict[str, List[str]]) -> None
31
+ def __init__ (self , hashes : Dict [str , List [str ]] = None ) -> None :
33
32
"""
34
33
:param hashes: A dict of algorithm names pointing to lists of allowed
35
34
hex digests
@@ -41,8 +40,7 @@ def __init__(self, hashes=None):
41
40
allowed [alg ] = sorted (keys )
42
41
self ._allowed = allowed
43
42
44
- def __and__ (self , other ):
45
- # type: (Hashes) -> Hashes
43
+ def __and__ (self , other : "Hashes" ) -> "Hashes" :
46
44
if not isinstance (other , Hashes ):
47
45
return NotImplemented
48
46
@@ -62,21 +60,14 @@ def __and__(self, other):
62
60
return Hashes (new )
63
61
64
62
@property
65
- def digest_count (self ):
66
- # type: () -> int
63
+ def digest_count (self ) -> int :
67
64
return sum (len (digests ) for digests in self ._allowed .values ())
68
65
69
- def is_hash_allowed (
70
- self ,
71
- hash_name , # type: str
72
- hex_digest , # type: str
73
- ):
74
- # type: (...) -> bool
66
+ def is_hash_allowed (self , hash_name : str , hex_digest : str ) -> bool :
75
67
"""Return whether the given hex digest is allowed."""
76
68
return hex_digest in self ._allowed .get (hash_name , [])
77
69
78
- def check_against_chunks (self , chunks ):
79
- # type: (Iterator[bytes]) -> None
70
+ def check_against_chunks (self , chunks : Iterator [bytes ]) -> None :
80
71
"""Check good hashes against ones built from iterable of chunks of
81
72
data.
82
73
@@ -99,37 +90,31 @@ def check_against_chunks(self, chunks):
99
90
return
100
91
self ._raise (gots )
101
92
102
- def _raise (self , gots ):
103
- # type: (Dict[str, _Hash]) -> NoReturn
93
+ def _raise (self , gots : Dict [str , "_Hash" ]) -> "NoReturn" :
104
94
raise HashMismatch (self ._allowed , gots )
105
95
106
- def check_against_file (self , file ):
107
- # type: (BinaryIO) -> None
96
+ def check_against_file (self , file : BinaryIO ) -> None :
108
97
"""Check good hashes against a file-like object
109
98
110
99
Raise HashMismatch if none match.
111
100
112
101
"""
113
102
return self .check_against_chunks (read_chunks (file ))
114
103
115
- def check_against_path (self , path ):
116
- # type: (str) -> None
104
+ def check_against_path (self , path : str ) -> None :
117
105
with open (path , "rb" ) as file :
118
106
return self .check_against_file (file )
119
107
120
- def __bool__ (self ):
121
- # type: () -> bool
108
+ def __bool__ (self ) -> bool :
122
109
"""Return whether I know any known-good hashes."""
123
110
return bool (self ._allowed )
124
111
125
- def __eq__ (self , other ):
126
- # type: (object) -> bool
112
+ def __eq__ (self , other : object ) -> bool :
127
113
if not isinstance (other , Hashes ):
128
114
return NotImplemented
129
115
return self ._allowed == other ._allowed
130
116
131
- def __hash__ (self ):
132
- # type: () -> int
117
+ def __hash__ (self ) -> int :
133
118
return hash (
134
119
"," .join (
135
120
sorted (
@@ -149,13 +134,11 @@ class MissingHashes(Hashes):
149
134
150
135
"""
151
136
152
- def __init__ (self ):
153
- # type: () -> None
137
+ def __init__ (self ) -> None :
154
138
"""Don't offer the ``hashes`` kwarg."""
155
139
# Pass our favorite hash in to generate a "gotten hash". With the
156
140
# empty list, it will never match, so an error will always raise.
157
141
super ().__init__ (hashes = {FAVORITE_HASH : []})
158
142
159
- def _raise (self , gots ):
160
- # type: (Dict[str, _Hash]) -> NoReturn
143
+ def _raise (self , gots : Dict [str , "_Hash" ]) -> "NoReturn" :
161
144
raise HashMissing (gots [FAVORITE_HASH ].hexdigest ())
0 commit comments