{"id":"293fb74bbcb4f262","repo":"pytest-dev/pytest","slug":"expected-module-names-as-args-got-0-instead","errorCode":null,"errorMessage":"expected module names as *args, got {0} instead","messagePattern":"expected module names as \\*args, got (.+?) instead","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/assertion/__init__.py","lineNumber":103,"sourceCode":"    # Eagerly validate the value; it is only read lazily when an assertion fails.\n    config.getini(\"assertion_text_diff_style\")\n\n\ndef register_assert_rewrite(*names: str) -> None:\n    \"\"\"Register one or more module names to be rewritten on import.\n\n    This function will make sure that this module or all modules inside\n    the package will get their assert statements rewritten.\n    Thus you should make sure to call this before the module is\n    actually imported, usually in your __init__.py if you are a plugin\n    using a package.\n\n    :param names: The module names to register.\n    \"\"\"\n    for name in names:\n        if not isinstance(name, str):\n            msg = \"expected module names as *args, got {0} instead\"  # type: ignore[unreachable]\n            raise TypeError(msg.format(repr(names)))\n    rewrite_hook: RewriteHook\n    for hook in sys.meta_path:\n        if isinstance(hook, rewrite.AssertionRewritingHook):\n            rewrite_hook = hook\n            break\n    else:\n        rewrite_hook = DummyRewriteHook()\n    rewrite_hook.mark_rewrite(*names)\n\n\nclass RewriteHook(Protocol):\n    def mark_rewrite(self, *names: str) -> None: ...\n\n\nclass DummyRewriteHook:\n    \"\"\"A no-op import hook for when rewriting is disabled.\"\"\"\n\n    def mark_rewrite(self, *names: str) -> None:","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/assertion/__init__.py#L85-L121","documentation":"The register_assert_rewrite function accepts module names as *args (strings). If any argument is not a string (e.g., a module object, int, or list), pytest raises TypeError. This ensures the assertion-rewrite hook receives valid module names before the module is imported.","triggerScenarios":"Calling pytest.register_assert_rewrite(my_module) where my_module is an actual module object or other non-string type. The isinstance(name, str) check fails for each non-string argument.","commonSituations":"Passing an imported module object instead of its dotted name string, or passing a list/tuple as a single argument instead of unpacking with *.","solutions":["Pass module name strings, not module objects: register_assert_rewrite('mypkg.mymodule').","If you have a list of names, unpack it: register_assert_rewrite(*names).","Call register_assert_rewrite before the module is imported (typically in __init__.py or conftest.py)."],"exampleFix":"# before\nimport mypkg.utils\npytest.register_assert_rewrite(mypkg.utils)  # module object\n\n# after\npytest.register_assert_rewrite('mypkg.utils')  # string name","handlingStrategy":"type-guard","validationCode":"import pytest\n\ndef safe_register_assert_rewrite(*names):\n    for name in names:\n        if not isinstance(name, str):\n            raise TypeError(f'Expected module name string, got {type(name).__name__}: {name!r}')\n    pytest.register_assert_rewrite(*names)","typeGuard":"def is_module_name_string(value) -> bool:\n    return isinstance(value, str) and len(value) > 0 and all(\n        part.isidentifier() for part in value.split('.')\n    )","tryCatchPattern":null,"preventionTips":["Always pass dotted module name strings to register_assert_rewrite, never module objects.","Call register_assert_rewrite early in __init__.py or conftest.py before importing the target module.","Use type annotations to enforce str-only *args in wrapper functions."],"tags":["api","type-error","assertion-rewrite"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}