`WarningsRecorder.pop()` returns the last match instead of the first when warning categories are unrelated
Maintainer antworten meist innerhalb von 1 Tag
@Aditya-XR arbeitet bereits daran.
Seit 24.9.2026.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 35/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- python
- Bereich
- testing-qa
Rechercherichtung
Start at the WarningsRecorder.pop implementation and read TestSubclassWarningPop plus test_recording, especially the existing category-selection loop. Add a regression case using unrelated warning categories and verify that pop returns the first matching warning; run the relevant warning-recorder tests.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
WarningsRecorder.pop() (and pytest.warns(...).pop() / recwarn.pop()) returns the last matching warning instead of the first one when the recorded warnings have categories that are unrelated to each other.
import warnings
def test_pop(recwarn):
warnings.warn("first", UserWarning)
warnings.warn("second", RuntimeWarning)
warnings.warn("third", DeprecationWarning)
assert str(recwarn.pop().message) == "first"
E AssertionError: assert 'third' == 'first'
The docstring says pop returns "the first recorded warning which is an instance of cls, but not an instance of a child class of any other match". None of the three categories is a subclass of another, so the first one should come back. That was also the behaviour before 8.0.
The cause is the "best inexact match" loop added in #11160 (the fix for #10701):
if issubclass(w.category, cls) and (
best_idx is None
or not issubclass(w.category, self._list[best_idx].category)
):
best_idx = i
not issubclass(new, best) is true both when new is more general than best and when the two are unrelated siblings. So every sibling replaces the current best, and the loop ends up on the last one.
The existing tests in TestSubclassWarningPop only use a single parent/child chain, so they don't catch this. test_recording pops a UserWarning and a DeprecationWarning, but both have the message "hello", so it passes whichever one comes back.
Reproduced on main (pytest 9.2.0.dev345+g872117358), Python 3.13.5, Windows 11. The loop was introduced by #11160, so the bug is present since 8.0.0; in 7.4.x pop() returned the first match. I have a fix with regression tests ready and will open a PR.
- Vorherrschende Sprache
- Python
- Sterne
- 14.7k
- Forks
- 3.6k
- Ø Merge
- 2 T. 21 Std.
- Gemergte PRs (30 T.)
- 38
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus pytest-dev/pytest
-
pytest.approx with a list expected value reports overflowing differences for numpy integer arraysEvtl. vergeben @rinopatrick hat das vor 2 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
pytest-dev/pytest#15314 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Logging capture crashes when another thread registers a loggerEvtl. vergeben @Spikhalskiy hat das vor 13 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
pytest-dev/pytest#15237 ·
Maintainer antworten meist innerhalb von 1 Tag
-
`faulthandler_timeout` stops covering a test once setup, call or a subtest fails (`pytest_exception_interact` cancels the timer)Evtl. vergeben @mayur-dhavan hat das vor 9 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
pytest-dev/pytest#15180 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)Evtl. vergeben @hardikkaurani hat das vor 40 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
pytest-dev/pytest#15099 · 7 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`Evtl. wieder frei @siddy-stack hat das vor 77 Tagen übernommen, und es ist kein Pull Request offen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
pytest-dev/pytest#14514 · 6 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in pytest-dev/pytest
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
unionai-oss/pandera#2640 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
enhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
JdeRobot/RoboticsInfrastructure#843 ·
Maintainer antworten meist innerhalb von 1 Tag
-
automated endpoint-dead needs-curation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
almanac-data/climate-almanac#122 ·
Maintainer antworten meist innerhalb von 1 Tag
-
area:harness enhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
evarness-ai/iris-harness#420 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
suitenumerique/find#206 ·
Maintainer antworten meist innerhalb von 2 Tagen