pytest-dev/pytest · error · TypeError

use setattr(target, name, value) or setattr(target, value) w

Error message

use setattr(target, name, value) or setattr(target, value) with target being a dotted import string

What it means

`monkeypatch.setattr` supports a short form `setattr(target, value)` where `target` is a dotted import string. If `value` was not provided (so the method treats the 2nd arg as `target` for the short form) but that target is not a `str`, the call is ambiguous and TypeError is raised telling you to either pass three args or use a dotted-string target.

Source

Thrown at src/_pytest/monkeypatch.py:225

        Raises :class:`AttributeError` if the attribute does not exist, unless
        ``raising`` is set to False.

        **Where to patch**

        ``monkeypatch.setattr`` works by (temporarily) changing the object that a name points to with another one.
        There can be many names pointing to any individual object, so for patching to work you must ensure
        that you patch the name used by the system under test.

        See the section :ref:`Where to patch <python:where-to-patch>` in the :mod:`unittest.mock`
        docs for a complete explanation, which is meant for :func:`unittest.mock.patch` but
        applies to ``monkeypatch.setattr`` as well.
        """
        __tracebackhide__ = True
        import inspect

        if value is NOTSET:
            if not isinstance(target, str):
                raise TypeError(
                    "use setattr(target, name, value) or "
                    "setattr(target, value) with target being a dotted "
                    "import string"
                )
            value = name
            name, target = derive_importpath(target, raising)
        else:
            if not isinstance(name, str):
                raise TypeError(
                    "use setattr(target, name, value) with name being a string or "
                    "setattr(target, value) with target being a dotted "
                    "import string"
                )

        oldval = getattr(target, name, NOTSET)
        if raising and oldval is NOTSET:
            raise AttributeError(f"{target!r} has no attribute {name!r}")

View on GitHub (pinned to 98b357f69e)

Solutions

  1. If target is an object, pass three arguments: `setattr(obj, 'name', value)`.
  2. If you want the short form, pass a dotted string: `setattr('module.attr', value)`.

Example fix

# before
monkeypatch.setattr(os, fake)
# after
monkeypatch.setattr(os, 'getcwd', fake)
Defensive patterns

Strategy: type-guard

Validate before calling

def valid_setattr_short_form(target) -> bool:
    return isinstance(target, str) and '.' in target

Type guard

def can_use_short_setattr(target: object) -> bool:
    return isinstance(target, str) and '.' in target

Prevention

When it happens

Trigger: `monkeypatch.setattr(os, 'new')` — passing an object as first arg with only two positional args, instead of either `setattr('os.new', val)` or `setattr(os, 'new', val)`.

Common situations: Forgetting the third argument; mixing up the two-arg (string target) and three-arg (object target) overloads.

Related errors


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