mypy does not narrow between Generic type[_T] and Callable[[Any], _T]
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
MyPy doesn't recognize that isinstance(type_var, type) and isinstance(another_var, type_var) narrows another_var to whatever type_var is.
To Reproduce
class RemoteFunction(Generic[_T]):
def __init__(self, return_type: type[_T] | Callable[[Any], _T]):
self._return_type = return_type
def _validate(self, response: Any) -> _T:
if isinstance(self._return_type, type):
reveal_type(self._return_type)
# Revealed type is "builtins.type" Mypy
# Type of "self._return_type" is "type[object] | type" Pylance
if not isinstance(response, self._return_type):
raise TypeError
reveal_type(response)
# Revealed type is "builtins.object"Mypy
# Type of "response" is "Any" Pylance
return response # Incompatible return value type (got "object", expected "_T")
return self._return_type(response)
def __call__(self) -> _T:
remote_response: Any = None # actually from network/db/etc
return self._validate(remote_response)
# Usage example
my_remote_func = RemoteFunction(int)
response = my_remote_func()
reveal_type(response)
# Type of "response" is "int" Pylance
# Revealed type is "builtins.int" Mypy
Expected Behavior
mypy should correctly type response as _T
Actual Behavior
test333.py: note: In member "_validate" of class "RemoteFunction":
test333.py:20: note: Revealed type is "builtins.type"
test333.py:25: note: Revealed type is "builtins.object"
test333.py:28: error: Incompatible return value type (got "object", expected "_T") [return-value]
return response # Incompatible return value type (got "object", expected "_T")
Interestingly, pyright shows similar types for reveal_type, as annotated above, but does not give an error. Both correctly infer the type of the response in the outer scope, presumably because of the __call__ return type annotation.
Your Environment
- Mypy version used: 1.19.0
- Mypy command-line flags: --strict
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.11.4
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 damit, das RemoteFunction-Beispiel mit mypy --strict nachzustellen, wobei du dich auf _validate, die isinstance-Prüfungen und die Ergebnisse von reveal_type konzentrierst. Verfolge, wie generische Typvariablen eingeengt werden, wenn die Union type[_T] und Callable[[Any], _T] enthält. Als erledigt gilt die Aufgabe, wenn das Beispiel response als _T zurückgibt, ohne den gemeldeten Fehler wegen einer inkompatiblen Rückgabe, und dabei die Inferenz von call auf der äußeren Ebene erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100