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)
| 45 | |
| 46 | |
| 47 | def 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 | |
| 73 | class DummyRewriteHook: |
nothing calls this directly
no test coverage detected