Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong overload signatures with @overload and @staticmethod #12566

Closed
aaron-skydio opened this issue Apr 12, 2022 · 3 comments
Closed

Wrong overload signatures with @overload and @staticmethod #12566

aaron-skydio opened this issue Apr 12, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@aaron-skydio
Copy link

Bug Report

When given the following file, mypy does not produce the correct signatures for the two overloads of __call__:

from typing import overload, Callable, TypeVar, Optional, Any, Union


class SingletonAPI:
    F = TypeVar("F", bound=Callable[..., Any])

    # Bare decorator usage
    @overload
    @staticmethod
    def __call__(func: F) -> F:
        ...

    # Decorator with arguments
    @overload
    @staticmethod
    def __call__(
        *,
        show_spinner: bool = True,
    ) -> Callable[[F], F]:
        ...

    @staticmethod
    def __call__(
        func: Optional[F] = None,
        *,
        show_spinner: bool = True,
    ) -> Any:
        ...


singleton = SingletonAPI()


@singleton
def f(a: bool) -> str:
    pass


@singleton(show_spinner=True)
def g(a: bool) -> str:
    pass

To Reproduce

Run mypy on the above file. We've tested on mypy 0.942 and mypy-0.950+dev.74df7fb1172993af3b2456765fb8368481ef0ab9

Expected Behavior

As best we can tell, the above usage is correct, and we expect mypy to find no errors in this code.

Actual Behavior

Mypy emits the following errors on this code:

fails.py:34: error: No overload variant of "__call__" of "SingletonAPI" matches argument type "Callable[[bool], str]"
fails.py:34: note: Possible overload variants:
fails.py:34: note:     def __call__(func) -> SingletonAPI
fails.py:34: note:     def __call__(show_spinner) -> Callable[[F], F]
fails.py:39: error: No overload variant of "__call__" of "SingletonAPI" matches argument type "bool"
fails.py:39: note: Possible overload variants:
fails.py:39: note:     def __call__(func) -> SingletonAPI
fails.py:39: note:     def __call__(show_spinner) -> Callable[[F], F]
Found 2 errors in 1 file (checked 1 source file)

If we apply the following fix to the above file to make __call__ an instance method instead of static, it passes:

--- fails.py    2022-04-11 23:37:13.397162142 -0700
+++ passes.py   2022-04-11 23:37:29.197661252 -0700
@@ -6,21 +6,23 @@
 
     # Bare decorator usage
     @overload
-    @staticmethod
-    def __call__(func: F) -> F:
+    # @staticmethod
+    def __call__(self, func: F) -> F:
         ...
 
     # Decorator with arguments
     @overload
-    @staticmethod
+    # @staticmethod
     def __call__(
+        self,
         *,
         show_spinner: bool = True,
     ) -> Callable[[F], F]:
         ...
 
-    @staticmethod
+    # @staticmethod
     def __call__(
+        self,
         func: Optional[F] = None,
         *,
         show_spinner: bool = True,

Your Environment

  • Mypy version used: 0.942, and mypy-0.950+dev.74df7fb1172993af3b2456765fb8368481ef0ab9
  • Mypy command-line flags: --python-version=3.6 through 3.10
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8 and 3.10
  • Operating system and version: Ubuntu Linux
@aaron-skydio aaron-skydio added the bug mypy got something wrong label Apr 12, 2022
@AlexWaygood
Copy link
Member

This looks like it might be a duplicate of #7781? :)

@aaron-skydio
Copy link
Author

It certainly does :)
I swear I searched...

@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 12, 2022

No worries! Mypy has a lot of open issues :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants