From: Denis Laxalde Date: Wed, 28 Apr 2021 12:58:19 +0000 (+0200) Subject: Define an AnyCursor type alias X-Git-Tag: 3.0.dev0~63^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=98e1ace9dbebfdfbf0937c4f7c0b39f1545eb01c;p=thirdparty%2Fpsycopg.git Define an AnyCursor type alias This will make it easier to define custom row factories without having to use BaseCursor[Any, ] as the connection type is not meaningful in this context. We expose this name instead of BaseCursor in top-level package accordingly. --- diff --git a/docs/advanced/rows.rst b/docs/advanced/rows.rst index ed53f335d..c6a18dbe3 100644 --- a/docs/advanced/rows.rst +++ b/docs/advanced/rows.rst @@ -17,10 +17,10 @@ This can be implemented as a class, for instance: .. code:: python from typing import Any, Sequence - from psycopg3 import BaseCursor + from psycopg3 import AnyCursor class DictRowFactory: - def __init__(self, cursor: BaseCursor[Any, dict[str, Any]]): + def __init__(self, cursor: AnyCursor[dict[str, Any]]): self.fields = [c.name for c in cursor.description] def __call__(self, values: Sequence[Any]) -> dict[str, Any]: @@ -31,7 +31,7 @@ or as a plain function: .. code:: python def dict_row_factory( - cursor: BaseCursor[Any, dict[str, Any]] + cursor: AnyCursor[dict[str, Any]] ) -> Callable[[Sequence[Any]], dict[str, Any]]: fields = [c.name for c in cursor.description] diff --git a/psycopg3/psycopg3/__init__.py b/psycopg3/psycopg3/__init__.py index 642c6011c..b1c651903 100644 --- a/psycopg3/psycopg3/__init__.py +++ b/psycopg3/psycopg3/__init__.py @@ -10,7 +10,7 @@ from . import pq from . import types from .copy import Copy, AsyncCopy from .adapt import global_adapters -from .cursor import AsyncCursor, Cursor, BaseCursor +from .cursor import AnyCursor, AsyncCursor, Cursor from .errors import Warning, Error, InterfaceError, DatabaseError from .errors import DataError, OperationalError, IntegrityError from .errors import InternalError, ProgrammingError, NotSupportedError @@ -47,13 +47,13 @@ BinaryDumper.register(Binary, global_adapters) # dbapi20 # so that function signatures are consistent with the documentation. __all__ = [ "__version__", + "AnyCursor", "AsyncConnection", "AsyncCopy", "AsyncCursor", "AsyncServerCursor", "AsyncTransaction", "BaseConnection", - "BaseCursor", "Column", "Connection", "Copy", diff --git a/psycopg3/psycopg3/_column.py b/psycopg3/psycopg3/_column.py index caeddc563..e16ade777 100644 --- a/psycopg3/psycopg3/_column.py +++ b/psycopg3/psycopg3/_column.py @@ -10,7 +10,7 @@ from operator import attrgetter from . import errors as e if TYPE_CHECKING: - from .cursor import BaseCursor + from .cursor import AnyCursor class ColumnData(NamedTuple): @@ -23,7 +23,7 @@ class Column(Sequence[Any]): __module__ = "psycopg3" - def __init__(self, cursor: "BaseCursor[Any, Any]", index: int): + def __init__(self, cursor: "AnyCursor[Any]", index: int): res = cursor.pgresult assert res diff --git a/psycopg3/psycopg3/cursor.py b/psycopg3/psycopg3/cursor.py index ac5cccac7..11fbd4a7a 100644 --- a/psycopg3/psycopg3/cursor.py +++ b/psycopg3/psycopg3/cursor.py @@ -471,6 +471,9 @@ class BaseCursor(Generic[ConnectionType, Row]): self._pgq = pgq +AnyCursor = BaseCursor[Any, Row] + + class Cursor(BaseCursor["Connection[Any]", Row]): __module__ = "psycopg3" __slots__ = () diff --git a/psycopg3/psycopg3/proto.py b/psycopg3/psycopg3/proto.py index f83982f65..657780967 100644 --- a/psycopg3/psycopg3/proto.py +++ b/psycopg3/psycopg3/proto.py @@ -14,7 +14,7 @@ from ._enums import Format if TYPE_CHECKING: from .connection import BaseConnection - from .cursor import BaseCursor + from .cursor import AnyCursor from .adapt import Dumper, Loader, AdaptersMap from .waiting import Wait, Ready from .sql import Composable @@ -59,7 +59,7 @@ class RowMaker(Protocol[Row_co]): class RowFactory(Protocol[Row]): - def __call__(self, __cursor: "BaseCursor[Any, Row]") -> RowMaker[Row]: + def __call__(self, __cursor: "AnyCursor[Row]") -> RowMaker[Row]: ... diff --git a/psycopg3/psycopg3/rows.py b/psycopg3/psycopg3/rows.py index 7e4babf1f..1aec4d9ed 100644 --- a/psycopg3/psycopg3/rows.py +++ b/psycopg3/psycopg3/rows.py @@ -13,14 +13,14 @@ from typing import TYPE_CHECKING from . import errors as e if TYPE_CHECKING: - from .cursor import BaseCursor + from .cursor import AnyCursor TupleRow = Tuple[Any, ...] def tuple_row( - cursor: "BaseCursor[Any, TupleRow]", + cursor: "AnyCursor[TupleRow]", ) -> Callable[[Sequence[Any]], TupleRow]: """Row factory to represent rows as simple tuples. @@ -35,7 +35,7 @@ DictRow = Dict[str, Any] def dict_row( - cursor: "BaseCursor[Any, DictRow]", + cursor: "AnyCursor[DictRow]", ) -> Callable[[Sequence[Any]], DictRow]: """Row factory to represent rows as dicts. @@ -54,7 +54,7 @@ def dict_row( def namedtuple_row( - cursor: "BaseCursor[Any, NamedTuple]", + cursor: "AnyCursor[NamedTuple]", ) -> Callable[[Sequence[Any]], NamedTuple]: """Row factory to represent rows as `~collections.namedtuple`.""" diff --git a/tests/test_typing.py b/tests/test_typing.py index c2df4c050..cca856bf8 100644 --- a/tests/test_typing.py +++ b/tests/test_typing.py @@ -282,7 +282,7 @@ class Thing: self.kwargs = kwargs def thing_row( - cur: psycopg3.BaseCursor[Any, Thing], + cur: psycopg3.AnyCursor[Thing], ) -> Callable[[Sequence[Any]], Thing]: assert cur.description names = [d.name for d in cur.description] diff --git a/tests/typing_example.py b/tests/typing_example.py index f03116a95..bc86ecdb6 100644 --- a/tests/typing_example.py +++ b/tests/typing_example.py @@ -5,12 +5,10 @@ from __future__ import annotations from dataclasses import dataclass from typing import Any, Callable, Optional, Sequence, Tuple -from psycopg3 import BaseCursor, Connection, Cursor, ServerCursor, connect +from psycopg3 import AnyCursor, Connection, Cursor, ServerCursor, connect -def int_row_factory( - cursor: BaseCursor[Any, int] -) -> Callable[[Sequence[int]], int]: +def int_row_factory(cursor: AnyCursor[int]) -> Callable[[Sequence[int]], int]: return lambda values: values[0] if values else 42 @@ -21,7 +19,7 @@ class Person: @classmethod def row_factory( - cls, cursor: BaseCursor[Any, Person] + cls, cursor: AnyCursor[Person] ) -> Callable[[Sequence[str]], Person]: def mkrow(values: Sequence[str]) -> Person: name, address = values