pytest-dev/pytest · error · TypeError
use setattr(target, name, value) or setattr(target, value)…
Error message
use setattr(target, name, value) or setattr(target, value) with target being a dotted import string
What it means
A TypeError raised by MonkeyPatch.setattr when the two-argument form (value is NOTSET) is used but `target` is not a string. In the two-arg form pytest expects target to be a dotted import path that it can resolve; passing an object, module, or None in that slot is a misuse. The message explains both the three-arg and the two-arg-with-string forms.
Solutions
- Use the three-arg form when you have the target object: `monkeypatch.setattr(obj, 'attr', value)`.
- Use the two-arg form only with a dotted string: `monkeypatch.setattr('pkg.mod.attr', value)`.
- Check that you did not omit the `name` positional argument.
Example fix
# before monkeypatch.setattr(obj, new_value) # after monkeypatch.setattr(obj, 'attr', new_value)
Defensive patterns
Strategy: type-guard
Validate before calling
def validate_setattr_args(target, name, value, NOTSET):
if value is NOTSET and not isinstance(target, str):
raise TypeError('two-arg setattr requires a dotted string target')
if value is not NOTSET and not isinstance(name, str):
raise TypeError('three-arg setattr requires a string name') Type guard
def is_setattr_target_string(t) -> bool:
return isinstance(t, str) Prevention
- Remember the two signatures: (target_obj, name, value) or ('dotted.path', value).
- When in doubt, use the explicit three-arg form.
- Lint test code for monkeypatch.setattr call sites.
When it happens
Trigger: Calling `monkeypatch.setattr(some_object, new_value)` (two args, target is an object) instead of the correct three-arg form, or `monkeypatch.setattr(None, new_value)`. The branch `value is NOTSET and not isinstance(target, str)` fires.
Common situations: Forgetting the attribute name argument; confusing monkeypatch.setattr with unittest.mock.patch signatures; copy-paste from code that used a dotted string.
Related errors
- use setattr(target, name, value) with name being a string…
- must be absolute import path string, not
- {name}
- has no attribute
- object at has no attribute
AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11).
Data as JSON: /api/errors/19aea25bbeb705a3.
Report an issue: GitHub.
Appendix: 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 0d6fbdeffa)