Unable to use `logging.NOTSET` with `assertLogs` or `assertNoLogs`
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
Since logging.NOTSET equals 0 (a falsey value), the naive conditional check in _AssertLogsContext prevents it from being used as a valid level parameter with unittest.TestCase.assertLogs() or unittest.TestCase.assertNoLogs().
Example:
import logging
import unittest
class LoggerTestCase(unittest.TestCase):
def test_logging(self):
root_logger = logging.getLogger()
root_logger.setLevel(logging.ERROR)
logger = logging.getLogger("foo")
with self.assertLogs(logger=logger, level=logging.NOTSET) as cm:
logger.info("first message")
logger.error("second message")
# This test currently fails because `assertLogs(...)` sees the falsey
# `logging.NOTSET` and sets the level to `logging.INFO` by default; so we
# end up seeing the "INFO:foo:first message" in the output.
self.assertEqual(cm.output, ["ERROR:foo:second message"])
In my real life use case, the logging.NOTSET interaction is useful in context of creating a custom class ExplicitlyConfiguredLogger(logging.Logger) that only logs when the logger is configured with something other than logging.NOTSET (used in https://github.com/element-hq/synapse/pull/18474) and wanting to write some tests for it.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
- gh-137555
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Lib/unittest/_log.py の、issue で参照されている _AssertLogsContext の条件分岐から始めます。assertLogs と assertNoLogs を通じて logging.NOTSET の例を再現し、その後、両方のコンテキストが INFO をデフォルトにせずにレベルを受け入れること、および回帰テストのカバレッジがこの動作を検証していることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- testing
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 25/100