python / python/cpython

Add unittest.TestCase.assertMappingEqual

未關閉
#121,104 0 則留言 3 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-feature
主要語言
Python
星號
77.2k
分支
35.9k
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. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 unittest.TestCase.assertDictEqual 入口開始,檢視連結的 PR gh-121113。判斷是否適合提供獨立的 assertMappingEqual API,並保留現有的字典行為;完成標準是實作所提議的 mapping 比較,涵蓋 Mapping 輸入,同時維持現有行為不變。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
testing
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。