pytest-dev/pytest · error · TypeError

expected module names as *args, got {0} instead

Error message

expected module names as *args, got {0} instead

What it means

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.

Source

Thrown at src/_pytest/assertion/__init__.py:103

    # Eagerly validate the value; it is only read lazily when an assertion fails.
    config.getini("assertion_text_diff_style")


def register_assert_rewrite(*names: str) -> None:
    """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 __init__.py if you are a plugin
    using a package.

    :param names: The module names to register.
    """
    for name in names:
        if not isinstance(name, str):
            msg = "expected module names as *args, got {0} instead"  # type: ignore[unreachable]
            raise TypeError(msg.format(repr(names)))
    rewrite_hook: RewriteHook
    for hook in sys.meta_path:
        if isinstance(hook, rewrite.AssertionRewritingHook):
            rewrite_hook = hook
            break
    else:
        rewrite_hook = DummyRewriteHook()
    rewrite_hook.mark_rewrite(*names)


class RewriteHook(Protocol):
    def mark_rewrite(self, *names: str) -> None: ...


class DummyRewriteHook:
    """A no-op import hook for when rewriting is disabled."""

    def mark_rewrite(self, *names: str) -> None:

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Pass module name strings, not module objects: register_assert_rewrite('mypkg.mymodule').
  2. If you have a list of names, unpack it: register_assert_rewrite(*names).
  3. Call register_assert_rewrite before the module is imported (typically in __init__.py or conftest.py).

Example fix

# before
import mypkg.utils
pytest.register_assert_rewrite(mypkg.utils)  # module object

# after
pytest.register_assert_rewrite('mypkg.utils')  # string name
Defensive patterns

Strategy: type-guard

Validate before calling

import pytest

def safe_register_assert_rewrite(*names):
    for name in names:
        if not isinstance(name, str):
            raise TypeError(f'Expected module name string, got {type(name).__name__}: {name!r}')
    pytest.register_assert_rewrite(*names)

Type guard

def is_module_name_string(value) -> bool:
    return isinstance(value, str) and len(value) > 0 and all(
        part.isidentifier() for part in value.split('.')
    )

Prevention

When it happens

Trigger: 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.

Common situations: Passing an imported module object instead of its dotted name string, or passing a list/tuple as a single argument instead of unpacking with *.

Related errors


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/293fb74bbcb4f262.json. Report an issue: GitHub.