diff --git a/path/classes.py b/path/classes.py index 2914996..7aeb7f2 100644 --- a/path/classes.py +++ b/path/classes.py @@ -1,5 +1,7 @@ +from __future__ import annotations + import functools -from typing import Any, Callable +from typing import Any, Callable, Generic, TypeVar class ClassProperty(property): @@ -8,18 +10,21 @@ def __get__(self, cls: Any, owner: type | None = None) -> Any: return self.fget.__get__(None, owner)() -class multimethod: +_T = TypeVar("_T") + + +class multimethod(Generic[_T]): """ Acts like a classmethod when invoked from the class and like an instancemethod when invoked from the instance. """ - func: Callable[..., Any] + func: Callable[..., _T] - def __init__(self, func: Callable[..., Any]): + def __init__(self, func: Callable[..., _T]): self.func = func - def __get__(self, instance: Any | None, owner: type | None) -> Any: + def __get__(self, instance: _T | None, owner: type[_T] | None) -> Callable[..., _T]: """ If called on an instance, pass the instance as the first argument.