MCPcopy Create free account
hub / github.com/pytest-dev/pytest / _patch_unwrap_mock_aware

Function _patch_unwrap_mock_aware

src/_pytest/doctest.py:465–491  ·  view source on GitHub ↗

Context manager which replaces ``inspect.unwrap`` with a version that's aware of mock objects and doesn't recurse into them.

()

Source from the content-addressed store, hash-verified

463
464@contextmanager
465def _patch_unwrap_mock_aware() -> Generator[None, None, None]:
466 """Context manager which replaces ``inspect.unwrap`` with a version
467 that's aware of mock objects and doesn't recurse into them."""
468 real_unwrap = inspect.unwrap
469
470 def _mock_aware_unwrap(
471 func: Callable[..., Any], *, stop: Optional[Callable[[Any], Any]] = None
472 ) -> Any:
473 try:
474 if stop is None or stop is _is_mocked:
475 return real_unwrap(func, stop=_is_mocked)
476 _stop = stop
477 return real_unwrap(func, stop=lambda obj: _is_mocked(obj) or _stop(func))
478 except Exception as e:
479 warnings.warn(
480 "Got %r when unwrapping %r. This is usually caused "
481 "by a violation of Python's object protocol; see e.g. "
482 "https://github.com/pytest-dev/pytest/issues/5080" % (e, func),
483 PytestWarning,
484 )
485 raise
486
487 inspect.unwrap = _mock_aware_unwrap
488 try:
489 yield
490 finally:
491 inspect.unwrap = real_unwrap
492
493
494class DoctestModule(pytest.Module):

Callers 2

_findMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected