From 0ba23bb7a489a2a2e7e879c8c9f6d8cd70200f96 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 14 Apr 2022 18:03:24 -0700 Subject: [PATCH 1/5] sqlite3: add 3.11 additions - Blob from python/cpython#30680 (and anticipating that python/cpython#91550 will be merged) - Aggregate window functions from python/cpython#20903 - Serialize/deserialize from python/cpython#26728 - Limit setting from python/cpython#28463 --- stdlib/sqlite3/dbapi2.pyi | 47 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/stdlib/sqlite3/dbapi2.pyi b/stdlib/sqlite3/dbapi2.pyi index 05e5a176d8ff..e1eaf99624e0 100644 --- a/stdlib/sqlite3/dbapi2.pyi +++ b/stdlib/sqlite3/dbapi2.pyi @@ -1,11 +1,12 @@ import sys -from _typeshed import Self, StrOrBytesPath +from _typeshed import ReadableBuffer, Self, StrOrBytesPath from datetime import date, datetime, time from types import TracebackType -from typing import Any, Callable, Generator, Iterable, Iterator, Protocol, TypeVar +from typing import Any, Callable, Generator, Iterable, Iterator, Protocol, TypeVar, overload from typing_extensions import Literal, final _T = TypeVar("_T") +_SqliteData = str | bytes | int | float | None paramstyle: str threadsafety: int @@ -128,6 +129,18 @@ class _AggregateProtocol(Protocol): def step(self, value: int) -> None: ... def finalize(self) -> int: ... +class _SingleParamWindowAggregateClass(Protocol): + def step(self, __param: Any) -> None: ... + def inverse(self, __param: Any) -> None: ... + def value(self) -> _SqliteData: ... + def finalize(self) -> _SqliteData: ... + +class _WindowAggretateClass(Protocol): + step: Callable[..., None] + inverse: Callable[..., None] + def value(self) -> _SqliteData: ... + def finalize(self) -> _SqliteData: ... + class Connection: DataError: Any DatabaseError: Any @@ -146,8 +159,23 @@ class Connection: total_changes: Any def __init__(self, *args: Any, **kwargs: Any) -> None: ... def close(self) -> None: ... + if sys.version_info >= (3, 11): + def blobopen(self, __table: str, __column: str, __row: int, *, readonly: bool = ..., name: str = ...) -> Blob: ... + def commit(self) -> None: ... def create_aggregate(self, name: str, n_arg: int, aggregate_class: Callable[[], _AggregateProtocol]) -> None: ... + if sys.version_info >= (3, 11): + # num_params determines how many params will be passed to the aggregate class. We provide an overload + # for the case where num_params = 1, which is expected to be the common case. + @overload + def create_window_function( + self, __name: str, __num_params: Literal[1], aggregate_class: Callable[[], _SingleParamWindowAggregateClass] + ) -> None: ... + @overload + def create_window_function( + self, __name: str, __num_params: int, aggregate_class: Callable[[], _WindowAggretateClass] + ) -> None: ... + def create_collation(self, __name: str, __callback: Any) -> None: ... if sys.version_info >= (3, 8): def create_function(self, name: str, narg: int, func: Any, *, deterministic: bool = ...) -> None: ... @@ -181,6 +209,11 @@ class Connection: name: str = ..., sleep: float = ..., ) -> None: ... + if sys.version_info >= (3, 11): + def setlimit(self, __category: int, __limit: int) -> int: ... + def getlimit(self, __category: int) -> int: ... + def serialize(self, *, name: str = ...) -> bytes: ... + def deserialize(self, __data: ReadableBuffer, *, name: str = ...) -> None: ... def __call__(self, *args: Any, **kwargs: Any) -> Any: ... def __enter__(self: Self) -> Self: ... @@ -253,3 +286,13 @@ if sys.version_info < (3, 8): def __init__(self, *args, **kwargs): ... class Warning(Exception): ... + +if sys.version_info >= (3, 11): + class Blob: + def close(self) -> None: ... + def read(self, __length: int = ...) -> bytes: ... + def write(self, __data: bytes) -> None: ... + def tell(self) -> int: ... + # whence must be one of os.SEEK_SET, os.SEEK_CUR, os.SEEK_END + def seek(self, __offset: int, __whence: int = ...) -> None: ... + def __len__(self) -> int: ... From 6024e3f2441937d38ad7423d06c7f01dfa2a3868 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 15 Apr 2022 21:28:35 -0700 Subject: [PATCH 2/5] blob.__enter__ from python/cpython#91562 --- stdlib/sqlite3/dbapi2.pyi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stdlib/sqlite3/dbapi2.pyi b/stdlib/sqlite3/dbapi2.pyi index e1eaf99624e0..fc517d8361ee 100644 --- a/stdlib/sqlite3/dbapi2.pyi +++ b/stdlib/sqlite3/dbapi2.pyi @@ -296,3 +296,5 @@ if sys.version_info >= (3, 11): # whence must be one of os.SEEK_SET, os.SEEK_CUR, os.SEEK_END def seek(self, __offset: int, __whence: int = ...) -> None: ... def __len__(self) -> int: ... + def __enter__(self: Self) -> Self: ... + def __exit__(self, __typ: object, __val: object, __tb: object) -> Literal[False]: ... From b0c6e853648be8cbcda9ca5a39b06c646d74aa43 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 15 Apr 2022 21:31:13 -0700 Subject: [PATCH 3/5] Sebastian's feedback --- stdlib/sqlite3/dbapi2.pyi | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/stdlib/sqlite3/dbapi2.pyi b/stdlib/sqlite3/dbapi2.pyi index fc517d8361ee..eecea7d22195 100644 --- a/stdlib/sqlite3/dbapi2.pyi +++ b/stdlib/sqlite3/dbapi2.pyi @@ -126,18 +126,24 @@ if sys.version_info < (3, 8): def get(self, *args, **kwargs) -> None: ... class _AggregateProtocol(Protocol): - def step(self, value: int) -> None: ... + def step(self, value: int) -> object: ... def finalize(self) -> int: ... class _SingleParamWindowAggregateClass(Protocol): - def step(self, __param: Any) -> None: ... - def inverse(self, __param: Any) -> None: ... + def step(self, __param: Any) -> object: ... + def inverse(self, __param: Any) -> object: ... + def value(self) -> _SqliteData: ... + def finalize(self) -> _SqliteData: ... + +class _AnyParamWindowAggreateClass(Protocol) + def step(self, *args: Any) -> object: ... + def inverse(self, *args: Any) -> object: ... def value(self) -> _SqliteData: ... def finalize(self) -> _SqliteData: ... class _WindowAggretateClass(Protocol): - step: Callable[..., None] - inverse: Callable[..., None] + step: Callable[..., object] + inverse: Callable[..., object] def value(self) -> _SqliteData: ... def finalize(self) -> _SqliteData: ... @@ -169,11 +175,16 @@ class Connection: # for the case where num_params = 1, which is expected to be the common case. @overload def create_window_function( - self, __name: str, __num_params: Literal[1], aggregate_class: Callable[[], _SingleParamWindowAggregateClass] + self, __name: str, __num_params: Literal[1], __aggregate_class: Callable[[], _SingleParamWindowAggregateClass] | None + ) -> None: ... + # And for num_params = -1, which means the aggregate must accept any number of parameters. + @overload + def create_window_function( + self, __name: str, __num_params: Literal[-1], __aggregate_class: Callable[[], _AnyParamWindowAggreateClass] | None ) -> None: ... @overload def create_window_function( - self, __name: str, __num_params: int, aggregate_class: Callable[[], _WindowAggretateClass] + self, __name: str, __num_params: int, __aggregate_class: Callable[[], _WindowAggretateClass] | None ) -> None: ... def create_collation(self, __name: str, __callback: Any) -> None: ... From fe01c60a27bfac0b955fade0bbc87352cca8bd0d Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 15 Apr 2022 21:32:20 -0700 Subject: [PATCH 4/5] derp --- stdlib/sqlite3/dbapi2.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/sqlite3/dbapi2.pyi b/stdlib/sqlite3/dbapi2.pyi index eecea7d22195..b113dcedeb3e 100644 --- a/stdlib/sqlite3/dbapi2.pyi +++ b/stdlib/sqlite3/dbapi2.pyi @@ -135,7 +135,7 @@ class _SingleParamWindowAggregateClass(Protocol): def value(self) -> _SqliteData: ... def finalize(self) -> _SqliteData: ... -class _AnyParamWindowAggreateClass(Protocol) +class _AnyParamWindowAggreateClass(Protocol): def step(self, *args: Any) -> object: ... def inverse(self, *args: Any) -> object: ... def value(self) -> _SqliteData: ... From eed182409cc34ba5b7ed380ca1ded94260ba112c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 16 Apr 2022 07:00:24 -0700 Subject: [PATCH 5/5] I can spell --- stdlib/sqlite3/dbapi2.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/sqlite3/dbapi2.pyi b/stdlib/sqlite3/dbapi2.pyi index b113dcedeb3e..1f129f323e76 100644 --- a/stdlib/sqlite3/dbapi2.pyi +++ b/stdlib/sqlite3/dbapi2.pyi @@ -135,13 +135,13 @@ class _SingleParamWindowAggregateClass(Protocol): def value(self) -> _SqliteData: ... def finalize(self) -> _SqliteData: ... -class _AnyParamWindowAggreateClass(Protocol): +class _AnyParamWindowAggregateClass(Protocol): def step(self, *args: Any) -> object: ... def inverse(self, *args: Any) -> object: ... def value(self) -> _SqliteData: ... def finalize(self) -> _SqliteData: ... -class _WindowAggretateClass(Protocol): +class _WindowAggregateClass(Protocol): step: Callable[..., object] inverse: Callable[..., object] def value(self) -> _SqliteData: ... @@ -180,11 +180,11 @@ class Connection: # And for num_params = -1, which means the aggregate must accept any number of parameters. @overload def create_window_function( - self, __name: str, __num_params: Literal[-1], __aggregate_class: Callable[[], _AnyParamWindowAggreateClass] | None + self, __name: str, __num_params: Literal[-1], __aggregate_class: Callable[[], _AnyParamWindowAggregateClass] | None ) -> None: ... @overload def create_window_function( - self, __name: str, __num_params: int, __aggregate_class: Callable[[], _WindowAggretateClass] | None + self, __name: str, __num_params: int, __aggregate_class: Callable[[], _WindowAggregateClass] | None ) -> None: ... def create_collation(self, __name: str, __callback: Any) -> None: ...