@@ -53,7 +53,10 @@ def test_workflow_raises() -> None:
5353
5454# Define some custom exceptions for testing
5555class CustomError (Exception ):
56- pass
56+ def __init__ (self , code : int = 0 , message : str = "" ):
57+ self .code = code
58+ self .message = message
59+ super ().__init__ (message )
5760
5861
5962class AnotherCustomError (Exception ):
@@ -78,6 +81,29 @@ async def async_function(*, raise_error: bool, raise_another_error: bool) -> str
7881 return "Success"
7982
8083
84+ # Test functions with predicate
85+ @suppress_exceptions (
86+ (CustomError ,),
87+ reason = "Only suppress CustomError with code >= 100" ,
88+ predicate = lambda e : hasattr (e , "code" ) and e .code >= 100 ,
89+ )
90+ def sync_function_with_predicate (error_code : int = 0 ) -> str :
91+ if error_code > 0 :
92+ raise CustomError (code = error_code , message = f"Error { error_code } " )
93+ return "Success"
94+
95+
96+ @suppress_exceptions (
97+ (CustomError ,),
98+ reason = "Only suppress CustomError with code >= 100" ,
99+ predicate = lambda e : hasattr (e , "code" ) and e .code >= 100 ,
100+ )
101+ async def async_function_with_predicate (error_code : int = 0 ) -> str :
102+ if error_code > 0 :
103+ raise CustomError (code = error_code , message = f"Error { error_code } " )
104+ return "Success"
105+
106+
81107def test_sync_function_no_exception ():
82108 result = sync_function (raise_error = False , raise_another_error = False )
83109 assert result == "Success"
@@ -106,3 +132,78 @@ def test_sync_function_with_different_exception():
106132async def test_async_function_with_different_exception ():
107133 with pytest .raises (AnotherCustomError ):
108134 await async_function (raise_error = False , raise_another_error = True )
135+
136+
137+ # Tests for predicate functionality
138+ def test_sync_function_predicate_suppresses_matching_exception ():
139+ """Test that predicate suppresses exception when condition is met"""
140+ result = sync_function_with_predicate (
141+ error_code = 150
142+ ) # code >= 100, should be suppressed
143+ assert result is None
144+
145+
146+ def test_sync_function_predicate_raises_non_matching_exception ():
147+ """Test that predicate does not suppress exception when condition is not met"""
148+ with pytest .raises (CustomError ):
149+ sync_function_with_predicate (error_code = 50 ) # code < 100, should be raised
150+
151+
152+ def test_sync_function_predicate_no_exception ():
153+ """Test that function works normally when no exception is raised"""
154+ result = sync_function_with_predicate (error_code = 0 )
155+ assert result == "Success"
156+
157+
158+ async def test_async_function_predicate_suppresses_matching_exception ():
159+ """Test that predicate suppresses exception when condition is met"""
160+ result = await async_function_with_predicate (
161+ error_code = 200
162+ ) # code >= 100, should be suppressed
163+ assert result is None
164+
165+
166+ async def test_async_function_predicate_raises_non_matching_exception ():
167+ """Test that predicate does not suppress exception when condition is not met"""
168+ with pytest .raises (CustomError ):
169+ await async_function_with_predicate (
170+ error_code = 25
171+ ) # code < 100, should be raised
172+
173+
174+ async def test_async_function_predicate_no_exception ():
175+ """Test that function works normally when no exception is raised"""
176+ result = await async_function_with_predicate (error_code = 0 )
177+ assert result == "Success"
178+
179+
180+ # Test edge cases for predicate
181+ @suppress_exceptions (
182+ (ValueError , TypeError ),
183+ reason = "Complex predicate test" ,
184+ predicate = lambda e : "suppress" in str (e ).lower (),
185+ )
186+ def function_with_complex_predicate (message : str ) -> str :
187+ if "value" in message :
188+ raise ValueError (message )
189+ if "type" in message :
190+ raise TypeError (message )
191+ return "Success"
192+
193+
194+ def test_complex_predicate_suppresses_matching ():
195+ """Test complex predicate that checks exception message"""
196+ result = function_with_complex_predicate ("please suppress this value error" )
197+ assert result is None
198+
199+
200+ def test_complex_predicate_raises_non_matching ():
201+ """Test complex predicate raises when condition not met"""
202+ with pytest .raises (ValueError ):
203+ function_with_complex_predicate ("value error without keyword" )
204+
205+
206+ def test_complex_predicate_different_exception_type ():
207+ """Test complex predicate with different exception type"""
208+ result = function_with_complex_predicate ("type error with suppress keyword" )
209+ assert result is None
0 commit comments