MCPcopy
hub / github.com/pytest-dev/pytest / WarningsChecker

Class WarningsChecker

src/_pytest/recwarn.py:265–379  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

263
264@final
265class WarningsChecker(WarningsRecorder):
266 def __init__(
267 self,
268 expected_warning: type[Warning] | tuple[type[Warning], ...] = Warning,
269 match_expr: str | re.Pattern[str] | None = None,
270 *,
271 _ispytest: bool = False,
272 ) -> None:
273 check_ispytest(_ispytest)
274 super().__init__(_ispytest=True)
275
276 msg = "exceptions must be derived from Warning, not %s"
277 if isinstance(expected_warning, tuple):
278 for exc in expected_warning:
279 if not issubclass(exc, Warning):
280 raise TypeError(msg % type(exc))
281 expected_warning_tup = expected_warning
282 elif isinstance(expected_warning, type) and issubclass(
283 expected_warning, Warning
284 ):
285 expected_warning_tup = (expected_warning,)
286 else:
287 raise TypeError(msg % type(expected_warning))
288
289 self.expected_warning = expected_warning_tup
290 self.match_expr = match_expr
291
292 def matches(self, warning: warnings.WarningMessage) -> bool:
293 assert self.expected_warning is not None
294 return issubclass(warning.category, self.expected_warning) and bool(
295 self.match_expr is None or re.search(self.match_expr, str(warning.message))
296 )
297
298 def __exit__(
299 self,
300 exc_type: type[BaseException] | None,
301 exc_val: BaseException | None,
302 exc_tb: TracebackType | None,
303 ) -> None:
304 super().__exit__(exc_type, exc_val, exc_tb)
305
306 __tracebackhide__ = True
307
308 # BaseExceptions like pytest.{skip,fail,xfail,exit} or Ctrl-C within
309 # pytest.warns should *not* trigger "DID NOT WARN" and get suppressed
310 # when the warning doesn't happen. Control-flow exceptions should always
311 # propagate.
312 if exc_val is not None and (
313 not isinstance(exc_val, Exception)
314 # Exit is an Exception, not a BaseException, for some reason.
315 or isinstance(exc_val, Exit)
316 ):
317 return
318
319 def found_str() -> str:
320 return pformat([record.message for record in self], indent=2)
321
322 try:

Callers 2

test_typecheckingMethod · 0.90
warnsFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_typecheckingMethod · 0.72