`callable()` fails to narrow type to `Callable`
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
Bug Report
I'm sorry if this is a duplicate; it's possible it's covered by one of the existing issues about type narrowing, but I couldn't find anything that explicitly covers this case.
https://mypy.readthedocs.io/en/stable/type_narrowing.html#type-narrowing-expressions explicitly says that callable(obj) will narrow obj to a callable type. However, in my code this doesn't seem to work; the argument to callable is still left as Any.
To Reproduce
With mypy 1.19.1 and --strict:
from collections.abc import Callable
class A:
def get_callback(self, name: str) -> Callable[[], bool]:
callback = getattr(self, name)
assert callable(callback)
return callback
Also demonstrated in this playground.
Actual Behavior
t.py:7: error: Returning Any from function declared to return "Callable[[], bool]" [no-any-return]
Your Environment
- Mypy version used: 1.19.1
- Mypy command-line flags:
--strict - Python version used: 3.13
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
Inizia con il riproduttore t.py usando mypy 1.19.1 e --strict, quindi leggi la documentazione sulle espressioni di restringimento dei tipi collegata nel report, concentrandoti su callable(obj). Traccia come viene gestita l’asserzione callable e verifica che l’esempio non segnali più no-any-return quando restituisce Callable[[], bool].
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 48/100