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

Function register_assert_rewrite

src/_pytest/assertion/__init__.py:47–70  ·  view source on GitHub ↗

Register one or more module names to be rewritten on import. This function will make sure that this module or all modules inside the package will get their assert statements rewritten. Thus you should make sure to call this before the module is actually imported, usually in your __i

(*names: str)

Source from the content-addressed store, hash-verified

45
46
47def register_assert_rewrite(*names: str) -> None:
48 """Register one or more module names to be rewritten on import.
49
50 This function will make sure that this module or all modules inside
51 the package will get their assert statements rewritten.
52 Thus you should make sure to call this before the module is
53 actually imported, usually in your __init__.py if you are a plugin
54 using a package.
55
56 :raises TypeError: If the given module names are not strings.
57 """
58 for name in names:
59 if not isinstance(name, str):
60 msg = "expected module names as *args, got {0} instead" # type: ignore[unreachable]
61 raise TypeError(msg.format(repr(names)))
62 for hook in sys.meta_path:
63 if isinstance(hook, rewrite.AssertionRewritingHook):
64 importhook = hook
65 break
66 else:
67 # TODO(typing): Add a protocol for mark_rewrite() and use it
68 # for importhook and for PytestPluginManager.rewrite_hook.
69 importhook = DummyRewriteHook() # type: ignore
70 importhook.mark_rewrite(*names)
71
72
73class DummyRewriteHook:

Callers

nothing calls this directly

Calls 3

mark_rewriteMethod · 0.95
DummyRewriteHookClass · 0.85
formatMethod · 0.45

Tested by

no test coverage detected