{"id":"9465c8efbc5fa489","repo":"pytest-dev/pytest","slug":"must-be-absolute-import-path-string-not-import-p","errorCode":null,"errorMessage":"must be absolute import path string, not {import_path!r}","messagePattern":"must be absolute import path string, not (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/monkeypatch.py","lineNumber":105,"sourceCode":"            else:\n                raise ImportError(f\"import error in {used}: {ex}\") from ex\n        found = annotated_getattr(found, part, used)\n    return found\n\n\ndef annotated_getattr(obj: object, name: str, ann: str) -> object:\n    try:\n        obj = getattr(obj, name)\n    except AttributeError as e:\n        raise AttributeError(\n            f\"{type(obj).__name__!r} object at {ann} has no attribute {name!r}\"\n        ) from e\n    return obj\n\n\ndef derive_importpath(import_path: str, raising: bool) -> tuple[str, object]:\n    if not isinstance(import_path, str) or \".\" not in import_path:\n        raise TypeError(f\"must be absolute import path string, not {import_path!r}\")\n    module, attr = import_path.rsplit(\".\", 1)\n    target = resolve(module)\n    if raising:\n        annotated_getattr(target, attr, ann=module)\n    return attr, target\n\n\n@final\nclass MonkeyPatch:\n    \"\"\"Helper to conveniently monkeypatch attributes/items/environment\n    variables/syspath.\n\n    Returned by the :fixture:`monkeypatch` fixture.\n\n    .. versionchanged:: 6.2\n        Can now also be used directly as `pytest.MonkeyPatch()`, for when\n        the fixture is not available. In this case, use\n        :meth:`with MonkeyPatch.context() as mp: <context>` or remember to call","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/monkeypatch.py#L87-L123","documentation":"`derive_importpath` requires a non-empty `str` containing at least one `.` separating the module path from the attribute. Anything else (non-string, or a string with no dot) raises TypeError. This validates the dotted-string form of `monkeypatch.setattr`/`delattr`.","triggerScenarios":"`monkeypatch.setattr('os', fake)` (no attribute segment), `monkeypatch.setattr(123, fake)` (non-string), or `monkeypatch.setattr('', fake)`.","commonSituations":"Passing a module object instead of a dotted string to the two-argument form; forgetting the attribute part of the path.","solutions":["Use the dotted form `'module.attr'` for the two-argument `setattr(target_string, value)` call.","Or switch to the three-argument form `setattr(module_obj, 'attr', value)` passing the module object directly."],"exampleFix":"# before\nmonkeypatch.setattr('os', fake)\n# after\nmonkeypatch.setattr('os.getcwd', fake)  # or: monkeypatch.setattr(os, 'getcwd', fake)","handlingStrategy":"type-guard","validationCode":"def is_dotted_string(p) -> bool:\n    return isinstance(p, str) and '.' in p","typeGuard":"def is_dotted_import_path(p: object) -> bool:\n    return isinstance(p, str) and '.' in p","tryCatchPattern":null,"preventionTips":["Use the dotted `'module.attr'` form, or pass the module object plus a string name.","Avoid passing bare module names or non-strings to the two-arg form."],"tags":["pytest","monkeypatch","type-error","import-path"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}