python / python/cpython

Add unittest.TestCase.assertMappingEqual

Offen
#121,104 0 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne beim Einstiegspunkt unittest.TestCase.assertDictEqual und prüfe den verknüpften PR gh-121113. Bestimme, ob eine separate assertMappingEqual-API angemessen ist, und bewahre das bestehende Wörterbuchverhalten; die Aufgabe ist abgeschlossen, wenn der vorgeschlagene Mapping-Vergleich mit Abdeckung für Mapping-Eingaben implementiert ist und das bestehende Verhalten intakt bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
testing
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.