python / python/cpython

Setting many types of values to `category` argument of `warnings.simplefilter()` doesn't get any error

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

まだ誰も着手していません。

3.12 3.13 3.14 stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

Setting Warning to category argument of warnings.filterwarnings() works, then setting the str type value "Hello World" to category argument of warnings.filterwarnings() gets the error message as shown below. *This is an understandable behavior:

import warnings

warnings.resetwarnings()

warnings.filterwarnings(action="ignore", category=Warning)

print(warnings.filters)
# [('ignore', None, <class 'Warning'>, None, 0)]

warnings.filterwarnings(action="ignore", category="Hello World") # Error

print(warnings.filters)

AssertionError: category must be a class

But, setting many types of values to category argument of warnings.simplefilter() doesn't get any error as shown below. *This isn't an understandable behavior:

import warnings

warnings.simplefilter(action="default", category=Warning)         # Understandable
warnings.simplefilter(action="default", category="Hello World") # Not understandable
warnings.simplefilter(action="default", category=100)                 # Not understandable
warnings.simplefilter(action="default", category=3.14)                # Not understandable
warnings.simplefilter(action="default", category=3.14+0.j)          # Not understandable
warnings.simplefilter(action="default", category=True)                # Not understandable

print(warnings.filters)
# [('default', None, True, None, 0),
#  ('default', None, (3.14+0j), None, 0),
#  ('default', None, 100, None, 0),
#  ('default', None, 'Hello World', None, 0),
#  ('default', None, <class 'Warning'>, None, 0)]

So, category argument of warnings.simplefilter() should also return the error message as shown below:

AssertionError: category must be a class

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs
  • gh-136303
  • gh-136305

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

調査の方向性

レポートに示されている warnings.simplefilter() と warnings.filterwarnings() のエントリポイントから始め、それぞれが category 引数をどのように検証するかを比較します。無効な値を使った例を再現し、simplefilter() が filterwarnings() と同じ category の検証エラーを発生させることを確認します。

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

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

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

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