Skip to content

Commit

Permalink
Fix type hinting for .joinpath
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SethMMorton committed Dec 23, 2024
1 parent 9c93f57 commit 37d86a2
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions path/classes.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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.
Expand Down

0 comments on commit 37d86a2

Please sign in to comment.