dry-python / dry-python/classes

Prevent an implementation for `int` class from operating on `bool` values

Aberta
#310 1 comentário 0 reações 0 responsáveis Ver no GitHub
Linguagem predominante
Python
Estrelas
730
Forks
30
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

**Context.** Consider the following piece of code.

```python
from classes import typeclass

@typeclass
def render(data_value) -> str:
"""Pretty-print a value."""

@render.instance(int)
def _render_int(data_value: int) -> str:
return f'🔢 {data_value}'

render(True) == '🔢 True'
```
*(this should be runnable as-is on `classes 0.4.0`.)*

I would have expected this code to fail with a `NotImplementedError` because the `bool` case wasn't specified; but instead, that case is handled by the `int` implementation because

```pycon
In [3]: issubclass(bool, int)
Out[3]: True
```

**Decision.** In an `int` implementation, recognize if the provided value is actually a `bool` and refuse to process that value.

**Consequences.** I have been writing in Python for quite a few years now and I might even have encountered this relationship between `int` and `bool` types before, but that is not quite a type of thing that I keep in my short-term memory to be able to instantly recognize.

I believe this change can help avert bugs which might be hard to trace otherwise.

Guia de contribuição

Abrir o guia de contribuição

Direção de pesquisa

Comece reproduzindo o exemplo com classes 0.4.0 e, em seguida, rastreie o dispatch a partir da declaração da typeclass e do registro de render.instance(int). Está concluído quando render(True) não usar mais a implementação de int e, em vez disso, gerar NotImplementedError para o caso bool não especificado.

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

Avaliação

Stack de tecnologia
python
Domínio
backend
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
45/100

Receba novas issues na sua caixa de entrada

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