python / python/mypy

overloaded class-decorator is seen as not Callable when used on an overloaded method

Aperta
#16,840 2 commenti 0 reazioni 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
overloaded class-decorator is seen as not Callable when used on an overloaded method

(A clear and concise description of what the bug is.)

To Reproduce

from collections.abc import Callable
from typing import Any, Generic, TypeVar, overload
from typing_extensions import ParamSpec, Self, reveal_type

_P = ParamSpec("_P")
_R = TypeVar("_R")

class DecoratorClass(Generic[_P, _R]):
    def __new__(cls, func: Callable[_P, _R] | None = None) -> Self: ...
    @overload
    def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
    @overload
    def __call__(self, *args: Any, backend: str, **backend_kwargs: Any) -> _R: ...

@overload
@DecoratorClass
def overload_first(foo: str) -> None: ...
@overload
@DecoratorClass
def overload_first(foo: int) -> None: ...

@DecoratorClass
@overload
def overload_second(foo: str) -> None: ...
@DecoratorClass
@overload
def overload_second(foo: int) -> None: ...

_ = reveal_type(overload_first)
_ = reveal_type(overload_second)

Expected Behavior

In the short term: Maybe a better error message? It is not true that DecoratorClass is not callable here.
In the long term: overload-expansion ? Is that even feasible?

Actual Behavior

stubs\networkx\networkx\__init__.pyi:15: error: "DecoratorClass[[str], None]" not callable  [misc]
stubs\networkx\networkx\__init__.pyi:18: error: "DecoratorClass[[int], None]" not callable  [misc]
stubs\networkx\networkx\__init__.pyi:23: error: "DecoratorClass[[str], None]" not callable  [misc]
stubs\networkx\networkx\__init__.pyi:26: error: "DecoratorClass[[int], None]" not callable  [misc]
stubs\networkx\networkx\__init__.pyi:30: error: Cannot determine type of "overload_first"  [has-type]
stubs\networkx\networkx\__init__.pyi:30: note: Revealed type is "Any"
stubs\networkx\networkx\__init__.pyi:31: error: Cannot determine type of "overload_second"  [has-type]
stubs\networkx\networkx\__init__.pyi:31: note: Revealed type is "Any"

Your Environment

  • Mypy version used: mypy 1.8.0 (compiled: yes)
  • Mypy command-line flags: --platform win32 --python-version 3.8
  • Mypy configuration options from mypy.ini (and other config files): From typeshed
  • Python version used: Python 3.9.13

Additional information
For comparison, pyright currently accepts the above and sees the type as such: (only the last method overload is kept, with the class overload)

Running Pyright (base configs) for Python 3.8...
9.6.2
Running: C:\Program Files\nodejs\npx.CMD pyright@1.1.342 stubs/networkx --pythonversion 3.8
e:\Users\Avasam\Documents\Git\typeshed\stubs\networkx\networkx\__init__.pyi
  e:\Users\Avasam\Documents\Git\typeshed\stubs\networkx\networkx\__init__.pyi:30:17 - information: Type of "overload_first" is "DecoratorClass[(foo: int), None]"
  e:\Users\Avasam\Documents\Git\typeshed\stubs\networkx\networkx\__init__.pyi:31:17 - information: Type of "overload_second" is "DecoratorClass[(foo: int), None]"

The Pylance language server shows the following overloads
image
image

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 con la riproduzione in stubs/networkx/networkx/init.pyi ed esegui mypy usando le impostazioni indicate Python 3.8 e win32. Traccia come vengono espansi i decoratori sovraccarichi e i metodi sovraccarichi e come viene verificata la loro invocabilità. La modifica completata dovrebbe evitare gli errori errati not-callable e cannot-determine-type, preservando al contempo i tipi rivelati utili, ma il report non definisce un comportamento finale per i sovraccarichi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.