mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-18 18:05:57 +08:00
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:
20
third_party/2and3/singledispatch.pyi
vendored
20
third_party/2and3/singledispatch.pyi
vendored
@@ -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]: ...
|
||||
|
||||
Reference in New Issue
Block a user