]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Use automatic type hints in docs
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 16 Apr 2021 18:01:02 +0000 (19:01 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 16 Apr 2021 19:18:38 +0000 (20:18 +0100)
The result is not perfect but it can be improved. Better than redefining
all the types.

12 files changed:
docs/advanced/adapt.rst
docs/api/connections.rst
docs/api/cursors.rst
docs/api/pq.rst
docs/conf.py
docs/lib/pg3_docs.py
docs/requirements.txt
psycopg3/psycopg3/adapt.py
psycopg3/psycopg3/connection.py
psycopg3/psycopg3/cursor.py
psycopg3/psycopg3/server_cursor.py
psycopg3/psycopg3/transaction.py

index f5fae47c7229e460092f0144d66b85759ea18067..f4a14b2b938ec550ee0ba72843f20dd8fe2e9849 100644 (file)
@@ -159,7 +159,6 @@ Objects involved in types adaptation
         your cake. Don't tell me I didn't warn you.
 
     .. autoattribute:: oid
-        :annotation: int
 
         .. admonition:: todo
 
index 6976c20a114438799c63bd783d23c9e375b80e07..32ccd45deeca46cdc222423b173acde43390262b 100644 (file)
@@ -62,10 +62,7 @@ The `!Connection` class
             close the connection automatically when the block is exited.
 
     .. autoattribute:: closed
-        :annotation: bool
-
     .. autoattribute:: broken
-        :annotation: bool
 
 
     .. method:: cursor(*, binary: bool = False, row_factory: Optional[RowFactory] = None) -> Cursor
@@ -115,7 +112,7 @@ The `!Connection` class
 
     .. automethod:: commit()
     .. automethod:: rollback()
-    .. automethod:: transaction(savepoint_name: Optional[str] = None, force_rollback: bool = False) -> Transaction
+    .. automethod:: transaction
 
         .. note:: It must be called as ``with conn.transaction() as tx: ...``
 
@@ -123,7 +120,6 @@ The `!Connection` class
         or `rollback()`.
 
     .. autoattribute:: autocommit
-        :annotation: bool
 
         The property is writable for sync connections, read-only for async
         ones: you should call ``await`` `~AsyncConnection.set_autocommit`\
@@ -132,7 +128,6 @@ The `!Connection` class
     .. rubric:: Checking and configuring the connection state
 
     .. autoattribute:: client_encoding
-        :annotation: str
 
         The property is writable for sync connections, read-only for async
         ones: you should call ``await`` `~AsyncConnection.set_client_encoding`\
@@ -168,17 +163,14 @@ The `!Connection` class
 
         TODO
 
-
     .. automethod:: fileno
 
     .. autoattribute:: prepare_threshold
-        :annotation: Optional[int]
 
         See :ref:`prepared-statements` for details.
 
 
     .. autoattribute:: prepared_max
-        :annotation: int
 
         If more queries need to be prepared, old ones are deallocated__.
 
@@ -187,6 +179,8 @@ The `!Connection` class
 
     .. rubric:: Methods you can use to do something cool
 
+    .. automethod:: cancel
+
     .. automethod:: notifies
 
         Notifies are recevied after using :sql:`LISTEN` in a connection, when
@@ -194,15 +188,17 @@ The `!Connection` class
         listened channels.
 
     .. automethod:: add_notify_handler
+
+        :param callback: a callable taking a `Notify` parameter.
+
     .. automethod:: remove_notify_handler
 
     See :ref:`async-notify` for details.
 
-    .. automethod:: cancel
     .. automethod:: add_notice_handler
 
-        The argument of the callback is a `~psycopg3.errors.Diagnostic` object
-        containing all the details about the notice.
+        :param callback: a callable taking a `~psycopg3.errors.Diagnostic`
+            object containing all the details about the notice.
 
     .. automethod:: remove_notice_handler
 
@@ -237,7 +233,7 @@ The `!AsyncConnection` class
     .. automethod:: commit
     .. automethod:: rollback
 
-    .. automethod:: transaction(savepoint_name: Optional[str] = None, force_rollback: bool = False) -> AsyncTransaction
+    .. automethod:: transaction
 
         .. note:: It must be called as ``async with conn.transaction() as tx: ...``.
 
@@ -261,10 +257,11 @@ Connection support objects
 
     .. autoproperty:: savepoint_name
     .. autoattribute:: connection
-        :annotation: Connection
 
 .. autoclass:: AsyncTransaction()
 
+    .. autoattribute:: connection
+
 .. autoexception:: Rollback
 
     It can be used as
index 128bf4b2810a2d3cf429093159e0c650ef3f5d36..089e56755459229476afe09f0cc8fe8dbff7c8a3 100644 (file)
@@ -46,7 +46,6 @@ The `!Cursor` class
             to close the cursor automatically when the block is exited.
 
     .. autoattribute:: closed
-        :annotation: bool
 
     .. rubric:: Methods to send commands
 
@@ -127,6 +126,14 @@ The `!Cursor` class
     .. note:: cursors are iterable objects, so just using ``for record in
         cursor`` syntax will iterate on the records in the current recordset.
 
+    .. autoproperty:: row_factory
+
+        The property affects the objects returned by the `fetchone()`,
+        `fetchmany()`, `fetchall()` methods. The default
+        (`~psycopg3.rows.tuple_row`) returns a tuple for each record fetched.
+
+        See :ref:`row-factories` for details.
+
     .. automethod:: fetchone
     .. automethod:: fetchmany
     .. automethod:: fetchall
@@ -141,28 +148,22 @@ The `!Cursor` class
 
     .. rubric:: Information about the data
 
-    .. attribute:: description
-        :type: Optional[List[Column]]
+    .. autoattribute:: description
 
         A list of objects describing each column of the current queryset.
 
         `!None` if the last operation didn't return a queryset.
 
     .. autoattribute:: rowcount
-        :annotation: int
-
     .. autoattribute:: rownumber
-        :annotation: int
 
     .. autoattribute:: query
-        :annotation: Optional[bytes]
 
         The query will be in PostgreSQL format (with ``$1``, ``$2``...
         parameters), the parameters will *not* be merged to the query: see
         `params`.
 
     .. autoattribute:: params
-        :annotation: Optional[List[Optional[bytes]]]
 
         The parameters are adapted to PostgreSQL format.
 
@@ -185,7 +186,6 @@ The `!ServerCursor` class
     documented the differences:
 
     .. autoattribute:: name
-        :annotation: str
 
     .. automethod:: close
 
@@ -239,7 +239,6 @@ The `!ServerCursor` class
             batches of `itersize` to reduce the number of server roundtrips.
 
     .. autoattribute:: itersize
-        :annotation: int
 
         Number of records to fetch at time when iterating on the cursor. The
         default is 100.
index cfc95e9cbd35477f82496fcd8e5c2c80fccec3e9..5a445eeed00aa5e31230e9c6b9d38e4bffb79d89 100644 (file)
@@ -57,7 +57,6 @@ Module content
 --------------
 
 .. autodata:: __impl__
-    :annotation: str:
 
     The choice of implementation is automatic but can be forced setting the
     :envvar:`PSYCOPG3_IMPL` env var.
index 7060245a99a4c3adb1e6038c2ce32bc9e84e153d..1829a15ce2156edc5aad6b8d537419d779b2adcd 100644 (file)
@@ -37,6 +37,7 @@ release = "UNRELEASED"
 extensions = [
     "sphinx.ext.autodoc",
     "sphinx.ext.intersphinx",
+    "sphinx_autodoc_typehints",
     "sql_role",
     "pg3_docs",
     "libpq_docs",
index b580d2eb808c1ae8fff24b339d4d38f18bcad9ae..cf6e677002ed66933461ddd84314b0d3c6e76b18 100644 (file)
@@ -6,6 +6,7 @@ Customisation for docs generation.
 
 import os
 import re
+import logging
 import importlib
 from typing import Dict
 from collections import deque
@@ -39,6 +40,12 @@ def setup(app):
     recover_defined_module(psycopg3)
     monkeypatch_autodoc()
 
+    # Disable warnings in sphinx_autodoc_typehints because it doesn't seem that
+    # there is a workaround for: "WARNING: Cannot resolve forward reference in
+    # type annotations"
+    logger = logging.getLogger("sphinx.sphinx_autodoc_typehints")
+    logger.setLevel(logging.ERROR)
+
 
 # Classes which may have __module__ overwritten
 recovered_classes: Dict[type, str] = {}
index a6049581df1c62dbf3f06ea624ebc7d5449001f7..2f7344f6275da759ad8de80d5786cfbf0a3bf902 100644 (file)
@@ -1,3 +1,4 @@
 Sphinx >= 3.3, < 3.4
 docutils >= 0.16, < 0.17
+sphinx-autodoc-typehints >= 1.12, < 1.13
 furo
index afa82b1fa7ab51c2731300c24ddd7ead0568b7f6..62f08e2637ea224280df76dcf47f5d94637f294d 100644 (file)
@@ -39,7 +39,7 @@ class Dumper(ABC):
             context.connection if context else None
         )
 
-        self.oid = self._oid
+        self.oid: int = self._oid
         """The oid to pass to the server, if known."""
 
     @abstractmethod
index 51e7c1720eb6da6363c43f0e2923f438d3a82141..7492d753c98a7c320fbaefda28bc1935324d4a2d 100644 (file)
@@ -564,6 +564,7 @@ class Connection(BaseConnection):
             transaction. If `!None`, one will be chosen automatically.
         :param force_rollback: Roll back the transaction at the end of the
             block even if there were no error (e.g. to try a no-op process).
+        :rtype: Transaction
         """
         with Transaction(self, savepoint_name, force_rollback) as tx:
             yield tx
@@ -736,6 +737,8 @@ class AsyncConnection(BaseConnection):
     ) -> AsyncIterator[AsyncTransaction]:
         """
         Start a context block with a new transaction or nested transaction.
+
+        :rtype: AsyncTransaction
         """
         tx = AsyncTransaction(self, savepoint_name, force_rollback)
         async with tx:
index 2a8ad986d1712db889ffc4880a3b000de8209bb3..d678338d8b210f42846b89530f721c5454784c99 100644 (file)
@@ -175,6 +175,7 @@ class BaseCursor(Generic[ConnectionType]):
 
     @property
     def row_factory(self) -> RowFactory:
+        """Writable attribute to control how result rows are formed."""
         return self._row_factory
 
     @row_factory.setter
@@ -538,6 +539,8 @@ class Cursor(BaseCursor["Connection"]):
         Return the next record from the current recordset.
 
         Return `!None` the recordset is finished.
+
+        :rtype: Optional[Row], with Row defined by `row_factory`
         """
         self._check_result()
         record = self._tx.load_row(self._pos)
@@ -545,11 +548,13 @@ class Cursor(BaseCursor["Connection"]):
             self._pos += 1
         return record  # type: ignore[no-any-return]
 
-    def fetchmany(self, size: int = 0) -> Sequence[Row]:
+    def fetchmany(self, size: int = 0) -> List[Row]:
         """
         Return the next *size* records from the current recordset.
 
         *size* default to `!self.arraysize` if not specified.
+
+        :rtype: Sequence[Row], with Row defined by `row_factory`
         """
         self._check_result()
         assert self.pgresult
@@ -562,9 +567,11 @@ class Cursor(BaseCursor["Connection"]):
         self._pos += len(records)
         return records
 
-    def fetchall(self) -> Sequence[Row]:
+    def fetchall(self) -> List[Row]:
         """
         Return all the remaining records from the current recordset.
+
+        :rtype: Sequence[Row], with Row defined by `row_factory`
         """
         self._check_result()
         assert self.pgresult
@@ -601,6 +608,8 @@ class Cursor(BaseCursor["Connection"]):
     def copy(self, statement: Query) -> Iterator[Copy]:
         """
         Initiate a :sql:`COPY` operation and return an object to manage it.
+
+        :rtype: Copy
         """
         with self._conn.lock:
             self._conn.wait(self._start_copy_gen(statement))
@@ -704,6 +713,9 @@ class AsyncCursor(BaseCursor["AsyncConnection"]):
 
     @asynccontextmanager
     async def copy(self, statement: Query) -> AsyncIterator[AsyncCopy]:
+        """
+        :rtype: AsyncCopy
+        """
         async with self._conn.lock:
             await self._conn.wait(self._start_copy_gen(statement))
 
index 1ce78a5cf1f86979b7663ac7693f15954ead1119..9cf4d32533ae069287abc189ac06ad7cc3ee05de 100644 (file)
@@ -179,7 +179,7 @@ class ServerCursor(BaseCursor["Connection"]):
         super().__init__(connection, format=format, row_factory=row_factory)
         self._helper: ServerCursorHelper["Connection"]
         self._helper = ServerCursorHelper(name)
-        self.itersize = DEFAULT_ITERSIZE
+        self.itersize: int = DEFAULT_ITERSIZE
 
     def __del__(self) -> None:
         if not self._closed:
@@ -300,7 +300,7 @@ class AsyncServerCursor(BaseCursor["AsyncConnection"]):
         super().__init__(connection, format=format, row_factory=row_factory)
         self._helper: ServerCursorHelper["AsyncConnection"]
         self._helper = ServerCursorHelper(name)
-        self.itersize = DEFAULT_ITERSIZE
+        self.itersize: int = DEFAULT_ITERSIZE
 
     def __del__(self) -> None:
         if not self._closed:
index cdc2b7b65e2344c5fe350a06f5456c4c97c6fcf2..ef05a921abc83af635a1c8c37cfb3ea5892987b4 100644 (file)
@@ -54,11 +54,6 @@ class BaseTransaction(Generic[ConnectionType]):
         self.force_rollback = force_rollback
         self._entered = self._exited = False
 
-    @property
-    def connection(self) -> ConnectionType:
-        """The connection the object is managing."""
-        return self._conn
-
     @property
     def savepoint_name(self) -> Optional[str]:
         """
@@ -183,6 +178,11 @@ class Transaction(BaseTransaction["Connection"]):
 
     __module__ = "psycopg3"
 
+    @property
+    def connection(self) -> "Connection":
+        """The connection the object is managing."""
+        return self._conn
+
     def __enter__(self) -> "Transaction":
         with self._conn.lock:
             self._conn.wait(self._enter_gen())
@@ -205,6 +205,10 @@ class AsyncTransaction(BaseTransaction["AsyncConnection"]):
 
     __module__ = "psycopg3"
 
+    @property
+    def connection(self) -> "AsyncConnection":
+        return self._conn
+
     async def __aenter__(self) -> "AsyncTransaction":
         async with self._conn.lock:
             await self._conn.wait(self._enter_gen())