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

Aperta
#310 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia riproducendo l’esempio con classes 0.4.0, quindi traccia il dispatch a partire dalla dichiarazione della typeclass e dalla registrazione di render.instance(int). Il lavoro è completato quando render(True) non usa più l’implementazione di int e solleva invece NotImplementedError per il caso bool non specificato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Context. Consider the following piece of code.

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

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.

Lingua principale
Python
Stelle
730
Fork
30
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dry-python/classes

Tutte le issue di dry-python/classes

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.