@@ -294,17 +294,31 @@ def assertMatchesTemplate(self, exc, exc_type, template):
294294 self .assertEqual (type (exc ), type (template ))
295295 self .assertEqual (exc .args , template .args )
296296
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 )
297306
298307class ExceptionGroupSubgroupTests (ExceptionGroupTestBase ):
299308 def setUp (self ):
300309 self .eg = create_simple_eg ()
301310 self .eg_template = [ValueError (1 ), TypeError (int ), ValueError (2 )]
302311
303312 def test_basics_subgroup_split__bad_arg_type (self ):
313+ class C :
314+ pass
315+
304316 bad_args = ["bad arg" ,
317+ C ,
305318 OSError ('instance not type' ),
306319 [OSError , TypeError ],
307- (OSError , 42 )]
320+ (OSError , 42 ),
321+ ]
308322 for arg in bad_args :
309323 with self .assertRaises (TypeError ):
310324 self .eg .subgroup (arg )
@@ -336,10 +350,14 @@ def test_basics_subgroup_by_type__match(self):
336350 self .assertMatchesTemplate (subeg , ExceptionGroup , template )
337351
338352 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 ))
340356
341357 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 ))
343361
344362 def test_basics_subgroup_by_predicate__match (self ):
345363 eg = self .eg
@@ -350,9 +368,12 @@ def test_basics_subgroup_by_predicate__match(self):
350368 ((ValueError , TypeError ), self .eg_template )]
351369
352370 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 )
356377
357378
358379class ExceptionGroupSplitTests (ExceptionGroupTestBase ):
@@ -399,14 +420,18 @@ def test_basics_split_by_type__match(self):
399420 self .assertIsNone (rest )
400421
401422 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 )
405428
406429 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 )
410435
411436 def test_basics_split_by_predicate__match (self ):
412437 eg = self .eg
@@ -420,14 +445,16 @@ def test_basics_split_by_predicate__match(self):
420445 ]
421446
422447 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 )
429452 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 )
431458
432459
433460class DeepRecursionInSplitAndSubgroup (unittest .TestCase ):
0 commit comments