{"record":{"id":"fc099e9a229a78aa","repo":"pytest-dev/pytest","slug":"cat-is-not-a-warning-subclass","errorCode":null,"errorMessage":"{cat} is not a Warning subclass","messagePattern":"(.+?) is not a Warning subclass","errorType":"exception","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/__init__.py","lineNumber":2344,"sourceCode":"def _resolve_warning_category(category: str) -> type[Warning]:\n    \"\"\"\n    Copied from warnings._getcategory, but changed so it lets exceptions (specially ImportErrors)\n    propagate so we can get access to their tracebacks (#9218).\n    \"\"\"\n    __tracebackhide__ = True\n    if not category:\n        return Warning\n\n    if \".\" not in category:\n        import builtins as m\n\n        klass = category\n    else:\n        module, _, klass = category.rpartition(\".\")\n        m = importlib.import_module(module)\n    cat = getattr(m, klass)\n    if not issubclass(cat, Warning):\n        raise UsageError(f\"{cat} is not a Warning subclass\")\n    return cast(type[Warning], cat)\n\n\ndef apply_warning_filters(\n    config_filters: Iterable[str], cmdline_filters: Iterable[str]\n) -> None:\n    \"\"\"Applies pytest-configured filters to the warnings module\"\"\"\n    # Filters should have this precedence: cmdline options, config.\n    # Filters should be applied in the inverse order of precedence.\n    for arg in config_filters:\n        try:\n            warnings.filterwarnings(*parse_warning_filter(arg, escape=False))\n        except ImportError as e:\n            warnings.warn(\n                f\"Failed to import filter module '{e.name}': {arg}\", PytestConfigWarning\n            )\n            continue\n","sourceCodeStart":2326,"sourceCodeEnd":2362,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/config/__init__.py#L2326-L2362","documentation":"Raised by _resolve_warning_category() when the category name in a warning filter resolves to a class that is not a subclass of Warning. Warning filters require the category field to be a Warning subclass (e.g., DeprecationWarning, UserWarning). The resolved class is checked with issubclass(cat, Warning).","triggerScenarios":"Providing a warning filter where the category field names a class that is not a Warning subclass. Example: filterwarnings = [\"ignore::ValueError\"] or [\"ignore::os.PathLike\"]. The category is resolved via importlib/import and getattr, then the issubclass check fails.","commonSituations":"Using a built-in exception class (e.g., ValueError, RuntimeError) instead of a Warning subclass. Typo in the category name resolving to a non-Warning class. Specifying a fully-qualified path to a custom class that does not inherit from Warning.","solutions":["Use a Warning subclass: DeprecationWarning, UserWarning, RuntimeWarning, FutureWarning, etc.","For custom warnings, ensure the class inherits from Warning (or a Warning subclass)","Use the bare 'Warning' category (empty field defaults to Warning) to match all warnings"],"exampleFix":"# before\nfilterwarnings = [\"ignore::ValueError\"]\n\n# after\nfilterwarnings = [\"ignore::UserWarning\"]","handlingStrategy":"validation","validationCode":"import importlib\nfrom warnings import Warning\ndef validate_warning_category(category: str) -> bool:\n    if not category:\n        return True  # defaults to Warning\n    if '.' not in category:\n        import builtins\n        cat = getattr(builtins, category, None)\n    else:\n        module, _, klass = category.rpartition('.')\n        try:\n            m = importlib.import_module(module)\n            cat = getattr(m, klass, None)\n        except ImportError:\n            return False\n    return cat is not None and isinstance(cat, type) and issubclass(cat, Warning)","typeGuard":"from warnings import Warning\ndef is_warning_subclass(cls: type) -> bool:\n    return isinstance(cls, type) and issubclass(cls, Warning)","tryCatchPattern":"from pytest import UsageError\ntry:\n    parse_warning_filter(arg, escape=True)\nexcept UsageError as e:\n    print(f\"Invalid warning category: {e}\")","preventionTips":["Use Warning subclasses: DeprecationWarning, UserWarning, RuntimeWarning, etc.","For custom warnings, ensure the class inherits from Warning","Leave the category field empty to match all warnings (defaults to Warning)"],"tags":["pytest","config","warnings","filterwarnings","category"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}