@@ -294,17 +294,31 @@ def assertMatchesTemplate(self, exc, exc_type, template):
294
294
self .assertEqual (type (exc ), type (template ))
295
295
self .assertEqual (exc .args , template .args )
296
296
297
+ class Predicate :
298
+ def __init__ (self , func ):
299
+ self .func = func
300
+
301
+ def __call__ (self , e ):
302
+ return self .func (e )
303
+
304
+ def method (self , e ):
305
+ return self .func (e )
297
306
298
307
class ExceptionGroupSubgroupTests (ExceptionGroupTestBase ):
299
308
def setUp (self ):
300
309
self .eg = create_simple_eg ()
301
310
self .eg_template = [ValueError (1 ), TypeError (int ), ValueError (2 )]
302
311
303
312
def test_basics_subgroup_split__bad_arg_type (self ):
313
+ class C :
314
+ pass
315
+
304
316
bad_args = ["bad arg" ,
317
+ C ,
305
318
OSError ('instance not type' ),
306
319
[OSError , TypeError ],
307
- (OSError , 42 )]
320
+ (OSError , 42 ),
321
+ ]
308
322
for arg in bad_args :
309
323
with self .assertRaises (TypeError ):
310
324
self .eg .subgroup (arg )
@@ -336,10 +350,14 @@ def test_basics_subgroup_by_type__match(self):
336
350
self .assertMatchesTemplate (subeg , ExceptionGroup , template )
337
351
338
352
def test_basics_subgroup_by_predicate__passthrough (self ):
339
- self .assertIs (self .eg , self .eg .subgroup (lambda e : True ))
353
+ f = lambda e : True
354
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
355
+ self .assertIs (self .eg , self .eg .subgroup (callable ))
340
356
341
357
def test_basics_subgroup_by_predicate__no_match (self ):
342
- self .assertIsNone (self .eg .subgroup (lambda e : False ))
358
+ f = lambda e : False
359
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
360
+ self .assertIsNone (self .eg .subgroup (callable ))
343
361
344
362
def test_basics_subgroup_by_predicate__match (self ):
345
363
eg = self .eg
@@ -350,9 +368,12 @@ def test_basics_subgroup_by_predicate__match(self):
350
368
((ValueError , TypeError ), self .eg_template )]
351
369
352
370
for match_type , template in testcases :
353
- subeg = eg .subgroup (lambda e : isinstance (e , match_type ))
354
- self .assertEqual (subeg .message , eg .message )
355
- self .assertMatchesTemplate (subeg , ExceptionGroup , template )
371
+ f = lambda e : isinstance (e , match_type )
372
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
373
+ with self .subTest (callable = callable ):
374
+ subeg = eg .subgroup (f )
375
+ self .assertEqual (subeg .message , eg .message )
376
+ self .assertMatchesTemplate (subeg , ExceptionGroup , template )
356
377
357
378
358
379
class ExceptionGroupSplitTests (ExceptionGroupTestBase ):
@@ -399,14 +420,18 @@ def test_basics_split_by_type__match(self):
399
420
self .assertIsNone (rest )
400
421
401
422
def test_basics_split_by_predicate__passthrough (self ):
402
- match , rest = self .eg .split (lambda e : True )
403
- self .assertMatchesTemplate (match , ExceptionGroup , self .eg_template )
404
- self .assertIsNone (rest )
423
+ f = lambda e : True
424
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
425
+ match , rest = self .eg .split (callable )
426
+ self .assertMatchesTemplate (match , ExceptionGroup , self .eg_template )
427
+ self .assertIsNone (rest )
405
428
406
429
def test_basics_split_by_predicate__no_match (self ):
407
- match , rest = self .eg .split (lambda e : False )
408
- self .assertIsNone (match )
409
- self .assertMatchesTemplate (rest , ExceptionGroup , self .eg_template )
430
+ f = lambda e : False
431
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
432
+ match , rest = self .eg .split (callable )
433
+ self .assertIsNone (match )
434
+ self .assertMatchesTemplate (rest , ExceptionGroup , self .eg_template )
410
435
411
436
def test_basics_split_by_predicate__match (self ):
412
437
eg = self .eg
@@ -420,14 +445,16 @@ def test_basics_split_by_predicate__match(self):
420
445
]
421
446
422
447
for match_type , match_template , rest_template in testcases :
423
- match , rest = eg .split (lambda e : isinstance (e , match_type ))
424
- self .assertEqual (match .message , eg .message )
425
- self .assertMatchesTemplate (
426
- match , ExceptionGroup , match_template )
427
- if rest_template is not None :
428
- self .assertEqual (rest .message , eg .message )
448
+ f = lambda e : isinstance (e , match_type )
449
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
450
+ match , rest = eg .split (callable )
451
+ self .assertEqual (match .message , eg .message )
429
452
self .assertMatchesTemplate (
430
- rest , ExceptionGroup , rest_template )
453
+ match , ExceptionGroup , match_template )
454
+ if rest_template is not None :
455
+ self .assertEqual (rest .message , eg .message )
456
+ self .assertMatchesTemplate (
457
+ rest , ExceptionGroup , rest_template )
431
458
432
459
433
460
class DeepRecursionInSplitAndSubgroup (unittest .TestCase ):
0 commit comments