Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

`WarningsRecorder.pop()` returns the last match instead of the first when warning categories are unrelated

オープン
#15,097 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Aditya-XR がすでに取り組んでいます。

2026年9月24日 から。

  • #15098 @Aditya-XR による — オープン
  • #15105 @chen-maker999 による — マージされずにクローズ
  • #15119 @Tejas5405 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
testing-qa

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
14.6k
フォーク
3.4k
平均マージ
2日 8時間
マージ済み PR(30日)
30

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pytest-dev/pytest のほかの issue

pytest-dev/pytest の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。