]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Define an AnyCursor type alias
authorDenis Laxalde <denis.laxalde@dalibo.com>
Wed, 28 Apr 2021 12:58:19 +0000 (14:58 +0200)
committerDenis Laxalde <denis.laxalde@dalibo.com>
Fri, 30 Apr 2021 06:46:38 +0000 (08:46 +0200)
This will make it easier to define custom row factories without having
to use BaseCursor[Any, <rowtype>] as the connection type is not
meaningful in this context.

We expose this name instead of BaseCursor in top-level package
accordingly.

docs/advanced/rows.rst
psycopg3/psycopg3/__init__.py
psycopg3/psycopg3/_column.py
psycopg3/psycopg3/cursor.py
psycopg3/psycopg3/proto.py
psycopg3/psycopg3/rows.py
tests/test_typing.py
tests/typing_example.py

index ed53f335ddad8982b3445259dc3e6e9c4851d590..c6a18dbe3ce3a0ac4e413cdd36c4786395ffc2b1 100644 (file)
@@ -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]
 
index 642c6011c76b17c77ece5f1eea06f5a6fea9616d..b1c651903bc610b53a90e6ce0039d1215c7471ed 100644 (file)
@@ -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",
index caeddc5636ac5adb1fa39161d1698f8f0ea6f054..e16ade777ac2312077d1af9b8e227a767d019e9a 100644 (file)
@@ -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
 
index ac5cccac7a5a3229e77016f587fc85587b31904c..11fbd4a7a8209c112d5bf806826b32ae5954f9f6 100644 (file)
@@ -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__ = ()
index f83982f65dc3a43f6b4574f2b374d2ea0070178c..657780967f53741fef6694956639dfde2d18f25e 100644 (file)
@@ -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]:
         ...
 
 
index 7e4babf1fc6b79d2d5f9d5a37aa18dd7194ed5ea..1aec4d9ed581117147dba270f82e9cb89afeccf8 100644 (file)
@@ -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`."""
 
index c2df4c05075f53c9ca3123d9b9c559984611a8f5..cca856bf83261e033b0d8a9a85ccc9ec7492ab28 100644 (file)
@@ -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]
index f03116a95cb5555837e30797c26f3b66ca58c129..bc86ecdb6cf925ed1d986f4fb1e84a53a8a3e7a0 100644 (file)
@@ -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