]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
docs: clean up details and errors in type annotations, more pages
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 10 Oct 2023 14:11:52 +0000 (16:11 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 10 Oct 2023 14:11:52 +0000 (16:11 +0200)
docs/advanced/rows.rst
docs/advanced/typing.rst

index fcb89ce9a8b3d4261e89c1a96713a2c3447ab185..3b8a4f1eb8b9dcc298bc4601c12eadfb9c33a428 100644 (file)
@@ -55,6 +55,17 @@ If you want to return objects of your choice you can use a row factory
     >>> cur.execute("select 'John Doe' as name, 33 as age").fetchone()
     Person(name='John Doe', age=33, weight=None)
 
+.. note::
+
+    The choice of a `!row_factory` in a `!Connection` or a `!Cursor`
+    constructor affects how the object is annotated for static type checking.
+
+    For instance, declaring a `!row_factory=dict_row` will result in the
+    cursors' `!executeany()` annotated as returning `list[dict[str, Any]]`
+    instead of `list[tuple[Any, ...]]`.
+
+    Please check :ref:`static-typing` for more details.
+
 
 .. index::
     single: Row Maker
index 5a91f60ea5bc0428ea4fd8fc2ddcc47bac625145..f1f901268c4acac5361adc8042ffac5b93bb5a3c 100644 (file)
@@ -19,24 +19,26 @@ Generic types
 -------------
 
 Psycopg `Connection` and `Cursor` objects are `~typing.Generic` objects and
-support a `!Row` parameter which is the type of the records returned.
+support a `!Row` parameter which is the type of the records returned. The
+parameter can be configured by passing a `!row_factory` parameter to the
+constructor or to the `~Connection.cursor()` method.
 
-By default methods such as `Cursor.fetchall()` return normal tuples of unknown
-size and content. As such, the `connect()` function returns an object of type
-`!psycopg.Connection[Tuple[Any, ...]]` and `Connection.cursor()` returns an
-object of type `!psycopg.Cursor[Tuple[Any, ...]]`. If you are writing generic
-plumbing code it might be practical to use annotations such as
-`!Connection[Any]` and `!Cursor[Any]`.
+By default, methods producing records such as `Cursor.fetchall()` return
+normal tuples of unknown size and content. As such, the `connect()` function
+returns an object of type `!psycopg.Connection[tuple[Any, ...]]` and
+`Connection.cursor()` returns an object of type `!psycopg.Cursor[tuple[Any,
+...]]`. If you are writing generic plumbing code it might be practical to use
+annotations such as `!Connection[Any]` and `!Cursor[Any]`.
 
 .. code:: python
 
-   conn = psycopg.connect() # type is psycopg.Connection[Tuple[Any, ...]]
+   conn = psycopg.connect() # type is psycopg.Connection[tuple[Any, ...]]
 
-   cur = conn.cursor()      # type is psycopg.Cursor[Tuple[Any, ...]]
+   cur = conn.cursor()      # type is psycopg.Cursor[tuple[Any, ...]]
 
-   rec = cur.fetchone()     # type is Optional[Tuple[Any, ...]]
+   rec = cur.fetchone()     # type is Optional[tuple[Any, ...]]
 
-   recs = cur.fetchall()    # type is List[Tuple[Any, ...]]
+   recs = cur.fetchall()    # type is List[tuple[Any, ...]]
 
 
 .. _row-factory-static:
@@ -54,14 +56,14 @@ cursors and annotate the returned objects accordingly. See
 .. code:: python
 
    dconn = psycopg.connect(row_factory=dict_row)
-   # dconn type is psycopg.Connection[Dict[str, Any]]
+   # dconn type is psycopg.Connection[dict[str, Any]]
 
    dcur = conn.cursor(row_factory=dict_row)
    dcur = dconn.cursor()
-   # dcur type is psycopg.Cursor[Dict[str, Any]] in both cases
+   # dcur type is psycopg.Cursor[dict[str, Any]] in both cases
 
    drec = dcur.fetchone()
-   # drec type is Optional[Dict[str, Any]]
+   # drec type is Optional[dict[str, Any]]
 
 
 .. _pool-generic:
@@ -109,7 +111,7 @@ type is not parametric) then it's not necessary to specify `!kwargs`:
             kwargs["row_factory"] = dict_row
             super().__init__(*args, **kwargs)
 
-    with ConnectionPool(connection_class=MyConnection[DictRow]) as pool:
+    with ConnectionPool(connection_class=MyConnection) as pool:
         # reveal_type(pool): ConnectionPool[MyConnection]
 
         with pool.connection() as conn: