Skip to content

Support binding of method that has no self or cls but a ParamSpec-based signature #5267

@erictraut

Description

@erictraut

See this discussion for details.

from typing import Callable, Generic, ParamSpec, TypeVar

_P = ParamSpec("_P")
_T = TypeVar("_T")

class _lru_cache_wrapper(Generic[_P, _T]):
    def __call__(*args: _P.args, **kwargs: _P.kwargs) -> _T:
        ...

def cache(__user_function: Callable[_P, _T]) -> _lru_cache_wrapper[_P, _T]:
    ...

class Foo:
    def not_cached(self) -> int:
        return 0

    @cache
    def cached(self, x: str, y: int) -> int:
        return 0

foo = Foo()
foo.not_cached()

foo.cached("hello", 42)

# Missing argument [20]: Call `_lru_cache_wrapper.__call__` expects argument `x`.
foo.cached()
# Incompatible parameter type [6]: In call `_lru_cache_wrapper.__call__`, for 1st positional only parameter expected `str` but got `int`.
foo.cached(1, 2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions