python / python/cpython

Add unittest.TestCase.assertMappingEqual

Aberta
#121,104 0 comentários 3 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib type-feature
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece pelo ponto de entrada unittest.TestCase.assertDictEqual e analise o PR vinculado gh-121113. Determine se uma API assertMappingEqual separada é apropriada e preserve o comportamento existente dos dicionários; considera-se concluído quando a comparação de mappings proposta estiver implementada com cobertura para entradas Mapping e o comportamento existente permanecer intacto.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
testing
Tipo de issue
Funcionalidade
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
25/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.