Skip to content

(🐞) staticmethod special-casing causes error to not be reported #16161

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

Open
KotlinIsland opened this issue Sep 22, 2023 · 0 comments
Open
Labels
bug mypy got something wrong

Comments

@KotlinIsland
Copy link
Contributor

If we copy the implementation of staticmethod from typeshed, mypy correctly reports the error here.

So it seems that mypys dedicated special-casing of staticmethod is causing this issue to be incorrectly not reported.

from collections.abc import Callable
from typing import Generic, ParamSpec, TypeVar, overload

P = ParamSpec("P")
T = TypeVar("T")
R_co = TypeVar("R_co", covariant=True)


def contextmanager(fn: Callable[P, R_co]) -> Callable[P, R_co]:
    def func(*args: P.args, **kwargs: P.kwargs) -> R_co:
        return fn(*args, **kwargs)
    return func


class staticmethod_copy(Generic[P, R_co]):
    @property
    def __func__(self) -> Callable[P, R_co]: ...
    @property
    def __isabstractmethod__(self) -> bool: ...
    def __init__(self, __f: Callable[P, R_co]) -> None: ...
    @overload
    def __get__(self, __instance: None, __owner: type) -> Callable[P, R_co]: ...
    @overload
    def __get__(self, __instance: T, __owner: type[T] | None = None) -> Callable[P, R_co]: ...
    __name__: str
    __qualname__: str
    @property
    def __wrapped__(self) -> Callable[P, R_co]: ...
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R_co: ...

# Just the relevant part of staticmethod definition that is needed
#class staticmethod(Generic[P, R_co]):
#    def __init__(self, __f: Callable[P, R_co]) -> None: ...
#    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R_co: ...

class Foo:
    @contextmanager
    @staticmethod_copy
    def copy() -> None: ...

    @contextmanager
    @staticmethod
    def native() -> None: ...


Foo().copy()   # ✅ error: Attribute function "copy" with type "Callable[[], None]" does not accept self argument  [misc]
Foo().native()  # ❌no error
@KotlinIsland KotlinIsland added the bug mypy got something wrong label Sep 22, 2023
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
@KotlinIsland and others