`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. 4 Std.
- Gemergte PRs (30 T.)
- 34
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 9.2.0 regression on Python 3.10.0: NodeId.__str__ raises AttributeErrorEvtl. vergeben @Aathi-27 hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
pytest-dev/pytest#15337 ·
Maintainer antworten meist innerhalb von 1 Tag
-
pytest.approx with a list expected value reports overflowing differences for numpy integer arraysEvtl. vergeben @rinopatrick hat das vor 4 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 15 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 11 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 42 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
Alle Issues in pytest-dev/pytest
Ähnliche Issues
-
community enhancement good first issue
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 77/100
Hochfrequenz/sapgui.mcp#1039 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
didi/helixharness#18 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
dataforgoodfr/democracy-watch#46 · 1 Reaktion ·