@@ -135,7 +135,7 @@ def _describe_set_difference(self) -> str:
135135 return "\n " .join (lines )
136136
137137
138- class Equals (_BinaryComparison ):
138+ class Equals (_BinaryComparison [ T ] ):
139139 """Matches if the items are equal."""
140140
141141 comparator = operator .eq
@@ -165,7 +165,7 @@ def match(self, other: T) -> Mismatch | None:
165165 return _BinaryMismatch (other , "!=" , self ._expected , False )
166166
167167
168- class NotEquals (_BinaryComparison ):
168+ class NotEquals (_BinaryComparison [ T ] ):
169169 """Matches if the items are not equal.
170170
171171 In most cases, this is equivalent to ``Not(Equals(foo))``. The difference
@@ -176,38 +176,38 @@ class NotEquals(_BinaryComparison):
176176 mismatch_string = "=="
177177
178178
179- class Is (_BinaryComparison ):
179+ class Is (_BinaryComparison [ T ] ):
180180 """Matches if the items are identical."""
181181
182182 comparator = operator .is_
183183 mismatch_string = "is not"
184184
185185
186- class LessThan (_BinaryComparison ):
186+ class LessThan (_BinaryComparison [ T ] ):
187187 """Matches if the item is less than the matchers reference object."""
188188
189189 comparator = operator .lt
190190 mismatch_string = ">="
191191
192192
193- class GreaterThan (_BinaryComparison ):
193+ class GreaterThan (_BinaryComparison [ T ] ):
194194 """Matches if the item is greater than the matchers reference object."""
195195
196196 comparator = operator .gt
197197 mismatch_string = "<="
198198
199199
200- class _NotNearlyEqual (Mismatch ):
200+ class _NotNearlyEqual (Mismatch , Generic [ T ] ):
201201 """Mismatch for Nearly matcher."""
202202
203- def __init__ (self , actual , expected , delta ) :
203+ def __init__ (self , actual : T , expected : T , delta : Any ) -> None :
204204 self .actual = actual
205205 self .expected = expected
206206 self .delta = delta
207207
208- def describe (self ):
208+ def describe (self ) -> str :
209209 try :
210- diff = abs (self .actual - self .expected )
210+ diff = abs (self .actual - self .expected ) # type: ignore[operator]
211211 return (
212212 f"{ self .actual !r} is not nearly equal to { self .expected !r} : "
213213 f"difference { diff !r} exceeds tolerance { self .delta !r} "
@@ -219,7 +219,7 @@ def describe(self):
219219 )
220220
221221
222- class Nearly (Matcher ):
222+ class Nearly (Matcher [ T ] ):
223223 """Matches if a value is nearly equal to the expected value.
224224
225225 This matcher is useful for comparing floating point values where exact
@@ -232,7 +232,7 @@ class Nearly(Matcher):
232232 operations (e.g., integers, floats, Decimal, etc.).
233233 """
234234
235- def __init__ (self , expected , delta = 0.001 ):
235+ def __init__ (self , expected : T , delta : Any = 0.001 ) -> None :
236236 """Create a Nearly matcher.
237237
238238 :param expected: The expected value to compare against.
@@ -242,12 +242,12 @@ def __init__(self, expected, delta=0.001):
242242 self .expected = expected
243243 self .delta = delta
244244
245- def __str__ (self ):
245+ def __str__ (self ) -> str :
246246 return f"Nearly({ self .expected !r} , delta={ self .delta !r} )"
247247
248- def match (self , actual ) :
248+ def match (self , actual : T ) -> Mismatch | None :
249249 try :
250- diff = abs (actual - self .expected )
250+ diff = abs (actual - self .expected ) # type: ignore[operator]
251251 if diff <= self .delta :
252252 return None
253253 except (TypeError , AttributeError ):
@@ -256,25 +256,25 @@ def match(self, actual):
256256 return _NotNearlyEqual (actual , self .expected , self .delta )
257257
258258
259- class SameMembers (Matcher ):
259+ class SameMembers (Matcher [ list [ T ]] ):
260260 """Matches if two iterators have the same members.
261261
262262 This is not the same as set equivalence. The two iterators must be of the
263263 same length and have the same repetitions.
264264 """
265265
266- def __init__ (self , expected ) :
266+ def __init__ (self , expected : list [ T ]) -> None :
267267 super ().__init__ ()
268268 self .expected = expected
269269
270- def __str__ (self ):
270+ def __str__ (self ) -> str :
271271 return f"{ self .__class__ .__name__ } ({ self .expected !r} )"
272272
273- def match (self , observed ) :
273+ def match (self , observed : list [ T ]) -> Mismatch | None :
274274 expected_only = list_subtract (self .expected , observed )
275275 observed_only = list_subtract (observed , self .expected )
276276 if expected_only == observed_only == []:
277- return
277+ return None
278278 return PostfixedMismatch (
279279 (
280280 f"\n missing: { _format (expected_only )} \n "
@@ -285,7 +285,7 @@ def match(self, observed):
285285
286286
287287class DoesNotStartWith (Mismatch ):
288- def __init__ (self , matchee , expected ) :
288+ def __init__ (self , matchee : str | bytes , expected : str | bytes ) -> None :
289289 """Create a DoesNotStartWith Mismatch.
290290
291291 :param matchee: the string that did not match.
@@ -294,33 +294,33 @@ def __init__(self, matchee, expected):
294294 self .matchee = matchee
295295 self .expected = expected
296296
297- def describe (self ):
297+ def describe (self ) -> str :
298298 return (
299299 f"{ text_repr (self .matchee )} does not start with { text_repr (self .expected )} ."
300300 )
301301
302302
303- class StartsWith (Matcher ):
303+ class StartsWith (Matcher [ str | bytes ] ):
304304 """Checks whether one string starts with another."""
305305
306- def __init__ (self , expected ) :
306+ def __init__ (self , expected : str | bytes ) -> None :
307307 """Create a StartsWith Matcher.
308308
309309 :param expected: the string that matchees should start with.
310310 """
311311 self .expected = expected
312312
313- def __str__ (self ):
313+ def __str__ (self ) -> str :
314314 return f"StartsWith({ self .expected !r} )"
315315
316- def match (self , matchee ) :
317- if not matchee .startswith (self .expected ):
316+ def match (self , matchee : str | bytes ) -> Mismatch | None :
317+ if not matchee .startswith (self .expected ): # type: ignore[arg-type]
318318 return DoesNotStartWith (matchee , self .expected )
319319 return None
320320
321321
322322class DoesNotEndWith (Mismatch ):
323- def __init__ (self , matchee , expected ) :
323+ def __init__ (self , matchee : str | bytes , expected : str | bytes ) -> None :
324324 """Create a DoesNotEndWith Mismatch.
325325
326326 :param matchee: the string that did not match.
@@ -329,27 +329,27 @@ def __init__(self, matchee, expected):
329329 self .matchee = matchee
330330 self .expected = expected
331331
332- def describe (self ):
332+ def describe (self ) -> str :
333333 return (
334334 f"{ text_repr (self .matchee )} does not end with { text_repr (self .expected )} ."
335335 )
336336
337337
338- class EndsWith (Matcher ):
338+ class EndsWith (Matcher [ str | bytes ] ):
339339 """Checks whether one string ends with another."""
340340
341- def __init__ (self , expected ) :
341+ def __init__ (self , expected : str | bytes ) -> None :
342342 """Create a EndsWith Matcher.
343343
344344 :param expected: the string that matchees should end with.
345345 """
346346 self .expected = expected
347347
348- def __str__ (self ):
348+ def __str__ (self ) -> str :
349349 return f"EndsWith({ self .expected !r} )"
350350
351- def match (self , matchee ) :
352- if not matchee .endswith (self .expected ):
351+ def match (self , matchee : str | bytes ) -> Mismatch | None :
352+ if not matchee .endswith (self .expected ): # type: ignore[arg-type]
353353 return DoesNotEndWith (matchee , self .expected )
354354 return None
355355
@@ -459,7 +459,7 @@ def match(self, value: str) -> Mismatch | None:
459459 return None
460460
461461
462- def has_len (x , y ) :
462+ def has_len (x : Any , y : int ) -> bool :
463463 return len (x ) == y
464464
465465
0 commit comments