{"id":"7e312bc3757250ae","repo":"pytest-dev/pytest","slug":"warning-must-be-an-instance-of-warning-or-subclass","errorCode":null,"errorMessage":"warning must be an instance of Warning or subclass, got {warning!r}","messagePattern":"warning must be an instance of Warning or subclass, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/nodes.py","lineNumber":258,"sourceCode":"        :param Warning warning:\n            The warning instance to issue.\n\n        :raises ValueError: If ``warning`` instance is not a subclass of Warning.\n\n        Example usage:\n\n        .. code-block:: python\n\n            node.warn(PytestWarning(\"some message\"))\n            node.warn(UserWarning(\"some message\"))\n\n        .. versionchanged:: 6.2\n            Any subclass of :class:`Warning` is now accepted, rather than only\n            :class:`PytestWarning <pytest.PytestWarning>` subclasses.\n        \"\"\"\n        # enforce type checks here to avoid getting a generic type error later otherwise.\n        if not isinstance(warning, Warning):\n            raise ValueError(\n                f\"warning must be an instance of Warning or subclass, got {warning!r}\"\n            )\n        path, lineno = get_fslocation_from_item(self)\n        assert lineno is not None\n        warnings.warn_explicit(\n            warning,\n            category=None,\n            filename=str(path),\n            lineno=lineno + 1,\n        )\n\n    # Methods for ordering nodes.\n\n    @property\n    def nodeid(self) -> str:\n        \"\"\"A ::-separated string denoting its collection tree address.\"\"\"\n        return self._nodeid\n","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/nodes.py#L240-L276","documentation":"Node.warn expects an instance of Warning (or a subclass such as PytestWarning / UserWarning). Passing a string, an exception, or any non-Warning object raises ValueError early so the warning machinery does not fail later with a generic type error.","triggerScenarios":"Calling item.warn('some message') or item.warn(some_string_variable) instead of passing a Warning instance. The check rejects category=None / raw strings.","commonSituations":"Following a tutorial that pre-dates pytest 6.2 (which restricted to PytestWarning) or general confusion between warnings.warn(str) and node.warn(warning).","solutions":["Wrap the message in a Warning subclass: node.warn(PytestWarning('message'))","For generic warnings use UserWarning: node.warn(UserWarning('message'))"],"exampleFix":"// before\nnode.warn('deprecated usage')\n// after\nfrom _pytest.warning_types import PytestWarning\nnode.warn(PytestWarning('deprecated usage'))","handlingStrategy":"type-guard","validationCode":"def safe_warn(node, warning):\n    if not isinstance(warning, Warning):\n        raise TypeError('expected Warning instance')\n    node.warn(warning)","typeGuard":"def is_warning_instance(w) -> bool:\n    return isinstance(w, Warning)","tryCatchPattern":null,"preventionTips":["Always construct a Warning subclass: PytestWarning(msg) or UserWarning(msg)","Never pass a raw string to node.warn"],"tags":["nodes","warnings","pytest","type-check"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}