{"record":{"id":"19aea25bbeb705a3","repo":"pytest-dev/pytest","slug":"use-setattr-target-name-value-or-setattr-target","errorCode":null,"errorMessage":"use setattr(target, name, value) or setattr(target, value) with target being a dotted import string","messagePattern":"use setattr\\(target, name, value\\) or setattr\\(target, value\\) with target being a dotted import string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/monkeypatch.py","lineNumber":225,"sourceCode":"        Raises :class:`AttributeError` if the attribute does not exist, unless\n        ``raising`` is set to False.\n\n        **Where to patch**\n\n        ``monkeypatch.setattr`` works by (temporarily) changing the object that a name points to with another one.\n        There can be many names pointing to any individual object, so for patching to work you must ensure\n        that you patch the name used by the system under test.\n\n        See the section :ref:`Where to patch <python:where-to-patch>` in the :mod:`unittest.mock`\n        docs for a complete explanation, which is meant for :func:`unittest.mock.patch` but\n        applies to ``monkeypatch.setattr`` as well.\n        \"\"\"\n        __tracebackhide__ = True\n        import inspect\n\n        if value is NOTSET:\n            if not isinstance(target, str):\n                raise TypeError(\n                    \"use setattr(target, name, value) or \"\n                    \"setattr(target, value) with target being a dotted \"\n                    \"import string\"\n                )\n            value = name\n            name, target = derive_importpath(target, raising)\n        else:\n            if not isinstance(name, str):\n                raise TypeError(\n                    \"use setattr(target, name, value) with name being a string or \"\n                    \"setattr(target, value) with target being a dotted \"\n                    \"import string\"\n                )\n\n        oldval = getattr(target, name, NOTSET)\n        if raising and oldval is NOTSET:\n            raise AttributeError(f\"{target!r} has no attribute {name!r}\")\n","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/monkeypatch.py#L207-L243","documentation":"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.","triggerScenarios":"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.","commonSituations":"Forgetting the attribute name argument; confusing monkeypatch.setattr with unittest.mock.patch signatures; copy-paste from code that used a dotted string.","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."],"exampleFix":"# before\nmonkeypatch.setattr(obj, new_value)\n# after\nmonkeypatch.setattr(obj, 'attr', new_value)","handlingStrategy":"type-guard","validationCode":"def validate_setattr_args(target, name, value, NOTSET):\n    if value is NOTSET and not isinstance(target, str):\n        raise TypeError('two-arg setattr requires a dotted string target')\n    if value is not NOTSET and not isinstance(name, str):\n        raise TypeError('three-arg setattr requires a string name')","typeGuard":"def is_setattr_target_string(t) -> bool:\n    return isinstance(t, str)","tryCatchPattern":null,"preventionTips":["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."],"tags":["pytest","monkeypatch","type-error","setattr","api-misuse"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}