`callable()` fails to narrow type to `Callable`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem t.py-Reproduzierer unter mypy 1.19.1 und --strict, lies dann die im Bericht verlinkte Dokumentation zu Ausdrücken zur Typenverengung und konzentriere dich dabei auf callable(obj). Verfolge, wie die callable-Assertion behandelt wird, und überprüfe, dass das Beispiel beim Zurückgeben von Callable[[], bool] nicht mehr no-any-return meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 48/100