From 9f9873711962f16610ef9f04f2710f409656ed74 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 26 Sep 2018 05:23:02 +0200 Subject: [PATCH] weakproxy fixes (#2450) * The callback argument to ref() and proxy() is optional, per documentation * proxy() returns a ProxyType or CallableProxyType Closes #1627 --- stdlib/2and3/_weakref.pyi | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/stdlib/2and3/_weakref.pyi b/stdlib/2and3/_weakref.pyi index 76c97df54..6a527c189 100644 --- a/stdlib/2and3/_weakref.pyi +++ b/stdlib/2and3/_weakref.pyi @@ -1,6 +1,7 @@ import sys -from typing import Any, Callable, Generic, Optional, TypeVar +from typing import Any, Callable, Generic, Optional, TypeVar, overload +_C = TypeVar('_C', bound=Callable[..., Any]) _T = TypeVar('_T') class CallableProxyType(object): # "weakcallableproxy" @@ -12,8 +13,7 @@ class ProxyType(object): # "weakproxy" class ReferenceType(Generic[_T]): if sys.version_info >= (3, 4): __callback__: Callable[[ReferenceType[_T]], Any] - def __init__(self, o: _T, callback: Callable[[ReferenceType[_T]], - Any] = ...) -> None: ... + def __init__(self, o: _T, callback: Optional[Callable[[ReferenceType[_T]], Any]] = ...) -> None: ... def __call__(self) -> Optional[_T]: ... def __hash__(self) -> int: ... @@ -21,4 +21,8 @@ ref = ReferenceType def getweakrefcount(object: Any) -> int: ... def getweakrefs(object: Any) -> int: ... -def proxy(object: _T, callback: Callable[[_T], Any] = ...) -> ref[_T]: ... +@overload +def proxy(object: _C, callback: Optional[Callable[[_C], Any]] = ...) -> CallableProxyType: ... +# Return CallableProxyType if object is callable, ProxyType otherwise +@overload +def proxy(object: _T, callback: Optional[Callable[[_T], Any]] = ...) -> Any: ...