From 1f174c921af7a08b2c7b08cc5e96e73e90d13c7b Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Fri, 29 Jul 2022 10:28:40 -0400 Subject: [PATCH 01/10] fix: TODO in weakref RE: being generic Fix #8437 --- stdlib/weakref.pyi | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index af960391e85d..16d9b0cc8595 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -124,11 +124,11 @@ 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 - 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: ... - def peek(self) -> tuple[Any, Any, tuple[Any, ...], dict[str, Any]] | None: ... +class finalize(Generic[_P, _T1, _T2]): + def __init__(self, __obj: _T1, __func: Callable[_P, _T2], *args: _P.args, **kwargs: _P.kwargs) -> None: ... + def __call__(self, _: object = ...) -> _T2 | None: ... + def detach(self) -> tuple[_T1, Callable[_P, _T2], tuple[Any, ...], dict[str, Any]] | None: ... + def peek(self) -> tuple[_T1, Callable[_P, _T2], tuple[Any, ...], dict[str, Any]] | None: ... @property def alive(self) -> bool: ... atexit: bool From 56043f2a5494a9660096513f918281aa6f233776 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Fri, 29 Jul 2022 11:25:24 -0400 Subject: [PATCH 02/10] refactor: update names of typevars and alphabetize them --- stdlib/weakref.pyi | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index 16d9b0cc8595..f80fb876e710 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -31,13 +31,14 @@ __all__ = [ "finalize", ] +_CallableT = TypeVar("_CallableT", bound=Callable[..., Any]) +_KT = TypeVar("_KT") +_P = ParamSpec("_P") +_R = TypeVar("_R") _T = TypeVar("_T") _T1 = TypeVar("_T1") _T2 = TypeVar("_T2") -_KT = TypeVar("_KT") _VT = TypeVar("_VT") -_CallableT = TypeVar("_CallableT", bound=Callable[..., Any]) -_P = ParamSpec("_P") ProxyTypes: tuple[type[Any], ...] @@ -124,11 +125,11 @@ class WeakKeyDictionary(MutableMapping[_KT, _VT]): @overload def __ior__(self: Self, other: Iterable[tuple[_KT, _VT]]) -> Self: ... -class finalize(Generic[_P, _T1, _T2]): - def __init__(self, __obj: _T1, __func: Callable[_P, _T2], *args: _P.args, **kwargs: _P.kwargs) -> None: ... - def __call__(self, _: object = ...) -> _T2 | None: ... - def detach(self) -> tuple[_T1, Callable[_P, _T2], tuple[Any, ...], dict[str, Any]] | None: ... - def peek(self) -> tuple[_T1, Callable[_P, _T2], tuple[Any, ...], dict[str, Any]] | None: ... +class finalize(Generic[_P, _T, _R]): + def __init__(self, __obj: _T, __func: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> None: ... + def __call__(self, _: object = ...) -> _R | None: ... + def detach(self) -> tuple[_T, Callable[_P, _R], tuple[Any, ...], dict[str, Any]] | None: ... + def peek(self) -> tuple[_T, Callable[_P, _R], tuple[Any, ...], dict[str, Any]] | None: ... @property def alive(self) -> bool: ... atexit: bool From adb905c0a4717b908865e55c5995d9321a3b025e Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Fri, 29 Jul 2022 12:09:34 -0400 Subject: [PATCH 03/10] fix: add test case for weakref.finalize --- test_cases/stdlib/test_weakref.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 test_cases/stdlib/test_weakref.py diff --git a/test_cases/stdlib/test_weakref.py b/test_cases/stdlib/test_weakref.py new file mode 100644 index 000000000000..563b2c6fdd96 --- /dev/null +++ b/test_cases/stdlib/test_weakref.py @@ -0,0 +1,22 @@ +from collections.abc import Callable +from typing_extensions import assert_type, TypeAlias +from weakref import finalize + +_ResultStructure: TypeAlias = tuple[str, Callable[[int, int], int], tuple[int], dict[str, int]] + + +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) +final2 = finalize(TObj, callback, 1, y=2) +assert_type(final1.peek(), _ResultStructure) +assert_type(final1.detach(), _ResultStructure) +assert_type(final2(), int) From ecc485242ae8804ddc56e66032a901c3765c38f2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Jul 2022 16:11:26 +0000 Subject: [PATCH 04/10] [pre-commit.ci] auto fixes from pre-commit.com hooks --- test_cases/stdlib/test_weakref.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_cases/stdlib/test_weakref.py b/test_cases/stdlib/test_weakref.py index 563b2c6fdd96..d4ce92f1b5f6 100644 --- a/test_cases/stdlib/test_weakref.py +++ b/test_cases/stdlib/test_weakref.py @@ -1,5 +1,5 @@ from collections.abc import Callable -from typing_extensions import assert_type, TypeAlias +from typing_extensions import TypeAlias, assert_type from weakref import finalize _ResultStructure: TypeAlias = tuple[str, Callable[[int, int], int], tuple[int], dict[str, int]] From ecb8206c0a571c87c8c17c71220a20d299a327b7 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Fri, 29 Jul 2022 12:12:06 -0400 Subject: [PATCH 05/10] fix: variable name mismatch --- test_cases/stdlib/test_weakref.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test_cases/stdlib/test_weakref.py b/test_cases/stdlib/test_weakref.py index d4ce92f1b5f6..4e716f0406d9 100644 --- a/test_cases/stdlib/test_weakref.py +++ b/test_cases/stdlib/test_weakref.py @@ -15,8 +15,9 @@ def callback(x: int, y: int) -> int: return x + y -final = finalize(TObj, callback, 1, y=2) -final2 = finalize(TObj, callback, 1, y=2) +final1 = finalize(TObj, callback, 1, y=2) +final2 = finalize(TObj, callback, 1, y=2) # detaching will make final1 return None assert_type(final1.peek(), _ResultStructure) assert_type(final1.detach(), _ResultStructure) +assert_type(final1(), None) assert_type(final2(), int) From 0dade0aebfda1dfdab15e6ed0f0859b8d9dab4f9 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Fri, 29 Jul 2022 12:18:16 -0400 Subject: [PATCH 06/10] fix: assert_type behavior --- test_cases/stdlib/test_weakref.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/test_cases/stdlib/test_weakref.py b/test_cases/stdlib/test_weakref.py index 4e716f0406d9..440b9091a5de 100644 --- a/test_cases/stdlib/test_weakref.py +++ b/test_cases/stdlib/test_weakref.py @@ -1,8 +1,9 @@ from collections.abc import Callable +from typing import Any, Optional, Tuple, Dict from typing_extensions import TypeAlias, assert_type from weakref import finalize -_ResultStructure: TypeAlias = tuple[str, Callable[[int, int], int], tuple[int], dict[str, int]] +_ResultStructure: TypeAlias = Optional[Tuple[str, Callable[[int, int], int], Tuple[Any, ...], Dict[str, Any]]] class TObj: @@ -15,9 +16,7 @@ def callback(x: int, y: int) -> int: return x + y -final1 = finalize(TObj, callback, 1, y=2) -final2 = finalize(TObj, callback, 1, y=2) # detaching will make final1 return None -assert_type(final1.peek(), _ResultStructure) -assert_type(final1.detach(), _ResultStructure) -assert_type(final1(), None) -assert_type(final2(), int) +final = finalize(TObj, callback, 1, y=2) +assert_type(final.peek(), _ResultStructure) +assert_type(final.detach(), _ResultStructure) +assert_type(final1(), int | None) From cb7836b09358c36074bdb23d1ef2aef3030f9049 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Fri, 29 Jul 2022 12:19:18 -0400 Subject: [PATCH 07/10] fix: Use Callable from typing --- test_cases/stdlib/test_weakref.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test_cases/stdlib/test_weakref.py b/test_cases/stdlib/test_weakref.py index 440b9091a5de..005bf4fc3e42 100644 --- a/test_cases/stdlib/test_weakref.py +++ b/test_cases/stdlib/test_weakref.py @@ -1,5 +1,4 @@ -from collections.abc import Callable -from typing import Any, Optional, Tuple, Dict +from typing import Any, Optional, Tuple, Dict, Callable from typing_extensions import TypeAlias, assert_type from weakref import finalize From 9a486d196891c61beebee3262dcc44f38a40b4d3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Jul 2022 16:20:48 +0000 Subject: [PATCH 08/10] [pre-commit.ci] auto fixes from pre-commit.com hooks --- test_cases/stdlib/test_weakref.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_cases/stdlib/test_weakref.py b/test_cases/stdlib/test_weakref.py index 005bf4fc3e42..9da8d7750101 100644 --- a/test_cases/stdlib/test_weakref.py +++ b/test_cases/stdlib/test_weakref.py @@ -1,4 +1,4 @@ -from typing import Any, Optional, Tuple, Dict, Callable +from typing import Any, Callable, Dict, Optional, Tuple from typing_extensions import TypeAlias, assert_type from weakref import finalize From f7473495693e5927a980fb341d9a5b73c2cf66a9 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Fri, 29 Jul 2022 12:24:23 -0400 Subject: [PATCH 09/10] fix: leftover incorrect variable name --- test_cases/stdlib/test_weakref.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_cases/stdlib/test_weakref.py b/test_cases/stdlib/test_weakref.py index 9da8d7750101..e54dc24a29de 100644 --- a/test_cases/stdlib/test_weakref.py +++ b/test_cases/stdlib/test_weakref.py @@ -18,4 +18,4 @@ def callback(x: int, y: int) -> int: final = finalize(TObj, callback, 1, y=2) assert_type(final.peek(), _ResultStructure) assert_type(final.detach(), _ResultStructure) -assert_type(final1(), int | None) +assert_type(final(), int | None) From 8ee8ab464c30c106a5460464acf54d04d1f93240 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Sun, 31 Jul 2022 06:25:52 -0400 Subject: [PATCH 10/10] fix: remove todo and revert all previoius changes :( --- stdlib/weakref.pyi | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index f80fb876e710..691558eec3d5 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -31,14 +31,13 @@ __all__ = [ "finalize", ] -_CallableT = TypeVar("_CallableT", bound=Callable[..., Any]) -_KT = TypeVar("_KT") -_P = ParamSpec("_P") -_R = TypeVar("_R") _T = TypeVar("_T") _T1 = TypeVar("_T1") _T2 = TypeVar("_T2") +_KT = TypeVar("_KT") _VT = TypeVar("_VT") +_CallableT = TypeVar("_CallableT", bound=Callable[..., Any]) +_P = ParamSpec("_P") ProxyTypes: tuple[type[Any], ...] @@ -125,11 +124,11 @@ class WeakKeyDictionary(MutableMapping[_KT, _VT]): @overload def __ior__(self: Self, other: Iterable[tuple[_KT, _VT]]) -> Self: ... -class finalize(Generic[_P, _T, _R]): - def __init__(self, __obj: _T, __func: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> None: ... - def __call__(self, _: object = ...) -> _R | None: ... - def detach(self) -> tuple[_T, Callable[_P, _R], tuple[Any, ...], dict[str, Any]] | None: ... - def peek(self) -> tuple[_T, Callable[_P, _R], tuple[Any, ...], dict[str, Any]] | None: ... +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: ... + def peek(self) -> tuple[Any, Any, tuple[Any, ...], dict[str, Any]] | None: ... @property def alive(self) -> bool: ... atexit: bool