Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion check_linter_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def run_linter(linter: str) -> str:
str: `stdout`.
"""
p = subprocess.Popen(
[linter, source_dir],
[sys.executable, "-m", linter, source_dir],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
Expand Down
18 changes: 16 additions & 2 deletions plum/dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, Optional, Tuple, TypeVar, Union
from typing import Any, Callable, Dict, Optional, Tuple, TypeVar, Union, overload

from .function import Function
from .overload import get_overloads
Expand All @@ -23,7 +23,21 @@ def __init__(self):
self.functions: Dict[str, Function] = {}
self.classes: Dict[str, Dict[str, Function]] = {}

def __call__(self, method: Optional[T] = None, precedence: int = 0) -> T:
@overload
def __call__(self, method: Callable[..., Any], precedence: int = 0) -> Function:
...

@overload
def __call__(
self, method: None, precedence: int = 0
) -> Callable[[Callable[..., Any]], Function]:
...

def __call__(
self,
method: Optional[Callable[..., Any]] = None,
precedence: int = 0,
) -> Union[Function, Callable[[Callable[..., Any]], Function]]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabrieldemarmiesse I wonder if this is the more accurate way to type what @dispatch does. What do you think of something like the following?

from typing import TypeVar, Protocol

F = TypeVar("F")

class FunctionProtocol(Function, Protocol[F]):
    __call__: F


    ...

    @overload
    def __call__(self, method: None, precedence: int = 0) -> Union[
        Callable[[F], FunctionProtocol[F]],
        Callable[[F, int], FunctionProtocol[F]],
    ]:
        ...

    def __call__(self, method: F, precedence: int = 0) -> FunctionProtocol[F]:
        ...

I'm not sure if that actually works, but it might better capture the spirit of @dispatch: retain what the function does originally, but add Function-functionality to it.

"""Decorator to register for a particular signature.

Args:
Expand Down
Loading