python / python/mypy

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

Open
#16,840 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in stubs/networkx/networkx/init.pyi and run mypy using the reported Python 3.8 and win32 settings. Trace how overloaded decorators and overloaded methods are expanded and checked for callability. Done should avoid the erroneous not-callable and cannot-determine-type errors while preserving useful revealed types, but the report does not define a final overload behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.