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

Overloading staticmethods #7781

Closed
nautics889 opened this issue Oct 23, 2019 · 9 comments · Fixed by #13482
Closed

Overloading staticmethods #7781

nautics889 opened this issue Oct 23, 2019 · 9 comments · Fixed by #13482
Labels
affects-typeshed Anything that blocks a typeshed change bug mypy got something wrong priority-1-normal topic-descriptors Properties, class vs. instance attributes topic-overloads

Comments

@nautics889
Copy link
Contributor

Overloaded staticmethod isn't considered correctly by the type checker.
foobar.py

from typing import overload

class Snafu(object):
    @overload
    @staticmethod
    def snafu(value: None) -> None: ...

    @overload
    @staticmethod
    def snafu(value: bytes) -> bytes: ...

    @overload
    @staticmethod
    def snafu(value: str) -> bytes: ...

    @staticmethod
    def snafu(value):
        return bytes(value, encoding='UTF-16')

print(Snafu().snafu('123'))

While running mypy foobar.py expected output: Success: no issues found in 1 source file,
but got foobar.py:21: error: No overload variant of "snafu" of "Snafu" matches argument type "str" .
I guessed #5224 had to fix this, but i've got the behavior above.

Am i doing something wrong?

mypy v0.740, Python v3.7.4

@msullivan msullivan added bug mypy got something wrong priority-1-normal labels Oct 23, 2019
@msullivan
Copy link
Collaborator

It looks like it only works right now if you call it directly via the class: Snafu.snauf('123').

@msullivan
Copy link
Collaborator

@Michael0x2a Any chance you are excited to take another look at this thrilling interaction?

@LefterisJP
Copy link
Contributor

I have also ran into this problem. Any chance it gets addressed?

@akeeman
Copy link

akeeman commented Apr 1, 2021

bug stil exists in

mypy 0.812
python 3.8.x

@Jasha10
Copy link

Jasha10 commented Sep 30, 2021

My current workaround is to write an overloaded function (outside of the class body) instead of an overloaded staticmethod.

@sam-hoffman
Copy link

This bug still appears to exist in mypy 0.931, python 3.10x :(

@srittau
Copy link
Contributor

srittau commented Mar 29, 2022

This is also a problem for typeshed: python/typeshed#7543.

@bluebird75
Copy link

Problem still present for mypy 0.961, python 3.10.2

class Toto:

    @overload
    @staticmethod
    def toto(arg: int, arg2: str) -> None: ...

    @overload
    @staticmethod
    def toto(arg: int) -> None: ...

    @overload
    @staticmethod
    def toto() -> None: ...

    @staticmethod
    def toto(arg: int = 0, arg2: str='') -> None:
        pass


Toto.toto(3)
t = Toto()
t.toto(3)
Oucome of mypy:
tests\qapplication.py:35: error: Attribute function "toto" with type "Callable[[], None]" does not accept self argument  [misc]
tests\qapplication.py:35: error: No overload variant of "toto" of "Toto" matches argument type "int"  [call-overload]
tests\qapplication.py:35: note: Possible overload variants:
tests\qapplication.py:35: note:     def toto(arg, arg2: str) -> None
tests\qapplication.py:35: note:     def toto(arg) -> None
tests\qapplication.py:35: note:     def toto() -> None
Found 3 errors in 1 file (checked 1 source file)

Line 35 is the t.toto(3)

@ultrabear
Copy link

Had this issue too (0.961 3.10.5), only crops up on instances of the class, not the class itself

kajarenc added a commit to kajarenc/streamlit that referenced this issue Jul 15, 2022
kajarenc added a commit to streamlit/streamlit that referenced this issue Jul 15, 2022
tconkling added a commit to tconkling/streamlit that referenced this issue Jul 15, 2022
…untime

* tim/SessionClientDisconnectedError:
  better docstring
  typo
  SessionClientDisconnectedError, with test
  WIP
  * Remove staticmethod decorator from __call__ method of SingletonAPI to avoid mypy bug python/mypy#7781 (streamlit#4981)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-typeshed Anything that blocks a typeshed change bug mypy got something wrong priority-1-normal topic-descriptors Properties, class vs. instance attributes topic-overloads
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants