No error for singledispatch on generic types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Please consider
from functools import singledispatch
import typing as tp
@singledispatch
def f(x: tp.Any) -> None: pass
@f.register
def _f_list(x: list[int]) -> None: pass
if __name__ == '__main__':
f([1, 2, 3])
Then
$ mypy sp.py
Success: no issues found in 1 source file
# on import no error as well, so Mypy raising an error would be very useful
$ python -c 'import sp'
On runtime
$ python sp.py
Traceback (most recent call last):
File "/usr/lib/python3.10/functools.py", line 831, in dispatch
impl = dispatch_cache[cls]
File "/usr/lib/python3.10/weakref.py", line 416, in __getitem__
return self.data[ref(key)]
KeyError: <weakref at 0x7f390d605c60; to 'type' at 0x7f390e07bd00 (list)>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.10/functools.py", line 834, in dispatch
impl = registry[cls]
KeyError: <class 'list'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/wrobell/projects/rbfly/sp.py", line 11, in <module>
f([1, 2, 3])
File "/usr/lib/python3.10/functools.py", line 878, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "/usr/lib/python3.10/functools.py", line 836, in dispatch
impl = _find_impl(cls, registry)
File "/usr/lib/python3.10/functools.py", line 783, in _find_impl
mro = _compose_mro(cls, registry.keys())
File "/usr/lib/python3.10/functools.py", line 744, in _compose_mro
types = [n for n in types if is_related(n)]
File "/usr/lib/python3.10/functools.py", line 744, in <listcomp>
types = [n for n in types if is_related(n)]
File "/usr/lib/python3.10/functools.py", line 743, in is_related
and issubclass(cls, typ))
TypeError: issubclass() argument 2 cannot be a parameterized generic
Some context (in general we need to be better at distinguishing between Python types and annotation types)
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.
Direzione di ricerca
Riproduci l’esempio in sp.py con mypy, quindi confronta il risultato statico con il fallimento a runtime di Python in functools singledispatch. Traccia il modo in cui mypy gestisce l’annotazione @f.register per list[int] e identifica il relativo punto di ingresso del controllo dei tipi. Il lavoro è completato quando l’esempio produce un errore utile di mypy invece di segnalare il successo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100