{"record":{"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":"exception","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/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/nodes.py#L240-L276","documentation":"Raised by Node.warn when the argument is not an instance of Warning or a subclass. Since pytest 6.2 any Warning subclass is accepted (not just PytestWarning), but a non-Warning value (string, Exception, int) is rejected at line 258 with a ValueError before warnings.warn_explicit is called. This avoids a confusing generic type error downstream in the warnings machinery.","triggerScenarios":"node.warn('some message string'); node.warn(Exception('x')); node.warn(42); passing a warning class instead of an instance like node.warn(PytestWarning).","commonSituations":"Developers pass a plain string expecting logging-style behavior; passing a warning class instead of an instantiated warning object.","solutions":["Wrap the message in a Warning subclass: node.warn(PytestWarning('message')).","If using UserWarning or a custom warning, instantiate it first.","Do not pass raw strings, ints, or non-Warning exceptions to warn()."],"exampleFix":"// before\nnode.warn('deprecated feature')\n// after\nimport warnings\nnode.warn(warnings.UserWarning('deprecated feature'))","handlingStrategy":"type-guard","validationCode":"if not isinstance(warning, Warning):\n    raise ValueError('expected a Warning instance')","typeGuard":"def _is_warning_instance(w) -> bool:\n    return isinstance(w, Warning)","tryCatchPattern":null,"preventionTips":["Always wrap messages in a Warning subclass instance.","Never pass the Warning class itself or a raw string."],"tags":["pytest","nodes","valueerror","warnings"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}