From: Daniele Varrazzo Date: Fri, 8 Jan 2021 01:24:33 +0000 (+0100) Subject: Return type of fetch methods improved X-Git-Tag: 3.0.dev0~196 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=66fd0942934e78206b5ae7cd67eb4d5aff000baf;p=thirdparty%2Fpsycopg.git Return type of fetch methods improved --- diff --git a/psycopg3/psycopg3/_transform.py b/psycopg3/psycopg3/_transform.py index 1cdea90d0..1f792f563 100644 --- a/psycopg3/psycopg3/_transform.py +++ b/psycopg3/psycopg3/_transform.py @@ -131,7 +131,7 @@ class Transformer(AdaptContext): f"cannot adapt type {cls.__name__} to format {Format(format).name}" ) - def load_rows(self, row0: int, row1: int) -> Sequence[Tuple[Any, ...]]: + def load_rows(self, row0: int, row1: int) -> List[Tuple[Any, ...]]: res = self._pgresult if not res: raise e.InterfaceError("result not set") diff --git a/psycopg3/psycopg3/cursor.py b/psycopg3/psycopg3/cursor.py index d3ac28608..9c029cf8d 100644 --- a/psycopg3/psycopg3/cursor.py +++ b/psycopg3/psycopg3/cursor.py @@ -446,7 +446,7 @@ class Cursor(BaseCursor["Connection"]): self._pos += 1 return record - def fetchmany(self, size: int = 0) -> List[Sequence[Any]]: + def fetchmany(self, size: int = 0) -> Sequence[Sequence[Any]]: """ Return the next *size* records from the current recordset. @@ -461,9 +461,9 @@ class Cursor(BaseCursor["Connection"]): self._pos, min(self._pos + size, self.pgresult.ntuples) ) self._pos += len(records) - return records # type: ignore[return-value] + return records - def fetchall(self) -> List[Sequence[Any]]: + def fetchall(self) -> Sequence[Sequence[Any]]: """ Return all the remaining records from the current recordset. """ @@ -471,7 +471,7 @@ class Cursor(BaseCursor["Connection"]): assert self.pgresult records = self._transformer.load_rows(self._pos, self.pgresult.ntuples) self._pos += self.pgresult.ntuples - return records # type: ignore[return-value] + return records def __iter__(self) -> Iterator[Sequence[Any]]: self._check_result() @@ -541,7 +541,7 @@ class AsyncCursor(BaseCursor["AsyncConnection"]): self._pos += 1 return rv - async def fetchmany(self, size: int = 0) -> List[Sequence[Any]]: + async def fetchmany(self, size: int = 0) -> Sequence[Sequence[Any]]: self._check_result() assert self.pgresult @@ -551,14 +551,14 @@ class AsyncCursor(BaseCursor["AsyncConnection"]): self._pos, min(self._pos + size, self.pgresult.ntuples) ) self._pos += len(records) - return records # type: ignore[return-value] + return records - async def fetchall(self) -> List[Sequence[Any]]: + async def fetchall(self) -> Sequence[Sequence[Any]]: self._check_result() assert self.pgresult records = self._transformer.load_rows(self._pos, self.pgresult.ntuples) self._pos += self.pgresult.ntuples - return records # type: ignore[return-value] + return records async def __aiter__(self) -> AsyncIterator[Sequence[Any]]: self._check_result() diff --git a/psycopg3/psycopg3/proto.py b/psycopg3/psycopg3/proto.py index f0a5a4c3e..fbaa6eea4 100644 --- a/psycopg3/psycopg3/proto.py +++ b/psycopg3/psycopg3/proto.py @@ -99,7 +99,7 @@ class Transformer(Protocol): def get_dumper(self, obj: Any, format: Format) -> "Dumper": ... - def load_rows(self, row0: int, row1: int) -> Sequence[Tuple[Any, ...]]: + def load_rows(self, row0: int, row1: int) -> List[Tuple[Any, ...]]: ... def load_row(self, row: int) -> Optional[Tuple[Any, ...]]: diff --git a/psycopg3/psycopg3/types/composite.py b/psycopg3/psycopg3/types/composite.py index 5ad3bee4e..0aabb4653 100644 --- a/psycopg3/psycopg3/types/composite.py +++ b/psycopg3/psycopg3/types/composite.py @@ -105,7 +105,7 @@ class CompositeInfo(TypeInfo): ) @classmethod - def _from_records(cls, recs: List[Any]) -> Optional["CompositeInfo"]: + def _from_records(cls, recs: Sequence[Any]) -> Optional["CompositeInfo"]: if not recs: return None if len(recs) > 1: diff --git a/psycopg3/psycopg3/types/range.py b/psycopg3/psycopg3/types/range.py index d204d44e6..e8102d22b 100644 --- a/psycopg3/psycopg3/types/range.py +++ b/psycopg3/psycopg3/types/range.py @@ -5,7 +5,7 @@ Support for range types adaptation. # Copyright (C) 2020 The Psycopg Team import re -from typing import Any, Dict, Generic, List, Optional, TypeVar, Type, Union +from typing import Any, Dict, Generic, Optional, Sequence, TypeVar, Type, Union from typing import cast, TYPE_CHECKING from decimal import Decimal from datetime import date, datetime @@ -413,7 +413,7 @@ class RangeInfo(TypeInfo): ) @classmethod - def _from_records(cls, recs: List[Any]) -> Optional["RangeInfo"]: + def _from_records(cls, recs: Sequence[Any]) -> Optional["RangeInfo"]: if not recs: return None if len(recs) > 1: diff --git a/psycopg3_c/psycopg3_c/_psycopg3.pyi b/psycopg3_c/psycopg3_c/_psycopg3.pyi index 9e24e0b83..b1043a528 100644 --- a/psycopg3_c/psycopg3_c/_psycopg3.pyi +++ b/psycopg3_c/psycopg3_c/_psycopg3.pyi @@ -32,7 +32,7 @@ class Transformer(proto.AdaptContext): self, params: Sequence[Any], formats: Sequence[Format] ) -> Tuple[List[Any], Tuple[int, ...]]: ... def get_dumper(self, obj: Any, format: Format) -> Dumper: ... - def load_rows(self, row0: int, row1: int) -> Sequence[Tuple[Any, ...]]: ... + def load_rows(self, row0: int, row1: int) -> List[Tuple[Any, ...]]: ... def load_row(self, row: int) -> Optional[Tuple[Any, ...]]: ... def load_sequence( self, record: Sequence[Optional[bytes]] diff --git a/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx b/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx index 4b9c62e43..8d9ea6ff3 100644 --- a/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx +++ b/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx @@ -222,7 +222,7 @@ cdef class Transformer: return ps, ts - def load_rows(self, int row0, int row1) -> Sequence[Tuple[Any, ...]]: + def load_rows(self, int row0, int row1) -> List[Tuple[Any, ...]]: if self._pgresult is None: raise e.InterfaceError("result not set")