{"record":{"id":"b90cdefc6a9095d8","repo":"pytest-dev/pytest","slug":"cls-r-not-found-in-warning-list","errorCode":null,"errorMessage":"{cls!r} not found in warning list","messagePattern":"(.+?) not found in warning list","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"src/_pytest/recwarn.py","lineNumber":228,"sourceCode":"\n    def pop(self, cls: type[Warning] = Warning) -> warnings.WarningMessage:\n        \"\"\"Pop the first recorded warning which is an instance of ``cls``,\n        but not an instance of a child class of any other match.\n        Raises ``AssertionError`` if there is no match.\n        \"\"\"\n        best_idx: int | None = None\n        for i, w in enumerate(self._list):\n            if w.category == cls:\n                return self._list.pop(i)  # exact match, stop looking\n            if issubclass(w.category, cls) and (\n                best_idx is None\n                or not issubclass(w.category, self._list[best_idx].category)\n            ):\n                best_idx = i\n        if best_idx is not None:\n            return self._list.pop(best_idx)\n        __tracebackhide__ = True\n        raise AssertionError(f\"{cls!r} not found in warning list\")\n\n    def clear(self) -> None:\n        \"\"\"Clear the list of recorded warnings.\"\"\"\n        self._list[:] = []\n\n    # Type ignored because we basically want the `catch_warnings` generic type\n    # parameter to be ourselves but that is not possible(?).\n    def __enter__(self) -> Self:  # type: ignore[override]\n        if self._entered:\n            __tracebackhide__ = True\n            raise RuntimeError(f\"Cannot enter {self!r} twice\")\n        _list = super().__enter__()\n        # record=True means it's None.\n        assert _list is not None\n        self._list = _list\n        warnings.simplefilter(\"always\")\n        return self\n","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/recwarn.py#L210-L246","documentation":"Raised by WarningsRecorder.pop when no recorded warning matches the requested category cls. pop searches for an exact category match first, then the most-derived subclass match; if neither is found it raises AssertionError. This signals a test expectation failure: the user asked for a warning of that category that was never emitted.","triggerScenarios":"Calling recorder.pop(UserWarning) or pop(DeprecationWarning) when the recorded list contains no warning of that category. Reached when best_idx stays None after the loop.","commonSituations":"Inspecting a WarningsRecorder/pytest.warns result and popping a category the code under test did not emit; ordering bugs where pop is called twice for the same warning; mismatched category assumptions after a refactor.","solutions":["Check membership before popping: if any(w.category is cls for w in recorder): recorder.pop(cls).","Use pytest.warns(ExpectedCategory) at the assertion level rather than manual pop, so pytest reports DID NOT WARN with context.","Inspect recorder.list to see what was actually emitted and correct the expected category."],"exampleFix":"// before\nwarn = recorder.pop(DeprecationWarning)  # AssertionError if absent\n// after\nmatches = [w for w in recorder if issubclass(w.category, DeprecationWarning)]\nassert matches, f'no DeprecationWarning; got {[w.category.__name__ for w in recorder]}'\nwarn = matches[0]","handlingStrategy":"try-catch","validationCode":"matches = [w for w in recorder if issubclass(w.category, cls)]\nassert matches, f'{cls!r} not in {[w.category.__name__ for w in recorder]}'\nwarn = matches[0]","typeGuard":"def recorder_has_category(recorder, cls) -> bool:\n    return any(issubclass(w.category, cls) for w in recorder)","tryCatchPattern":"try:\n    warn = recorder.pop(cls)\nexcept AssertionError:\n    # handle missing-warning expectation explicitly\n    raise AssertionError(f'expected {cls}; got {[w.category.__name__ for w in recorder]}')","preventionTips":["Check recorder membership before pop.","Prefer pytest.warns(ExpectedCategory) at the assertion boundary over manual pop.","Inspect recorder.list to confirm categories before popping."],"tags":["pytest","warns","warningsrecorder","pop","assertion"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}