From 3c229359b044b68b6af9ac2bec33a381c340251d Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 12 Jul 2021 17:54:57 +0200 Subject: [PATCH] proto modules renamed to abc --- docs/advanced/adapt.rst | 16 ++++++------ docs/api/{proto.rst => abc.rst} | 8 +++--- docs/api/adapt.rst | 6 ++--- docs/api/index.rst | 2 +- psycopg/psycopg/_adapters_map.py | 6 ++--- psycopg/psycopg/_preparing.py | 2 +- psycopg/psycopg/_queries.py | 4 +-- psycopg/psycopg/_struct.py | 2 +- psycopg/psycopg/_transform.py | 6 ++--- psycopg/psycopg/_typeinfo.py | 2 +- psycopg/psycopg/_tz.py | 2 +- psycopg/psycopg/{proto.py => abc.py} | 2 +- psycopg/psycopg/adapt.py | 24 +++++++++--------- psycopg/psycopg/connection.py | 5 ++-- psycopg/psycopg/conninfo.py | 2 +- psycopg/psycopg/copy.py | 4 +-- psycopg/psycopg/cursor.py | 6 ++--- psycopg/psycopg/dbapi20.py | 2 +- psycopg/psycopg/errors.py | 3 ++- psycopg/psycopg/generators.py | 4 +-- psycopg/psycopg/pool/base.py | 2 +- psycopg/psycopg/postgres.py | 2 +- psycopg/psycopg/pq/__init__.py | 12 ++++----- psycopg/psycopg/pq/{proto.py => abc.py} | 0 psycopg/psycopg/pq/misc.py | 2 +- psycopg/psycopg/pq/pq_ctypes.py | 14 +++++------ psycopg/psycopg/server_cursor.py | 2 +- psycopg/psycopg/sql.py | 2 +- psycopg/psycopg/transaction.py | 4 +-- psycopg/psycopg/types/array.py | 2 +- psycopg/psycopg/types/bool.py | 2 +- psycopg/psycopg/types/composite.py | 2 +- psycopg/psycopg/types/datetime.py | 2 +- psycopg/psycopg/types/json.py | 2 +- psycopg/psycopg/types/net.py | 2 +- psycopg/psycopg/types/none.py | 2 +- psycopg/psycopg/types/numeric.py | 2 +- psycopg/psycopg/types/range.py | 2 +- psycopg/psycopg/types/string.py | 4 +-- psycopg/psycopg/types/uuid.py | 2 +- psycopg/psycopg/waiting.py | 2 +- psycopg_c/psycopg_c/_psycopg.pyi | 27 +++++++++------------ psycopg_c/psycopg_c/_psycopg/generators.pyx | 8 +++--- tests/adapters_example.py | 2 +- 44 files changed, 102 insertions(+), 109 deletions(-) rename docs/api/{proto.rst => abc.rst} (95%) rename psycopg/psycopg/{proto.py => abc.py} (99%) rename psycopg/psycopg/pq/{proto.py => abc.py} (100%) diff --git a/docs/advanced/adapt.rst b/docs/advanced/adapt.rst index 3e5bc468b..9e7c57eda 100644 --- a/docs/advanced/adapt.rst +++ b/docs/advanced/adapt.rst @@ -17,8 +17,8 @@ returned. adaptation rules. - Adaptation configuration is performed by changing the - `~psycopg.proto.AdaptContext.adapters` object of objects implementing the - `~psycopg.proto.AdaptContext` protocols, for instance `~psycopg.Connection` + `~psycopg.abc.AdaptContext.adapters` object of objects implementing the + `~psycopg.abc.AdaptContext` protocols, for instance `~psycopg.Connection` or `~psycopg.Cursor`. - Every context object derived from another context inherits its adapters @@ -31,21 +31,21 @@ returned. :align: center - The `!adapters` attribute are `AdaptersMap` instances, and contain the - mapping from Python types and `~psycopg.proto.Dumper` classes, and from - PostgreSQL oids to `~psycopg.proto.Loader` classes. Changing this mapping + mapping from Python types and `~psycopg.abc.Dumper` classes, and from + PostgreSQL oids to `~psycopg.abc.Loader` classes. Changing this mapping (e.g. writing and registering your own adapters, or using a different configuration of builtin adapters) affects how types are converted between Python and PostgreSQL. - - Dumpers (objects implementing the `~psycopg.proto.Dumper` protocol) are + - Dumpers (objects implementing the `~psycopg.abc.Dumper` protocol) are the objects used to perform the conversion from a Python object to a bytes sequence in a format understood by PostgreSQL. The string returned *shouldn't be quoted*: the value will be passed to the database using functions such as :pq:`PQexecParams()` so quoting and quotes escaping is not necessary. The dumper usually also suggests the server what type to - use, via its `~psycopg.proto.Dumper.oid` attribute. + use, via its `~psycopg.abc.Dumper.oid` attribute. - - Loaders (objects implementing the `~psycopg.proto.Loader` protocol) are + - Loaders (objects implementing the `~psycopg.abc.Loader` protocol) are the objects used to perform the opposite operation: reading a bytes sequence from PostgreSQL and create a Python object out of it. @@ -141,7 +141,7 @@ and dispatching the values to convert to the right instance. type to use (for instance the PostgreSQL type of a Python list depends on the objects it contains, whether to use an :sql:`integer` or :sql:`bigint` depends on the number size...) In these cases the mechanism provided by - `~psycopg.proto.Dumper.get_key()` and `~psycopg.proto.Dumper.upgrade()` is + `~psycopg.abc.Dumper.get_key()` and `~psycopg.abc.Dumper.upgrade()` is used. - For every OID returned by the query, the `!Transformer` will instantiate a diff --git a/docs/api/proto.rst b/docs/api/abc.rst similarity index 95% rename from docs/api/proto.rst rename to docs/api/abc.rst index 85e0605f2..ad20f26e8 100644 --- a/docs/api/proto.rst +++ b/docs/api/abc.rst @@ -1,12 +1,10 @@ -`proto` -- Psycopg abstract classes -=================================== - -TODO: rename to abc +`!abc` -- Psycopg abstract classes +================================== The module exposes Psycopg definitions which can be used for static type checking. -.. module:: psycopg.proto +.. module:: psycopg.abc .. autoclass:: Dumper(cls, context=None) diff --git a/docs/api/adapt.rst b/docs/api/adapt.rst index 02553f392..c36ea540a 100644 --- a/docs/api/adapt.rst +++ b/docs/api/adapt.rst @@ -23,7 +23,7 @@ Dumpers and loaders This is an abstract base class: subclasses *must* at least implement the `dump()` method and specify the `format`. - The class implements the `~psycopg.proto.Dumper` protocol. + The class implements the `~psycopg.abc.Dumper` protocol. .. automethod:: dump @@ -39,7 +39,7 @@ Dumpers and loaders This is an abstract base class: subclasses *must* at least implement the `!load()` method and specify a `format`. - The class implements the `~psycopg.proto.Loader` protocol. + The class implements the `~psycopg.abc.Loader` protocol. .. automethod:: load @@ -79,4 +79,4 @@ Other objects used in adaptations .. autoclass:: Transformer(context=None) :param context: The context where the transformer should operate. - :type context: `~psycopg.proto.AdaptContext` + :type context: `~psycopg.abc.AdaptContext` diff --git a/docs/api/index.rst b/docs/api/index.rst index 2064ebb4a..44263f272 100644 --- a/docs/api/index.rst +++ b/docs/api/index.rst @@ -11,6 +11,6 @@ Psycopg 3 API errors adapt types - proto + abc pool pq diff --git a/psycopg/psycopg/_adapters_map.py b/psycopg/psycopg/_adapters_map.py index 3bef245f1..b572e7818 100644 --- a/psycopg/psycopg/_adapters_map.py +++ b/psycopg/psycopg/_adapters_map.py @@ -9,8 +9,8 @@ from typing import cast, TYPE_CHECKING from . import pq from . import errors as e +from .abc import Dumper, Loader from ._enums import PyFormat as PyFormat -from .proto import Dumper, Loader from ._cmodule import _psycopg from ._typeinfo import TypesRegistry @@ -23,7 +23,7 @@ RV = TypeVar("RV") class AdaptersMap: r""" Establish how types should be converted between Python and PostgreSQL in - an `~psycopg.proto.AdaptContext`. + an `~psycopg.abc.AdaptContext`. `!AdaptersMap` maps Python types to `~psycopg.adapt.Dumper` classes to define how Python types are converted to PostgreSQL, and maps OIDs to @@ -32,7 +32,7 @@ class AdaptersMap: Every `!AdaptContext` object has an underlying `!AdaptersMap` defining how types are converted in that context, exposed as the - `~psycopg.proto.AdaptContext.adapters` attribute: changing such map allows + `~psycopg.abc.AdaptContext.adapters` attribute: changing such map allows to customise adaptation in a context without changing separated contexts. When a context is created from another context (for instance when a diff --git a/psycopg/psycopg/_preparing.py b/psycopg/psycopg/_preparing.py index c983965f7..13c07901a 100644 --- a/psycopg/psycopg/_preparing.py +++ b/psycopg/psycopg/_preparing.py @@ -12,7 +12,7 @@ from .pq import ExecStatus from ._queries import PostgresQuery if TYPE_CHECKING: - from .pq.proto import PGresult + from .pq.abc import PGresult class Prepare(IntEnum): diff --git a/psycopg/psycopg/_queries.py b/psycopg/psycopg/_queries.py index 4732c1fee..4a492bd3a 100644 --- a/psycopg/psycopg/_queries.py +++ b/psycopg/psycopg/_queries.py @@ -12,11 +12,11 @@ from functools import lru_cache from . import pq from . import errors as e from .sql import Composable -from .proto import Query, Params +from .abc import Query, Params from ._enums import PyFormat if TYPE_CHECKING: - from .proto import Transformer + from .abc import Transformer class QueryPart(NamedTuple): diff --git a/psycopg/psycopg/_struct.py b/psycopg/psycopg/_struct.py index 3bb798abb..e4529ff08 100644 --- a/psycopg/psycopg/_struct.py +++ b/psycopg/psycopg/_struct.py @@ -7,7 +7,7 @@ Utility functions to deal with binary structs. import struct from typing import Callable, cast, Optional, Tuple -from .proto import Buffer +from .abc import Buffer from .compat import Protocol PackInt = Callable[[int], bytes] diff --git a/psycopg/psycopg/_transform.py b/psycopg/psycopg/_transform.py index 11a549ca1..4f7dae357 100644 --- a/psycopg/psycopg/_transform.py +++ b/psycopg/psycopg/_transform.py @@ -11,14 +11,14 @@ from collections import defaultdict from . import pq from . import postgres from . import errors as e +from .abc import LoadFunc, AdaptContext, PyFormat, DumperKey from .rows import Row, RowMaker -from .proto import LoadFunc, AdaptContext, PyFormat, DumperKey from .postgres import INVALID_OID if TYPE_CHECKING: - from .pq.proto import PGresult + from .abc import Dumper, Loader from .adapt import AdaptersMap - from .proto import Dumper, Loader + from .pq.abc import PGresult from .connection import BaseConnection DumperCache = Dict[DumperKey, "Dumper"] diff --git a/psycopg/psycopg/_typeinfo.py b/psycopg/psycopg/_typeinfo.py index e8a15dd3e..1d9f5c89a 100644 --- a/psycopg/psycopg/_typeinfo.py +++ b/psycopg/psycopg/_typeinfo.py @@ -11,8 +11,8 @@ from typing import Any, Callable, Dict, Iterator, Optional from typing import Sequence, Type, TypeVar, Union, TYPE_CHECKING from . import errors as e +from .abc import AdaptContext from .rows import dict_row -from .proto import AdaptContext if TYPE_CHECKING: from .connection import Connection, AsyncConnection diff --git a/psycopg/psycopg/_tz.py b/psycopg/psycopg/_tz.py index 5b34203ad..ac5af7d13 100644 --- a/psycopg/psycopg/_tz.py +++ b/psycopg/psycopg/_tz.py @@ -9,7 +9,7 @@ from typing import Dict, Optional, Union from datetime import timezone, tzinfo from .compat import ZoneInfo -from .pq.proto import PGconn +from .pq.abc import PGconn logger = logging.getLogger("psycopg") diff --git a/psycopg/psycopg/proto.py b/psycopg/psycopg/abc.py similarity index 99% rename from psycopg/psycopg/proto.py rename to psycopg/psycopg/abc.py index 1fa915a4e..a0158e2d3 100644 --- a/psycopg/psycopg/proto.py +++ b/psycopg/psycopg/abc.py @@ -15,7 +15,7 @@ from .compat import Protocol if TYPE_CHECKING: from .sql import Composable from .rows import Row, RowMaker - from .pq.proto import PGresult + from .pq.abc import PGresult from .waiting import Wait, Ready from .connection import BaseConnection from ._adapters_map import AdaptersMap diff --git a/psycopg/psycopg/adapt.py b/psycopg/psycopg/adapt.py index 78196a638..06d38d51c 100644 --- a/psycopg/psycopg/adapt.py +++ b/psycopg/psycopg/adapt.py @@ -7,9 +7,8 @@ Entry point into the adaptation system. from abc import ABC, abstractmethod from typing import Any, Optional, Type, Tuple, Union, TYPE_CHECKING -from . import pq, proto +from . import pq, abc from . import _adapters_map -from .proto import AdaptContext, Buffer as Buffer from ._enums import PyFormat as PyFormat from ._cmodule import _psycopg @@ -17,9 +16,10 @@ if TYPE_CHECKING: from .connection import BaseConnection AdaptersMap = _adapters_map.AdaptersMap +Buffer = abc.Buffer -class Dumper(proto.Dumper, ABC): +class Dumper(abc.Dumper, ABC): """ Convert Python object of the type *cls* to PostgreSQL representation. """ @@ -31,7 +31,7 @@ class Dumper(proto.Dumper, ABC): oid: int """The oid to pass to the server, if known.""" - def __init__(self, cls: type, context: Optional[AdaptContext] = None): + def __init__(self, cls: type, context: Optional[abc.AdaptContext] = None): self.cls = cls self.connection: Optional["BaseConnection[Any]"] = ( context.connection if context else None @@ -63,8 +63,8 @@ class Dumper(proto.Dumper, ABC): self, obj: Any, format: PyFormat ) -> Union[type, Tuple[type, ...]]: """ - Implementation of the `~psycopg.proto.Dumper.get_key()` member of the - `~psycopg.proto.Dumper` protocol. Look at its definition for details. + Implementation of the `~psycopg.abc.Dumper.get_key()` member of the + `~psycopg.abc.Dumper` protocol. Look at its definition for details. This implementation returns the *cls* passed in the constructor. Subclasses needing to specialise the PostgreSQL type according to the @@ -76,8 +76,8 @@ class Dumper(proto.Dumper, ABC): def upgrade(self, obj: Any, format: PyFormat) -> "Dumper": """ - Implementation of the `~psycopg.proto.Dumper.upgrade()` member of the - `~psycopg.proto.Dumper` protocol. Look at its definition for details. + Implementation of the `~psycopg.abc.Dumper.upgrade()` member of the + `~psycopg.abc.Dumper` protocol. Look at its definition for details. This implementation just returns *self*. If a subclass implements `get_key()` it should probably override `!upgrade()` too. @@ -92,7 +92,7 @@ class Loader(ABC): format: pq.Format - def __init__(self, oid: int, context: Optional[AdaptContext] = None): + def __init__(self, oid: int, context: Optional[abc.AdaptContext] = None): self.oid = oid self.connection: Optional["BaseConnection[Any]"] = ( context.connection if context else None @@ -104,7 +104,7 @@ class Loader(ABC): ... -Transformer: Type["proto.Transformer"] +Transformer: Type["abc.Transformer"] # Override it with fast object if available if _psycopg: @@ -118,7 +118,7 @@ else: class RecursiveDumper(Dumper): """Dumper with a transformer to help dumping recursive types.""" - def __init__(self, cls: type, context: Optional[AdaptContext] = None): + def __init__(self, cls: type, context: Optional[abc.AdaptContext] = None): super().__init__(cls, context) self._tx = Transformer(context) @@ -126,6 +126,6 @@ class RecursiveDumper(Dumper): class RecursiveLoader(Loader): """Loader with a transformer to help loading recursive types.""" - def __init__(self, oid: int, context: Optional[AdaptContext] = None): + def __init__(self, oid: int, context: Optional[abc.AdaptContext] = None): super().__init__(oid, context) self._tx = Transformer(context) diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index c00479641..64a158d49 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -23,10 +23,9 @@ from . import waiting from . import postgres from . import encodings from .pq import ConnStatus, ExecStatus, TransactionStatus, Format +from .abc import ConnectionType, Params, PQGen, PQGenConn, Query, RV from .sql import Composable from .rows import Row, RowFactory, tuple_row, TupleRow -from .proto import ConnectionType, Params, PQGen, PQGenConn -from .proto import Query, RV from .compat import asynccontextmanager from .cursor import Cursor, AsyncCursor from ._cmodule import _psycopg @@ -46,7 +45,7 @@ execute: Callable[["PGconn"], PQGen[List["PGresult"]]] CursorRow = TypeVar("CursorRow") if TYPE_CHECKING: - from .pq.proto import PGconn, PGresult + from .pq.abc import PGconn, PGresult from .pool.base import BasePool if _psycopg: diff --git a/psycopg/psycopg/conninfo.py b/psycopg/psycopg/conninfo.py index 469117dde..3d665c94b 100644 --- a/psycopg/psycopg/conninfo.py +++ b/psycopg/psycopg/conninfo.py @@ -116,7 +116,7 @@ class ConnectionInfo: __module__ = "psycopg" - def __init__(self, pgconn: pq.proto.PGconn): + def __init__(self, pgconn: pq.abc.PGconn): self.pgconn = pgconn @property diff --git a/psycopg/psycopg/copy.py b/psycopg/psycopg/copy.py index 5829189bd..c933e7e89 100644 --- a/psycopg/psycopg/copy.py +++ b/psycopg/psycopg/copy.py @@ -17,14 +17,14 @@ from typing import Any, Dict, List, Match, Optional, Sequence, Type, Tuple from . import pq from . import errors as e from .pq import ExecStatus +from .abc import ConnectionType, PQGen, Transformer from .adapt import PyFormat -from .proto import ConnectionType, PQGen, Transformer from .compat import create_task from ._cmodule import _psycopg from .generators import copy_from, copy_to, copy_end if TYPE_CHECKING: - from .pq.proto import PGresult + from .pq.abc import PGresult from .cursor import BaseCursor, Cursor, AsyncCursor from .connection import Connection, AsyncConnection # noqa: F401 diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 3fa4ac93c..4e1946524 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -16,9 +16,9 @@ from . import errors as e from . import generators from .pq import ExecStatus, Format +from .abc import ConnectionType, Query, Params, PQGen from .copy import Copy, AsyncCopy from .rows import Row, RowFactory -from .proto import ConnectionType, Query, Params, PQGen from .compat import asynccontextmanager from ._column import Column from ._cmodule import _psycopg @@ -26,8 +26,8 @@ from ._queries import PostgresQuery from ._preparing import Prepare if TYPE_CHECKING: - from .proto import Transformer - from .pq.proto import PGconn, PGresult + from .abc import Transformer + from .pq.abc import PGconn, PGresult from .connection import BaseConnection # noqa: F401 from .connection import Connection, AsyncConnection # noqa: F401 diff --git a/psycopg/psycopg/dbapi20.py b/psycopg/psycopg/dbapi20.py index a6a75db22..78f8f66fc 100644 --- a/psycopg/psycopg/dbapi20.py +++ b/psycopg/psycopg/dbapi20.py @@ -11,8 +11,8 @@ from typing import Any, Optional, Sequence from . import postgres from .pq import Format, Escaping +from .abc import AdaptContext from .adapt import Dumper -from .proto import AdaptContext class DBAPITypeObject: diff --git a/psycopg/psycopg/errors.py b/psycopg/psycopg/errors.py index 80e36c5b6..9daa4a379 100644 --- a/psycopg/psycopg/errors.py +++ b/psycopg/psycopg/errors.py @@ -20,7 +20,8 @@ DBAPI-defined Exceptions are defined in the following hierarchy:: from typing import Any, Callable, Dict, Optional, Sequence, Tuple, Type, Union from typing import cast -from psycopg.pq.proto import PGresult + +from psycopg.pq.abc import PGresult from psycopg.pq._enums import DiagnosticField diff --git a/psycopg/psycopg/generators.py b/psycopg/psycopg/generators.py index 95b2994d4..86c13c288 100644 --- a/psycopg/psycopg/generators.py +++ b/psycopg/psycopg/generators.py @@ -21,10 +21,10 @@ from typing import List, Optional, Union from . import pq from . import errors as e from .pq import ConnStatus, PollingStatus, ExecStatus -from .proto import PQGen, PQGenConn +from .abc import PQGen, PQGenConn from .waiting import Wait, Ready from .encodings import py_codecs -from .pq.proto import PGconn, PGresult +from .pq.abc import PGconn, PGresult logger = logging.getLogger(__name__) diff --git a/psycopg/psycopg/pool/base.py b/psycopg/psycopg/pool/base.py index f652ecfe5..55c25301c 100644 --- a/psycopg/psycopg/pool/base.py +++ b/psycopg/psycopg/pool/base.py @@ -9,7 +9,7 @@ from typing import Any, Callable, Deque, Dict, Generic, Optional from typing import TYPE_CHECKING from collections import Counter, deque -from ..proto import ConnectionType +from ..abc import ConnectionType if TYPE_CHECKING: from typing import Counter as TCounter diff --git a/psycopg/psycopg/postgres.py b/psycopg/psycopg/postgres.py index c48e13996..396e409d4 100644 --- a/psycopg/psycopg/postgres.py +++ b/psycopg/psycopg/postgres.py @@ -5,7 +5,7 @@ Types configuration specific to PostgreSQL. # Copyright (C) 2020-2021 The Psycopg Team from ._typeinfo import TypeInfo, RangeInfo, TypesRegistry -from .proto import AdaptContext +from .abc import AdaptContext from ._adapters_map import AdaptersMap # Global objects with PostgreSQL builtins and globally registered user types. diff --git a/psycopg/psycopg/pq/__init__.py b/psycopg/psycopg/pq/__init__.py index 50773d92e..e78f6b6ec 100644 --- a/psycopg/psycopg/pq/__init__.py +++ b/psycopg/psycopg/pq/__init__.py @@ -13,11 +13,11 @@ import os import logging from typing import Callable, List, Type +from . import abc from .misc import ConninfoOption, PGnotify, PGresAttDesc from .misc import error_message from ._enums import ConnStatus, DiagnosticField, ExecStatus, Format from ._enums import Ping, PollingStatus, TransactionStatus -from . import proto logger = logging.getLogger(__name__) @@ -28,11 +28,11 @@ Possible values include ``python``, ``c``, ``binary``. """ version: Callable[[], int] -PGconn: Type[proto.PGconn] -PGresult: Type[proto.PGresult] -Conninfo: Type[proto.Conninfo] -Escaping: Type[proto.Escaping] -PGcancel: Type[proto.PGcancel] +PGconn: Type[abc.PGconn] +PGresult: Type[abc.PGresult] +Conninfo: Type[abc.Conninfo] +Escaping: Type[abc.Escaping] +PGcancel: Type[abc.PGcancel] def import_from_libpq() -> None: diff --git a/psycopg/psycopg/pq/proto.py b/psycopg/psycopg/pq/abc.py similarity index 100% rename from psycopg/psycopg/pq/proto.py rename to psycopg/psycopg/pq/abc.py diff --git a/psycopg/psycopg/pq/misc.py b/psycopg/psycopg/pq/misc.py index ae145fdb2..b5f2c63de 100644 --- a/psycopg/psycopg/pq/misc.py +++ b/psycopg/psycopg/pq/misc.py @@ -6,8 +6,8 @@ Various functionalities to make easier to work with the libpq. from typing import cast, NamedTuple, Optional, Union +from .abc import PGconn, PGresult from ._enums import ConnStatus, TransactionStatus -from .proto import PGconn, PGresult class PGnotify(NamedTuple): diff --git a/psycopg/psycopg/pq/pq_ctypes.py b/psycopg/psycopg/pq/pq_ctypes.py index 6f4341b63..bf632d6fd 100644 --- a/psycopg/psycopg/pq/pq_ctypes.py +++ b/psycopg/psycopg/pq/pq_ctypes.py @@ -25,7 +25,7 @@ from .misc import error_message, connection_summary from ._enums import Format, ExecStatus if TYPE_CHECKING: - from . import proto + from . import abc __impl__ = "python" @@ -70,9 +70,7 @@ class PGconn: def __init__(self, pgconn_ptr: impl.PGconn_struct): self._pgconn_ptr: Optional[impl.PGconn_struct] = pgconn_ptr - self.notice_handler: Optional[ - Callable[["proto.PGresult"], None] - ] = None + self.notice_handler: Optional[Callable[["abc.PGresult"], None]] = None self.notify_handler: Optional[Callable[[PGnotify], None]] = None self._notice_receiver = impl.PQnoticeReceiver( # type: ignore @@ -881,7 +879,7 @@ class Escaping: def __init__(self, conn: Optional[PGconn] = None): self.conn = conn - def escape_literal(self, data: "proto.Buffer") -> memoryview: + def escape_literal(self, data: "abc.Buffer") -> memoryview: if not self.conn: raise e.OperationalError( "escape_literal failed: no connection provided" @@ -900,7 +898,7 @@ class Escaping: impl.PQfreemem(out) return memoryview(rv) - def escape_identifier(self, data: "proto.Buffer") -> memoryview: + def escape_identifier(self, data: "abc.Buffer") -> memoryview: if not self.conn: raise e.OperationalError( "escape_identifier failed: no connection provided" @@ -919,7 +917,7 @@ class Escaping: impl.PQfreemem(out) return memoryview(rv) - def escape_string(self, data: "proto.Buffer") -> memoryview: + def escape_string(self, data: "abc.Buffer") -> memoryview: if not isinstance(data, bytes): data = bytes(data) @@ -950,7 +948,7 @@ class Escaping: return memoryview(out.value) - def escape_bytea(self, data: "proto.Buffer") -> memoryview: + def escape_bytea(self, data: "abc.Buffer") -> memoryview: len_out = c_size_t() # TODO: might be able to do without a copy but it's a mess. # the C library does it better anyway, so maybe not worth optimising diff --git a/psycopg/psycopg/server_cursor.py b/psycopg/psycopg/server_cursor.py index 39e2e0155..2367c6803 100644 --- a/psycopg/psycopg/server_cursor.py +++ b/psycopg/psycopg/server_cursor.py @@ -12,8 +12,8 @@ from typing import Sequence, Type, TYPE_CHECKING from . import pq from . import sql from . import errors as e +from .abc import ConnectionType, Query, Params, PQGen from .rows import Row, RowFactory -from .proto import ConnectionType, Query, Params, PQGen from .cursor import BaseCursor, execute if TYPE_CHECKING: diff --git a/psycopg/psycopg/sql.py b/psycopg/psycopg/sql.py index 740c35dcb..abc5bc4bb 100644 --- a/psycopg/psycopg/sql.py +++ b/psycopg/psycopg/sql.py @@ -10,8 +10,8 @@ from abc import ABC, abstractmethod from typing import Any, Iterator, List, Optional, Sequence, Union from .pq import Escaping +from .abc import AdaptContext from .adapt import Transformer, PyFormat -from .proto import AdaptContext def quote(obj: Any, context: Optional[AdaptContext] = None) -> str: diff --git a/psycopg/psycopg/transaction.py b/psycopg/psycopg/transaction.py index e3e54d2a0..2804e1e6f 100644 --- a/psycopg/psycopg/transaction.py +++ b/psycopg/psycopg/transaction.py @@ -12,8 +12,8 @@ from typing import Generic, Optional, Type, Union, TYPE_CHECKING from . import pq from . import sql from .pq import TransactionStatus -from .proto import ConnectionType, PQGen -from .pq.proto import PGresult +from .abc import ConnectionType, PQGen +from .pq.abc import PGresult if TYPE_CHECKING: from typing import Any # noqa: F401 diff --git a/psycopg/psycopg/types/array.py b/psycopg/psycopg/types/array.py index 1cc09148f..740c803c9 100644 --- a/psycopg/psycopg/types/array.py +++ b/psycopg/psycopg/types/array.py @@ -12,8 +12,8 @@ from typing import cast from .. import pq from .. import errors as e from .. import postgres +from ..abc import AdaptContext, Buffer, Dumper, DumperKey from ..adapt import RecursiveDumper, RecursiveLoader, PyFormat -from ..proto import AdaptContext, Buffer, Dumper, DumperKey from .._struct import pack_len, unpack_len from ..postgres import TEXT_OID, INVALID_OID from .._typeinfo import TypeInfo diff --git a/psycopg/psycopg/types/bool.py b/psycopg/psycopg/types/bool.py index c9b9de8a7..47b77fb17 100644 --- a/psycopg/psycopg/types/bool.py +++ b/psycopg/psycopg/types/bool.py @@ -6,8 +6,8 @@ Adapters for booleans. from .. import postgres from ..pq import Format +from ..abc import AdaptContext from ..adapt import Buffer, Dumper, Loader -from ..proto import AdaptContext class BoolDumper(Dumper): diff --git a/psycopg/psycopg/types/composite.py b/psycopg/psycopg/types/composite.py index 5ea8613a9..5cc567efe 100644 --- a/psycopg/psycopg/types/composite.py +++ b/psycopg/psycopg/types/composite.py @@ -12,8 +12,8 @@ from typing import Sequence, Tuple, Type from .. import pq from .. import postgres +from ..abc import AdaptContext, Buffer from ..adapt import PyFormat, RecursiveDumper, RecursiveLoader -from ..proto import AdaptContext, Buffer from .._struct import unpack_len from ..postgres import TEXT_OID from .._typeinfo import CompositeInfo as CompositeInfo # exported here diff --git a/psycopg/psycopg/types/datetime.py b/psycopg/psycopg/types/datetime.py index e9317f6ef..e73af2c43 100644 --- a/psycopg/psycopg/types/datetime.py +++ b/psycopg/psycopg/types/datetime.py @@ -13,8 +13,8 @@ from typing import Any, Callable, cast, Optional, Tuple, TYPE_CHECKING from .. import postgres from ..pq import Format from .._tz import get_tzinfo +from ..abc import AdaptContext, DumperKey from ..adapt import Buffer, Dumper, Loader, PyFormat -from ..proto import AdaptContext, DumperKey from ..errors import InterfaceError, DataError from .._struct import pack_int4, pack_int8, unpack_int4, unpack_int8 diff --git a/psycopg/psycopg/types/json.py b/psycopg/psycopg/types/json.py index 88e692265..efb9b8763 100644 --- a/psycopg/psycopg/types/json.py +++ b/psycopg/psycopg/types/json.py @@ -9,8 +9,8 @@ from typing import Any, Callable, Optional, Type, Union from .. import postgres from ..pq import Format +from ..abc import AdaptContext from ..adapt import Buffer, Dumper, Loader -from ..proto import AdaptContext from ..errors import DataError JsonDumpsFunction = Callable[[Any], str] diff --git a/psycopg/psycopg/types/net.py b/psycopg/psycopg/types/net.py index e3e844ea3..96cf1c76a 100644 --- a/psycopg/psycopg/types/net.py +++ b/psycopg/psycopg/types/net.py @@ -8,8 +8,8 @@ from typing import Callable, Optional, Type, Union, TYPE_CHECKING from .. import postgres from ..pq import Format +from ..abc import AdaptContext from ..adapt import Buffer, Dumper, Loader -from ..proto import AdaptContext if TYPE_CHECKING: import ipaddress diff --git a/psycopg/psycopg/types/none.py b/psycopg/psycopg/types/none.py index 6f9fdc2b0..958a20c34 100644 --- a/psycopg/psycopg/types/none.py +++ b/psycopg/psycopg/types/none.py @@ -5,8 +5,8 @@ Adapters for None. # Copyright (C) 2020-2021 The Psycopg Team from ..pq import Format +from ..abc import AdaptContext from ..adapt import Dumper -from ..proto import AdaptContext class NoneDumper(Dumper): diff --git a/psycopg/psycopg/types/numeric.py b/psycopg/psycopg/types/numeric.py index 72e218f31..93fa217bb 100644 --- a/psycopg/psycopg/types/numeric.py +++ b/psycopg/psycopg/types/numeric.py @@ -12,8 +12,8 @@ from decimal import Decimal, DefaultContext, Context from .. import postgres from .. import errors as e from ..pq import Format +from ..abc import AdaptContext from ..adapt import Buffer, Dumper, Loader, PyFormat -from ..proto import AdaptContext from .._struct import pack_int2, pack_uint2, unpack_int2 from .._struct import pack_int4, pack_uint4, unpack_int4, unpack_uint4 from .._struct import pack_int8, unpack_int8 diff --git a/psycopg/psycopg/types/range.py b/psycopg/psycopg/types/range.py index 2be4806ba..41e89806f 100644 --- a/psycopg/psycopg/types/range.py +++ b/psycopg/psycopg/types/range.py @@ -12,8 +12,8 @@ from datetime import date, datetime from .. import postgres from ..pq import Format +from ..abc import AdaptContext, Buffer, Dumper, DumperKey from ..adapt import RecursiveDumper, RecursiveLoader, PyFormat -from ..proto import AdaptContext, Buffer, Dumper, DumperKey from .._struct import pack_len, unpack_len from ..postgres import INVALID_OID from .._typeinfo import RangeInfo as RangeInfo # exported here diff --git a/psycopg/psycopg/types/string.py b/psycopg/psycopg/types/string.py index 8c1f6a16c..de02b42ab 100644 --- a/psycopg/psycopg/types/string.py +++ b/psycopg/psycopg/types/string.py @@ -8,12 +8,12 @@ from typing import Optional, Union, TYPE_CHECKING from .. import postgres from ..pq import Format, Escaping +from ..abc import AdaptContext from ..adapt import Buffer, Dumper, Loader -from ..proto import AdaptContext from ..errors import DataError if TYPE_CHECKING: - from ..pq.proto import Escaping as EscapingProto + from ..pq.abc import Escaping as EscapingProto class _StrDumper(Dumper): diff --git a/psycopg/psycopg/types/uuid.py b/psycopg/psycopg/types/uuid.py index 917e25366..133666c5c 100644 --- a/psycopg/psycopg/types/uuid.py +++ b/psycopg/psycopg/types/uuid.py @@ -8,8 +8,8 @@ from typing import Callable, Optional, TYPE_CHECKING from .. import postgres from ..pq import Format +from ..abc import AdaptContext from ..adapt import Buffer, Dumper, Loader -from ..proto import AdaptContext if TYPE_CHECKING: import uuid diff --git a/psycopg/psycopg/waiting.py b/psycopg/psycopg/waiting.py index f4a779cfe..2c2f75bc9 100644 --- a/psycopg/psycopg/waiting.py +++ b/psycopg/psycopg/waiting.py @@ -17,7 +17,7 @@ from asyncio import get_event_loop, wait_for, Event, TimeoutError from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE from . import errors as e -from .proto import PQGen, PQGenConn, RV +from .abc import PQGen, PQGenConn, RV class Wait(IntEnum): diff --git a/psycopg_c/psycopg_c/_psycopg.pyi b/psycopg_c/psycopg_c/_psycopg.pyi index f0e9846d0..e4f6b3e14 100644 --- a/psycopg_c/psycopg_c/_psycopg.pyi +++ b/psycopg_c/psycopg_c/_psycopg.pyi @@ -10,15 +10,14 @@ information. Will submit a bug. from typing import Any, Iterable, List, Optional, Sequence, Tuple from psycopg import pq -from psycopg import proto +from psycopg import abc from psycopg.rows import Row, RowMaker from psycopg.adapt import AdaptersMap, PyFormat -from psycopg.proto import Dumper, Loader -from psycopg.pq.proto import PGconn, PGresult +from psycopg.pq.abc import PGconn, PGresult from psycopg.connection import BaseConnection -class Transformer(proto.AdaptContext): - def __init__(self, context: Optional[proto.AdaptContext] = None): ... +class Transformer(abc.AdaptContext): + def __init__(self, context: Optional[abc.AdaptContext] = None): ... @property def connection(self) -> Optional[BaseConnection[Any]]: ... @property @@ -34,7 +33,7 @@ class Transformer(proto.AdaptContext): def dump_sequence( self, params: Sequence[Any], formats: Sequence[PyFormat] ) -> Tuple[List[Any], Tuple[int, ...], Sequence[pq.Format]]: ... - def get_dumper(self, obj: Any, format: PyFormat) -> Dumper: ... + def get_dumper(self, obj: Any, format: PyFormat) -> abc.Dumper: ... def load_rows( self, row0: int, row1: int, make_row: RowMaker[Row] ) -> List[Row]: ... @@ -42,22 +41,20 @@ class Transformer(proto.AdaptContext): def load_sequence( self, record: Sequence[Optional[bytes]] ) -> Tuple[Any, ...]: ... - def get_loader(self, oid: int, format: pq.Format) -> Loader: ... + def get_loader(self, oid: int, format: pq.Format) -> abc.Loader: ... # Generators -def connect(conninfo: str) -> proto.PQGenConn[PGconn]: ... -def execute(pgconn: PGconn) -> proto.PQGen[List[PGresult]]: ... +def connect(conninfo: str) -> abc.PQGenConn[PGconn]: ... +def execute(pgconn: PGconn) -> abc.PQGen[List[PGresult]]: ... # Copy support def format_row_text( - row: Sequence[Any], tx: proto.Transformer, out: Optional[bytearray] = None + row: Sequence[Any], tx: abc.Transformer, out: Optional[bytearray] = None ) -> bytearray: ... def format_row_binary( - row: Sequence[Any], tx: proto.Transformer, out: Optional[bytearray] = None + row: Sequence[Any], tx: abc.Transformer, out: Optional[bytearray] = None ) -> bytearray: ... -def parse_row_text(data: bytes, tx: proto.Transformer) -> Tuple[Any, ...]: ... -def parse_row_binary( - data: bytes, tx: proto.Transformer -) -> Tuple[Any, ...]: ... +def parse_row_text(data: bytes, tx: abc.Transformer) -> Tuple[Any, ...]: ... +def parse_row_binary(data: bytes, tx: abc.Transformer) -> Tuple[Any, ...]: ... # vim: set syntax=python: diff --git a/psycopg_c/psycopg_c/_psycopg/generators.pyx b/psycopg_c/psycopg_c/_psycopg/generators.pyx index 2a1e5f319..78a91fbc2 100644 --- a/psycopg_c/psycopg_c/_psycopg/generators.pyx +++ b/psycopg_c/psycopg_c/_psycopg/generators.pyx @@ -10,8 +10,8 @@ import logging from typing import List from psycopg import errors as e -from psycopg.pq import proto, error_message -from psycopg.proto import PQGen +from psycopg.pq import abc, error_message +from psycopg.abc import PQGen from psycopg.waiting import Wait, Ready cdef object WAIT_W = Wait.W @@ -19,7 +19,7 @@ cdef object WAIT_R = Wait.R cdef object WAIT_RW = Wait.RW cdef int READY_R = Ready.R -def connect(conninfo: str) -> PQGenConn[proto.PGconn]: +def connect(conninfo: str) -> PQGenConn[abc.PGconn]: """ Generator to create a database connection without blocking. @@ -55,7 +55,7 @@ def connect(conninfo: str) -> PQGenConn[proto.PGconn]: return conn -def execute(pq.PGconn pgconn) -> PQGen[List[proto.PGresult]]: +def execute(pq.PGconn pgconn) -> PQGen[List[abc.PGresult]]: """ Generator sending a query and returning results without blocking. diff --git a/tests/adapters_example.py b/tests/adapters_example.py index 57ff38091..6460e76f9 100644 --- a/tests/adapters_example.py +++ b/tests/adapters_example.py @@ -1,7 +1,7 @@ from typing import Optional, Tuple, Union from psycopg import pq -from psycopg.proto import Dumper, Loader, AdaptContext, PyFormat, Buffer +from psycopg.abc import Dumper, Loader, AdaptContext, PyFormat, Buffer def f() -> None: -- 2.47.3