Incorrectly narrows `type` from signature annotation to `Never`
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
When taking the parameter type from the signature, mypy incorrectly thinks that the annotation of type <class 'type'> is the actual type of the variable.
To Reproduce
In the following example, process_signature extracts the type of input from the signature of a function and then tries to match it to int or float. However the type of input_type is <class 'type'> not int or float. Therefore, when the code is run assert_never is reached.
from typing import Callable, assert_never, reveal_type
from inspect import signature
def takes_int(x: int) -> None:
print(f"{x} is a int")
def process_signature(
func: Callable[[int], None] | Callable[[float], None]
) -> None:
input_type = signature(func).parameters['x'].annotation
reveal_type(input_type) # Runtime type is 'type' => should reach `case _:`
match input_type:
case int():
print("This is an int")
case float():
print("This is a float")
case _:
assert_never(input_type) # this is reached during runtime
process_signature(takes_int)
Expected Behavior
mypy to complain that input to assert_never is <class 'type'> but expected Never.
Actual Behavior
mypy does not complain.
Your Environment
- Mypy version used:
1.19.1 - Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used:
3.13.7
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 eseguendo il reproducer fornito con mypy 1.19.1 e ispeziona il tipo rivelato per input_type. Segui come i casi match restringono l’annotazione da inspect.signature fino ad assert_never. Il lavoro è completato quando mypy segnala che l’input_type rimanente è di tipo type, invece di trattare il caso wildcard come Never.
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