mypy does not narrow between Generic type[_T] and Callable[[Any], _T]
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
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
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 riproducendo l’esempio RemoteFunction con mypy --strict, concentrandoti su _validate, sui controlli isinstance e sui risultati di reveal_type. Traccia come vengono ristrette le variabili di tipo generico quando l’unione contiene type[_T] e Callable[[Any], _T]. Il lavoro è completato quando l’esempio restituisce response come _T senza l’errore di ritorno incompatibile segnalato, preservando al contempo l’inferenza di call esterna.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100