From 86e613112baaf668288e744b2489628cd340827d Mon Sep 17 00:00:00 2001 From: Seth Morton Date: Mon, 23 Dec 2024 13:29:55 -0800 Subject: [PATCH] Fix type hinting for .joinpath Type hinting was taking its cue from the multimethod decorator, so joinpath was always returning Any as the type. To resolve, the multimethod decorator has been annotated with generics so that the typing system can correctly infer the return type of joinpath. --- path/classes.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/path/classes.py b/path/classes.py index 2914996..11dba45 100644 --- a/path/classes.py +++ b/path/classes.py @@ -1,5 +1,5 @@ import functools -from typing import Any, Callable +from typing import Any, Callable, Generic, TypeVar class ClassProperty(property): @@ -8,18 +8,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.