{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/recwarn.py#L210-L246","documentation":"WarningsRecorder.pop raises AssertionError when no recorded warning matches the requested class (or a subclass). It searches the recorded list and fails if nothing matches.","triggerScenarios":"Calling recwarn.pop(RuntimeWarning) when no RuntimeWarning (or subclass) was emitted during the test. Hits recwarn.py:228.","commonSituations":"The code under test didn't emit the expected warning; pop is called before the warning-emitting code ran; the warning category differs from what was expected.","solutions":["Ensure the code under test actually emits a warning of the requested category.","Check recwarn.list (or iterate) before popping to confirm a matching warning exists.","Prefer pytest.warns(ExpectedCategory) context manager over manual pop for clearer failure messages.","Verify the warning category — pop matches by exact class first, then by most-specific subclass."],"exampleFix":"// before\nrecwarn.pop(RuntimeWarning)  # may raise\n// after\nmatches = [w for w in recwarn.list if issubclass(w.category, RuntimeWarning)]\nassert matches, 'no RuntimeWarning emitted'\nrecwarn.pop(RuntimeWarning)","handlingStrategy":"try-catch","validationCode":"matches = [w for w in recwarn.list if issubclass(w.category, cls)]\nif not matches:\n    pytest.fail(f'no {cls.__name__} warning was recorded')\nrecwarn.pop(cls)","typeGuard":null,"tryCatchPattern":"try:\n    recwarn.pop(RuntimeWarning)\nexcept AssertionError:\n    pytest.fail('expected a RuntimeWarning that was not emitted')","preventionTips":["Prefer pytest.warns(Category) over manual recwarn.pop for clearer failures.","Inspect recwarn.list before popping to confirm the category exists.","Ensure the warning-emitting code runs before you call pop."],"tags":["pytest","warns","assertion"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}