diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index af960391e85d..691558eec3d5 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -124,7 +124,7 @@ class WeakKeyDictionary(MutableMapping[_KT, _VT]): @overload def __ior__(self: Self, other: Iterable[tuple[_KT, _VT]]) -> Self: ... -class finalize: # TODO: This is a good candidate for to be a `Generic[_P, _T]` class +class finalize: def __init__(self, __obj: object, __func: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs) -> None: ... def __call__(self, _: Any = ...) -> Any | None: ... def detach(self) -> tuple[Any, Any, tuple[Any, ...], dict[str, Any]] | None: ... diff --git a/test_cases/stdlib/test_weakref.py b/test_cases/stdlib/test_weakref.py new file mode 100644 index 000000000000..e54dc24a29de --- /dev/null +++ b/test_cases/stdlib/test_weakref.py @@ -0,0 +1,21 @@ +from typing import Any, Callable, Dict, Optional, Tuple +from typing_extensions import TypeAlias, assert_type +from weakref import finalize + +_ResultStructure: TypeAlias = Optional[Tuple[str, Callable[[int, int], int], Tuple[Any, ...], Dict[str, Any]]] + + +class TObj: + """_T type var in stubs""" + + pass + + +def callback(x: int, y: int) -> int: + return x + y + + +final = finalize(TObj, callback, 1, y=2) +assert_type(final.peek(), _ResultStructure) +assert_type(final.detach(), _ResultStructure) +assert_type(final(), int | None)