From 47d73ce63e8bca46676aeeee4955539f36043d9d Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 24 Nov 2020 02:07:00 +0000 Subject: [PATCH] Other classes have their __module__ specified These required jumping through hoop to have mypy and documentation agreeing, now it's more natural. --- docs/lib/pg3_docs.py | 2 -- psycopg3/psycopg3/_transform.py | 2 ++ psycopg3/psycopg3/connection.py | 28 +++++++++++++++------------- psycopg3/psycopg3/copy.py | 4 ++++ psycopg3/psycopg3/cursor.py | 7 +++++++ psycopg3/psycopg3/pq/pq_ctypes.py | 7 +++++++ psycopg3/psycopg3/transaction.py | 6 ++++++ 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/docs/lib/pg3_docs.py b/docs/lib/pg3_docs.py index ae73dbe60..cdb219ff0 100644 --- a/docs/lib/pg3_docs.py +++ b/docs/lib/pg3_docs.py @@ -15,8 +15,6 @@ def before_process_signature(app, obj, bound_method): # Drop "return: None" from the function signatures if ann["return"] is None: del ann["return"] - elif ann["return"] == "PGcancel": - ann["return"] = "psycopg3.pq.PGcancel" def process_signature( diff --git a/psycopg3/psycopg3/_transform.py b/psycopg3/psycopg3/_transform.py index 360a945a4..4fb23043d 100644 --- a/psycopg3/psycopg3/_transform.py +++ b/psycopg3/psycopg3/_transform.py @@ -31,6 +31,8 @@ class Transformer: state so adapting several values of the same type can be optimised. """ + __module__ = "psycopg3.adapt" + def __init__(self, context: AdaptContext = None): self._dumpers: DumpersMap self._loaders: LoadersMap diff --git a/psycopg3/psycopg3/connection.py b/psycopg3/psycopg3/connection.py index 48097661f..8813dcb34 100644 --- a/psycopg3/psycopg3/connection.py +++ b/psycopg3/psycopg3/connection.py @@ -39,7 +39,7 @@ connect: Callable[[str], PQGen["PGconn"]] execute: Callable[["PGconn"], PQGen[List["PGresult"]]] if TYPE_CHECKING: - import psycopg3 + from .cursor import AsyncCursor, Cursor from .pq.proto import PGconn, PGresult if pq.__impl__ == "c": @@ -68,8 +68,10 @@ class Notify(NamedTuple): """The PID of the backend process which sent the notification.""" +Notify.__module__ = "psycopg3" + NoticeHandler = Callable[[e.Diagnostic], None] -NotifyHandler = Callable[["psycopg3.Notify"], None] +NotifyHandler = Callable[[Notify], None] class BaseConnection: @@ -96,9 +98,7 @@ class BaseConnection: ConnStatus = pq.ConnStatus TransactionStatus = pq.TransactionStatus - cursor_factory: Union[ - Type["psycopg3.Cursor"], Type["psycopg3.AsyncCursor"] - ] + cursor_factory: Union[Type["Cursor"], Type["AsyncCursor"]] def __init__(self, pgconn: "PGconn"): self.pgconn = pgconn # TODO: document this @@ -229,7 +229,9 @@ class Connection(BaseConnection): Wrapper for a connection to the database. """ - cursor_factory: Type["psycopg3.Cursor"] + __module__ = "psycopg3" + + cursor_factory: Type["Cursor"] def __init__(self, pgconn: "PGconn"): super().__init__(pgconn) @@ -273,9 +275,7 @@ class Connection(BaseConnection): """Close the database connection.""" self.pgconn.finish() - def cursor( - self, name: str = "", format: Format = Format.TEXT - ) -> "psycopg3.Cursor": + def cursor(self, name: str = "", format: Format = Format.TEXT) -> "Cursor": """ Return a new `Cursor` to send commands and queries to the connection. """ @@ -370,7 +370,7 @@ class Connection(BaseConnection): result, encoding=self.client_encoding ) - def notifies(self) -> Iterator["psycopg3.Notify"]: + def notifies(self) -> Iterator[Notify]: """ Yield `Notify` objects as soon as they are received from the database. """ @@ -394,7 +394,9 @@ class AsyncConnection(BaseConnection): Asynchronous wrapper for a connection to the database. """ - cursor_factory: Type["psycopg3.AsyncCursor"] + __module__ = "psycopg3" + + cursor_factory: Type["AsyncCursor"] def __init__(self, pgconn: "PGconn"): super().__init__(pgconn) @@ -433,7 +435,7 @@ class AsyncConnection(BaseConnection): async def cursor( self, name: str = "", format: Format = Format.TEXT - ) -> "psycopg3.AsyncCursor": + ) -> "AsyncCursor": """ Return a new `AsyncCursor` to send commands and queries to the connection. """ @@ -529,7 +531,7 @@ class AsyncConnection(BaseConnection): result, encoding=self.client_encoding ) - async def notifies(self) -> AsyncIterator["psycopg3.Notify"]: + async def notifies(self) -> AsyncIterator[Notify]: while 1: async with self.lock: ns = await self.wait(notifies(self.pgconn)) diff --git a/psycopg3/psycopg3/copy.py b/psycopg3/psycopg3/copy.py index 7165120fc..931bddb0a 100644 --- a/psycopg3/psycopg3/copy.py +++ b/psycopg3/psycopg3/copy.py @@ -127,6 +127,8 @@ _bsrepl_re = re.compile(b"[\b\t\n\v\f\r\\\\]") class Copy(BaseCopy["Connection"]): """Manage a :sql:`COPY` operation.""" + __module__ = "psycopg3" + def read(self) -> bytes: """Read a row of data after a :sql:`COPY TO` operation. @@ -204,6 +206,8 @@ class Copy(BaseCopy["Connection"]): class AsyncCopy(BaseCopy["AsyncConnection"]): """Manage an asynchronous :sql:`COPY` operation.""" + __module__ = "psycopg3" + async def read(self) -> bytes: if self._finished: return b"" diff --git a/psycopg3/psycopg3/cursor.py b/psycopg3/psycopg3/cursor.py index 7bd54d20d..7c424d5c8 100644 --- a/psycopg3/psycopg3/cursor.py +++ b/psycopg3/psycopg3/cursor.py @@ -43,6 +43,9 @@ else: class Column(Sequence[Any]): + + __module__ = "psycopg3" + def __init__(self, pgresult: "PGresult", index: int, encoding: str): self._pgresult = pgresult self._index = index @@ -413,6 +416,8 @@ class BaseCursor(Generic[ConnectionType]): class Cursor(BaseCursor["Connection"]): + __module__ = "psycopg3" + def __enter__(self) -> "Cursor": return self @@ -549,6 +554,8 @@ class Cursor(BaseCursor["Connection"]): class AsyncCursor(BaseCursor["AsyncConnection"]): + __module__ = "psycopg3" + async def __aenter__(self) -> "AsyncCursor": return self diff --git a/psycopg3/psycopg3/pq/pq_ctypes.py b/psycopg3/psycopg3/pq/pq_ctypes.py index 7d27dc633..9cb8bcad7 100644 --- a/psycopg3/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/psycopg3/pq/pq_ctypes.py @@ -58,6 +58,7 @@ class PGconn: Python representation of a libpq connection. """ + __module__ = "psycopg3.pq" __slots__ = ( "pgconn_ptr", "notice_handler", @@ -578,6 +579,7 @@ class PGresult: Python representation of a libpq result. """ + __module__ = "psycopg3.pq" __slots__ = ("pgresult_ptr",) def __init__(self, pgresult_ptr: impl.PGresult_struct): @@ -689,6 +691,7 @@ class PGcancel: Created by `PGconn.get_cancel()`. """ + __module__ = "psycopg3.pq" __slots__ = ("pgcancel_ptr",) def __init__(self, pgcancel_ptr: impl.PGcancel_struct): @@ -729,6 +732,8 @@ class Conninfo: Utility object to manipulate connection strings. """ + __module__ = "psycopg3.pq" + @classmethod def get_defaults(cls) -> List[ConninfoOption]: opts = impl.PQconndefaults() @@ -780,6 +785,8 @@ class Escaping: Utility object to escape strings for SQL interpolation. """ + __module__ = "psycopg3.pq" + def __init__(self, conn: Optional[PGconn] = None): self.conn = conn diff --git a/psycopg3/psycopg3/transaction.py b/psycopg3/psycopg3/transaction.py index b42ed81ea..8130e44ac 100644 --- a/psycopg3/psycopg3/transaction.py +++ b/psycopg3/psycopg3/transaction.py @@ -28,6 +28,8 @@ class Rollback(Exception): enclosing transactions contexts up to and including the one specified. """ + __module__ = "psycopg3" + def __init__( self, transaction: Union["Transaction", "AsyncTransaction", None] = None, @@ -147,6 +149,8 @@ class Transaction(BaseTransaction["Connection"]): Returned by `Connection.transaction()` to handle a transaction block. """ + __module__ = "psycopg3" + def __enter__(self) -> "Transaction": with self._conn.lock: self._execute(self._enter_commands()) @@ -193,6 +197,8 @@ class AsyncTransaction(BaseTransaction["AsyncConnection"]): Returned by `AsyncConnection.transaction()` to handle a transaction block. """ + __module__ = "psycopg3" + async def __aenter__(self) -> "AsyncTransaction": async with self._conn.lock: await self._execute(self._enter_commands()) -- 2.47.2