Proper singledispatch type definitions

Correctly detects calls to `register()` with a function of incompatible return
type.  Correctly recognizes the `register()`, `dispatch()`, and
`_clear_cache()` methods on a generic function, as well as the `registry`
mapping.

Possible future improvements: it would be amazing if `register()` checked if
the first argument of the registered callable is indeed of valid type. This
would require Callable[] to support varargs.  It would also be great if we
could read the arguments of the remaining arguments during `@singledispatch()`
and cross-check them during `register()` with the currently registered
implementation. Again, this would require Callable[] to become much more
advanced.
This commit is contained in:
Lukasz Langa
2016-12-19 12:54:08 -08:00
parent beb9183103
commit 2058ae30ab
2 changed files with 32 additions and 6 deletions

View File

@@ -1,5 +1,17 @@
# Stubs for singledispatch (Python 3.5)
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.
from typing import Any, Callable, Generic, Mapping, Optional, TypeVar, overload
def singledispatch(func): ...
_T = TypeVar("_T")
class _SingleDispatchCallable(Generic[_T]):
registry = ... # type: Mapping[Any, Callable[..., _T]]
def dispatch(self, cls: Any) -> Callable[..., _T]: ...
@overload
def register(self, cls: Any) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ...
@overload
def register(self, cls: Any, func: Callable[..., _T]) -> Callable[..., _T]: ...
def _clear_cache(self) -> None: ...
def __call__(self, *args: Any, **kwargs: Any) -> _T: ...
def singledispatch(func: Callable[..., _T]) -> _SingleDispatchCallable[_T]: ...