basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

decorator that takes a named `Callable` fails when function is decorated with `@staticmethod`

Open DetachHead opened this issue 1 year ago • 2 comments

from __future__ import annotations

from types import FunctionType
from typing import Callable, Protocol


class Named(Protocol):
    __name__: str
    __qualname__: str


class Foo:
    @staticmethod
    def foo(fn: Callable[[str], None] & Named) -> FunctionType[[], None]:
        def wrapped():
            pass

        return wrapped

    @staticmethod
    @foo
    def bar(letter: str): ...


Foo().bar() # works at runtime
main.py: note: In class "Foo":
main.py:20:6: error: Argument 1 to "foo" of "Foo" has incompatible type "(letter: str) -> None"; expected "(str) -> None & Named"  [arg-type]
        @staticmethod
         ^
Found 1 error in 1 file (checked 1 source file)

https://mypy-play.net/?mypy=basedmypy-latest&python=3.12&gist=785d3f5c4dad6bd6a71c4c8520d10655

DetachHead avatar Sep 11 '24 05:09 DetachHead