python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.26k stars 2.79k forks source link

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

Open Avasam opened 8 months ago

Avasam commented 8 months ago

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

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

Avasam commented 8 months ago

As for the real world scenario where I encountered this: https://github.com/python/typeshed/pull/11336/files#diff-88b4b1d81d8a23e6e0f612acfb230bb8551893c0a56fed353784b2e744930430R48-R59

erictraut commented 8 months ago

As I mentioned in the pyright issue tracker, I don't think this is a valid use of the @overload decorator. I therefore rejected this as a bug. If we want to allow @overload to be used on callable objects — especially when those objects have overloaded signatures for __call__, then I think we'd need to extend the typing spec to accommodate this use case. As the typing spec is currently written, I wouldn't expect this to work.