From 1071dde976ffac97ecde959dd1cf412bffefaf76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 27 Aug 2021 12:34:33 -0400 Subject: [PATCH 1/8] TYP: cache_readonly --- pandas/_libs/properties.pyi | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 pandas/_libs/properties.pyi diff --git a/pandas/_libs/properties.pyi b/pandas/_libs/properties.pyi new file mode 100644 index 0000000000000..66015fa47b49d --- /dev/null +++ b/pandas/_libs/properties.pyi @@ -0,0 +1,13 @@ +from typing import ( + Callable, + Protocol, + TypeVar, +) + +_F = TypeVar("_F", contravariant=True) +_G = TypeVar("_G") + +class cache_readonly(Protocol[_F, _G]): + def __init__(self, func: Callable[[_F], _G]) -> None: ... + def __get__(self, obj: _F, typ) -> _G: ... + def __set__(self, obj: _F, value: _G) -> None: ... From 47d0126aed01c643ad55ed97e014f021449f5850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sat, 28 Aug 2021 21:11:20 -0400 Subject: [PATCH 2/8] overload for self --- pandas/_libs/properties.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/properties.pyi b/pandas/_libs/properties.pyi index 66015fa47b49d..74b96e6b29862 100644 --- a/pandas/_libs/properties.pyi +++ b/pandas/_libs/properties.pyi @@ -2,12 +2,16 @@ from typing import ( Callable, Protocol, TypeVar, + overload, ) -_F = TypeVar("_F", contravariant=True) +_F = TypeVar("_F") _G = TypeVar("_G") class cache_readonly(Protocol[_F, _G]): def __init__(self, func: Callable[[_F], _G]) -> None: ... + @overload def __get__(self, obj: _F, typ) -> _G: ... + @overload + def __get__(self, obj: None, typ) -> cache_readonly[_F, _G]: ... def __set__(self, obj: _F, value: _G) -> None: ... From f7f831be3dc8a6fa8f4865ff22759bf53f0f31fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 29 Aug 2021 09:34:53 -0400 Subject: [PATCH 3/8] type 'typ' --- pandas/_libs/properties.pyi | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/properties.pyi b/pandas/_libs/properties.pyi index 74b96e6b29862..3551c8487adc7 100644 --- a/pandas/_libs/properties.pyi +++ b/pandas/_libs/properties.pyi @@ -1,6 +1,9 @@ +from __future__ import annotations + from typing import ( Callable, Protocol, + Type, TypeVar, overload, ) @@ -11,7 +14,7 @@ _G = TypeVar("_G") class cache_readonly(Protocol[_F, _G]): def __init__(self, func: Callable[[_F], _G]) -> None: ... @overload - def __get__(self, obj: _F, typ) -> _G: ... + def __get__(self, obj: _F, typ: Type[_F] | None) -> _G: ... @overload - def __get__(self, obj: None, typ) -> cache_readonly[_F, _G]: ... + def __get__(self, obj: None, typ: Type[_F] | None) -> cache_readonly[_F, _G]: ... def __set__(self, obj: _F, value: _G) -> None: ... From 790c084ba4219a2201351b1eac22899c1732adb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 29 Aug 2021 17:49:31 -0400 Subject: [PATCH 4/8] Generic instead of Protocol --- pandas/_libs/properties.pyi | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pandas/_libs/properties.pyi b/pandas/_libs/properties.pyi index 3551c8487adc7..c417abbe9f14b 100644 --- a/pandas/_libs/properties.pyi +++ b/pandas/_libs/properties.pyi @@ -2,19 +2,18 @@ from __future__ import annotations from typing import ( Callable, - Protocol, - Type, + Generic, TypeVar, overload, ) -_F = TypeVar("_F") -_G = TypeVar("_G") +F = TypeVar("F") +G = TypeVar("G") -class cache_readonly(Protocol[_F, _G]): - def __init__(self, func: Callable[[_F], _G]) -> None: ... +class cache_readonly(Generic[F, G]): + def __init__(self, func: Callable[[F], G]) -> None: ... @overload - def __get__(self, obj: _F, typ: Type[_F] | None) -> _G: ... + def __get__(self, obj: F, typ) -> G: ... @overload - def __get__(self, obj: None, typ: Type[_F] | None) -> cache_readonly[_F, _G]: ... - def __set__(self, obj: _F, value: _G) -> None: ... + def __get__(self, obj: None, typ) -> cache_readonly[F, G]: ... + def __set__(self, obj: F, value: G) -> None: ... From b74e9e21dca05d3840a2775e7ecd79331bfaed1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 29 Aug 2021 19:23:40 -0400 Subject: [PATCH 5/8] need to expose everything from properties.pyx --- pandas/_libs/properties.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/properties.pyi b/pandas/_libs/properties.pyi index c417abbe9f14b..93a77336f6619 100644 --- a/pandas/_libs/properties.pyi +++ b/pandas/_libs/properties.pyi @@ -1,6 +1,7 @@ from __future__ import annotations from typing import ( + Any, Callable, Generic, TypeVar, @@ -10,10 +11,14 @@ from typing import ( F = TypeVar("F") G = TypeVar("G") -class cache_readonly(Generic[F, G]): +class CachedProperty(Generic[F, G]): def __init__(self, func: Callable[[F], G]) -> None: ... @overload def __get__(self, obj: F, typ) -> G: ... @overload def __get__(self, obj: None, typ) -> cache_readonly[F, G]: ... def __set__(self, obj: F, value: G) -> None: ... + +cache_readonly = CachedProperty + +AxisProperty: Any From 3f17b6178418edf24e1dc3c09f9731118e29334d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Tue, 31 Aug 2021 20:54:40 -0400 Subject: [PATCH 6/8] co/contra --- pandas/_libs/properties.pyi | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pandas/_libs/properties.pyi b/pandas/_libs/properties.pyi index 93a77336f6619..c7d2accb3381c 100644 --- a/pandas/_libs/properties.pyi +++ b/pandas/_libs/properties.pyi @@ -4,21 +4,25 @@ from typing import ( Any, Callable, Generic, + Type, TypeVar, overload, ) -F = TypeVar("F") -G = TypeVar("G") +F = TypeVar("F", contravariant=True) +G = TypeVar("G", covariant=True) class CachedProperty(Generic[F, G]): def __init__(self, func: Callable[[F], G]) -> None: ... @overload - def __get__(self, obj: F, typ) -> G: ... + def __get__(self, obj: F, typ: Type[F]) -> G: ... @overload - def __get__(self, obj: None, typ) -> cache_readonly[F, G]: ... - def __set__(self, obj: F, value: G) -> None: ... + def __get__(self, obj: None, typ: Type[F]) -> cache_readonly[F, G]: ... + def __set__(self, obj: F, value: Any) -> None: ... cache_readonly = CachedProperty -AxisProperty: Any +class AxisProperty: + def __init__(self, axis: int = ..., doc: str = ...) -> None: ... + def __get__(self, obj: Any, typ: Type) -> Any: ... + def __set__(self, obj: Any, value: Any) -> None: ... From e2024f79d742e05e68d0c9f4eaabc9a46e52b204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 1 Sep 2021 09:50:38 -0400 Subject: [PATCH 7/8] sub-classing --- pandas/_libs/properties.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/properties.pyi b/pandas/_libs/properties.pyi index c7d2accb3381c..bfae71fcd7f11 100644 --- a/pandas/_libs/properties.pyi +++ b/pandas/_libs/properties.pyi @@ -9,6 +9,7 @@ from typing import ( overload, ) +T = TypeVar("T") F = TypeVar("F", contravariant=True) G = TypeVar("G", covariant=True) @@ -17,7 +18,7 @@ class CachedProperty(Generic[F, G]): @overload def __get__(self, obj: F, typ: Type[F]) -> G: ... @overload - def __get__(self, obj: None, typ: Type[F]) -> cache_readonly[F, G]: ... + def __get__(self: T, obj: None, typ: Type[F]) -> T: ... def __set__(self, obj: F, value: Any) -> None: ... cache_readonly = CachedProperty From 9345b0a07fe6691c95844e4a32ead1fc722c9df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 1 Sep 2021 13:58:41 -0400 Subject: [PATCH 8/8] cache_readonly is almost a property --- pandas/_libs/properties.pyi | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/pandas/_libs/properties.pyi b/pandas/_libs/properties.pyi index bfae71fcd7f11..6f2b5fd0be620 100644 --- a/pandas/_libs/properties.pyi +++ b/pandas/_libs/properties.pyi @@ -1,26 +1,9 @@ -from __future__ import annotations - from typing import ( Any, - Callable, - Generic, Type, - TypeVar, - overload, ) -T = TypeVar("T") -F = TypeVar("F", contravariant=True) -G = TypeVar("G", covariant=True) - -class CachedProperty(Generic[F, G]): - def __init__(self, func: Callable[[F], G]) -> None: ... - @overload - def __get__(self, obj: F, typ: Type[F]) -> G: ... - @overload - def __get__(self: T, obj: None, typ: Type[F]) -> T: ... - def __set__(self, obj: F, value: Any) -> None: ... - +CachedProperty: Type[property] cache_readonly = CachedProperty class AxisProperty: