python / python/cpython

Add unittest.TestCase.assertMappingEqual

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

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

stdlib type-feature
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Feature or enhancement

Proposal:

unittest.TestCase has assertDictEqual which requires both arguments to be dict's. Would it make sense to add assertMappingEqual which would weaken this requirement to Mapping?

For example, this would be helpful with immutabledict which is not a dict.

Possible implementation (pseudocode) is to change the existing code:

def assertDictEqual(self, d1, d2, msg=None):
  # [existing] assert d1 and d2 are dict's
  # [existing] check that items are the same  

to

def assertMappingEqual(self, d1, d2, msg=None):
  # [new] assert d1 and d2 are Mapping's
  # [existing] check that items are the same  

def assertDictEqual(self, d1, d2, msg=None):
  # [existing] assert d1 and d2 are dict's
  self.assertMappingEqual(d2, d2, msg)

Considerations and comparison:

  1. Alternatively, the checks in assertDictEqual could be weakened to Mapping, however this might introduce problems into existing tests.

  2. For sequences, there are assertListEqual and assertSequenceEqual. Here assertListEqual simply calls assertSequenceEqual without any type checks.

  3. For sets, there are assertSetEqual and assertCountEqual. Here the implementations are different. assertSetEqual does not check the types of the arguments, but requires them to have the method difference. assertCountEqual only requires the arguments to be iterables.

  4. It is possible to replace assertMappingEqual(a, b) with assertDictEqual({**a}, {**b}), however this is arguably ugly.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-121113

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

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

はじめの一歩

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

調査の方向性

unittest.TestCase.assertDictEqual エントリーポイントから開始し、リンクされている PR gh-121113 を確認します。独立した assertMappingEqual API が適切かどうかを判断し、既存の辞書の動作を維持します。提案された mapping 比較が Mapping 入力のカバレッジを含めて実装され、既存の動作がそのまま維持されれば完了です。

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

評価

技術スタック
python
領域
testing
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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