python / python/mypy

Passing Callable to higher-order functions expecting Type[Callable] fails

Aperta
#15,593 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Mypy reports that higher-order functions with parameters of type Type[Callable] - or any subscripted variant - can't receive Callable (with compatible subscripts where needed) as arguments.

To Reproduce

Playground link (with more detail and further test cases):
https://mypy-play.net/?mypy=1.4.1&python=3.11&gist=bd6418069e6a7800eb0e2067829a2c90

def test_fn(_: Type[Callable]) -> None:
    """A higher order fn accepting some TYPE, instances of which are callable"""
    pass

test_fn(Callable)  # FAILS
test_fn(Callable[[int], None])  # FAILS

TestCase: TypeAlias = Callable[[int], None]
test_fn(TestCase)  # FAILS

Expected Behavior

From https://docs.python.org/3/library/typing.html#type-of-class-objects

A variable annotated with type[C] (or typing.Type[C]) may accept values that are classes themselves – specifically, it will accept the class object of C.

So, passing Callable to a function expecting a Type[Callable], either directly or via a TypeAlias, should work.

Actual Behavior

When a subscripted Callable expression is passed directly in the function call, mypy reports:

error: Argument 1 to "test_fn" has incompatible type "object"; expected "type[Callable[..., Any]]"  [arg-type]

When bare Callable is passed in-place, or when any Callable expression is passed via a variable, with or without a TypeAlias annotation, this becomes:

error: Argument 1 to "test_fn" has incompatible type "<typing special form>"; expected "type[Callable[..., Any]]"  [arg-type]

Your Environment

  • Mypy version used: Playground 1.4.1 (originally discovered in 1.3.0)
  • Mypy command-line flags: Playground defaults
  • Mypy configuration options from mypy.ini (and other config files): Playground defaults
  • Python version used: 3.11

Other Notes

Pylance/Pyright appears to be fine with this.

The actual use-case I have is as a (generic) factory function which, given the type of some callable, will return an appropriate version of a corresponding generic class, which manages registering and triggering callbacks of the original callable type. The constructed class preserves and forwards the function signature annotations from the callable onto its own methods, via a ParamSpec and return-type TypeVar. The function receives a type, rather than a callable instance, as when defining the callback interface I wouldn't typically have a function of the correct type to hand.

As the playgound link shows, I'm also intending that the factory fn support receiving callable Protocol classes, which while more flexible than collections.abc.Callable (kwargs!) are also much more verbose. Mypy seems happy with these.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo i tre casi che falliscono nel mypy playground collegato, incluse le forme Callable bare, subscripted e aliased. L’issue non indica alcun file sorgente né alcun test; il lavoro è completo quando le espressioni Callable compatibili vengono accettate se passate a parametri Type[Callable], senza gli errori arg-type segnalati.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.