[match-case]/[isinstance] narrowing logic against the special `AnyCallable` Protocol differs from `callable()` check
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
According to the typing spec (https://typing.python.org/en/latest/spec/callables.html#meaning-of-in-callable),
@runtime_checkable
class AnyCallable(Protocol):
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
is an implementation of Callable[..., Any]. Therefore, its isinstance-narrowing and match-case class pattern narrowing should behave the same as a simple if callable() narrowing. However, this is not the case: both isinstance and match-case produce different results:
from typing import Any, Callable
from typing_extensions import Protocol, runtime_checkable, assert_type
@runtime_checkable
class AnyCallable(Protocol):
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
def check_proto(p: AnyCallable) -> None:
assert_type(p, Callable[..., Any])
class FnImpl:
def __call__(self, x: object, /) -> int: ...
# Test Baseline
def test_iscallable_object(x: object) -> None:
if callable(x):
reveal_type(x) # N: Revealed type is "__main__.<callable subtype of object>"
def test_iscallable_impl(x: FnImpl) -> None:
if callable(x):
reveal_type(x) # N: Revealed type is "__main__.FnImpl"
def test_iscallable_callable(x: Callable[[object], int]) -> None:
if callable(x):
reveal_type(x) # N: Revealed type is "def (builtins.object) -> builtins.int"
# Test isinstance
def test_isinstance_object(x: object) -> None:
if isinstance(x, AnyCallable):
reveal_type(x) # N: Revealed type is "__main__.AnyCallable"
def test_isinstance_impl(x: FnImpl) -> None:
if isinstance(x, AnyCallable):
reveal_type(x) # N: Revealed type is "__main__.FnImpl"
def test_isinstance_callable(x: Callable[[object], int]) -> None:
if isinstance(x, AnyCallable):
reveal_type(x) # N: Revealed type is "__main__.AnyCallable"
# test match-case
def test_match_object(x: object) -> None:
match x:
case AnyCallable() as fn:
reveal_type(fn) # N: Revealed type is "__main__.AnyCallable"
def test_match_impl(x: FnImpl) -> None:
match x:
case AnyCallable() as fn:
reveal_type(fn) # N: Revealed type is "__main__.AnyCallable"
def test_match_callable(x: Callable[[object], int]) -> None:
match x:
case AnyCallable() as fn:
reveal_type(fn) # N: Revealed type is "__main__.AnyCallable"
https://mypy-play.net/?mypy=latest&python=3.12&gist=5cb05553fa6674d445906eda99802e90
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 la riproduzione nell’esempio mypy-play collegato e confronta callable(), isinstance(x, AnyCallable) e il narrowing in match-case. Traccia il comportamento del narrowing per gli input di tipo object, FnImpl e Callable; il lavoro è completato quando i controlli basati sul protocollo producono lo stesso narrowing effettivo di callable() quando la specifica di typing lo richiede.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100