Prevent an implementation for `int` class from operating on `bool` values
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 45/100
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di dry-python/classes
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
dry-python/classes#496 ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 45/100
dry-python/classes#494 ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 52/100
dry-python/classes#492 · 2 commenti ·
-
bug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
dry-python/classes#481 · 5 commenti · 4 reazioni ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 35/100
dry-python/classes#386 ·
Tutte le issue di dry-python/classes
Issue simili
-
Add: hunch Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
DiamondLightSource/dodal#2211 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
openml/openml-python#1749 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
sipyourdrink-ltd/bernstein#6191 ·