@@ -134,50 +134,42 @@ async def test_async_function_with_different_exception():
134134 await async_function (raise_error = False , raise_another_error = True )
135135
136136
137- # Tests for predicate functionality
138137def test_sync_function_predicate_suppresses_matching_exception ():
139- """Test that predicate suppresses exception when condition is met"""
140138 result = sync_function_with_predicate (
141139 error_code = 150
142140 ) # code >= 100, should be suppressed
143141 assert result is None
144142
145143
146144def test_sync_function_predicate_raises_non_matching_exception ():
147- """Test that predicate does not suppress exception when condition is not met"""
148145 with pytest .raises (CustomError ):
149146 sync_function_with_predicate (error_code = 50 ) # code < 100, should be raised
150147
151148
152149def test_sync_function_predicate_no_exception ():
153- """Test that function works normally when no exception is raised"""
154150 result = sync_function_with_predicate (error_code = 0 )
155151 assert result == "Success"
156152
157153
158154async def test_async_function_predicate_suppresses_matching_exception ():
159- """Test that predicate suppresses exception when condition is met"""
160155 result = await async_function_with_predicate (
161156 error_code = 200
162157 ) # code >= 100, should be suppressed
163158 assert result is None
164159
165160
166161async def test_async_function_predicate_raises_non_matching_exception ():
167- """Test that predicate does not suppress exception when condition is not met"""
168162 with pytest .raises (CustomError ):
169163 await async_function_with_predicate (
170164 error_code = 25
171165 ) # code < 100, should be raised
172166
173167
174168async def test_async_function_predicate_no_exception ():
175- """Test that function works normally when no exception is raised"""
176169 result = await async_function_with_predicate (error_code = 0 )
177170 assert result == "Success"
178171
179172
180- # Test edge cases for predicate
181173@suppress_exceptions (
182174 (ValueError , TypeError ),
183175 reason = "Complex predicate test" ,
@@ -192,18 +184,92 @@ def function_with_complex_predicate(message: str) -> str:
192184
193185
194186def test_complex_predicate_suppresses_matching ():
195- """Test complex predicate that checks exception message"""
196187 result = function_with_complex_predicate ("please suppress this value error" )
197188 assert result is None
198189
199190
200191def test_complex_predicate_raises_non_matching ():
201- """Test complex predicate raises when condition not met"""
202- with pytest .raises (ValueError ):
192+ with pytest .raises (ValueError , match = "value error without keyword" ):
203193 function_with_complex_predicate ("value error without keyword" )
204194
205195
206196def test_complex_predicate_different_exception_type ():
207- """Test complex predicate with different exception type"""
208197 result = function_with_complex_predicate ("type error with suppress keyword" )
209198 assert result is None
199+
200+
201+ # Test predicate exception handling
202+ @suppress_exceptions (
203+ (ValueError ,),
204+ reason = "Predicate that raises exception" ,
205+ predicate = lambda _ : bool (1 / 0 ), # This will raise ZeroDivisionError
206+ )
207+ def function_with_failing_predicate () -> str :
208+ msg = "Original error"
209+ raise ValueError (msg )
210+
211+
212+ @suppress_exceptions (
213+ (ValueError ,),
214+ reason = "Predicate that raises exception" ,
215+ predicate = lambda _ : bool (1 / 0 ), # This will raise ZeroDivisionError
216+ )
217+ async def async_function_with_failing_predicate () -> str :
218+ msg = "Original error"
219+ raise ValueError (msg )
220+
221+
222+ def test_sync_function_predicate_exception_reraised (caplog ):
223+ with pytest .raises (ValueError , match = "Original error" ):
224+ function_with_failing_predicate ()
225+
226+ # Check that warning was logged
227+ assert "Predicate function raised exception" in caplog .text
228+ assert "ZeroDivisionError" in caplog .text
229+
230+
231+ async def test_async_function_predicate_exception_reraised (caplog ):
232+ with pytest .raises (ValueError , match = "Original error" ):
233+ await async_function_with_failing_predicate ()
234+
235+ # Check that warning was logged
236+ assert "Predicate function raised exception" in caplog .text
237+ assert "ZeroDivisionError" in caplog .text
238+
239+
240+ @suppress_exceptions (
241+ (ValueError ,),
242+ reason = "Predicate that accesses invalid attribute" ,
243+ predicate = lambda e : e .nonexistent_attribute == "test" ,
244+ )
245+ def function_with_attribute_error_predicate () -> str :
246+ msg = "Original error"
247+ raise ValueError (msg )
248+
249+
250+ def test_predicate_attribute_error_reraised (caplog ):
251+ with pytest .raises (ValueError , match = "Original error" ):
252+ function_with_attribute_error_predicate ()
253+
254+ # Check that warning was logged about predicate failure
255+ assert "Predicate function raised exception" in caplog .text
256+ assert "AttributeError" in caplog .text
257+
258+
259+ @suppress_exceptions (
260+ (ValueError ,),
261+ reason = "Predicate that sometimes works" ,
262+ predicate = lambda e : len (str (e )) > 5 , # Safe predicate
263+ )
264+ def function_with_working_predicate (message : str ) -> str :
265+ raise ValueError (message )
266+
267+
268+ def test_predicate_works_normally ():
269+ # Short message - predicate returns False, exception re-raised
270+ with pytest .raises (ValueError ):
271+ function_with_working_predicate ("Hi" )
272+
273+ # Long message - predicate returns True, exception suppressed
274+ result = function_with_working_predicate ("This is a long error message" )
275+ assert result is None
0 commit comments