From: Daniele Varrazzo Date: Sat, 10 Jul 2021 15:42:30 +0000 (+0200) Subject: Rename adapt.Format to PyFormat X-Git-Tag: 3.0.dev1~28^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e2a12098a51fad396c2334061ff8e38c959762f4;p=thirdparty%2Fpsycopg.git Rename adapt.Format to PyFormat --- diff --git a/docs/advanced/adapt.rst b/docs/advanced/adapt.rst index 6958132f7..e24dcc615 100644 --- a/docs/advanced/adapt.rst +++ b/docs/advanced/adapt.rst @@ -133,7 +133,7 @@ right instance. :sql:`integer`, :sql:`bigint`, :sql:`smallint` according to its value). - According to the placeholder used (``%s``, ``%b``, ``%t``), Psycopg may - pick a binary or a text dumper. When using the ``%s`` "`~Format.AUTO`" + pick a binary or a text dumper. When using the ``%s`` "`~PyFormat.AUTO`" format, if the same type has both a text and a binary dumper registered, the last one registered (using `Dumper.register()`) will be selected. @@ -162,7 +162,7 @@ Objects involved in types adaptation move to API section -.. autoclass:: Format +.. autoclass:: PyFormat :members: diff --git a/psycopg/psycopg/_enums.py b/psycopg/psycopg/_enums.py index d00520295..7ccc1d1e6 100644 --- a/psycopg/psycopg/_enums.py +++ b/psycopg/psycopg/_enums.py @@ -12,7 +12,7 @@ from enum import Enum from . import pq -class Format(str, Enum): +class PyFormat(str, Enum): """ Enum representing the format wanted for a query argument. @@ -30,20 +30,20 @@ class Format(str, Enum): """Binary parameter (``%b`` placeholder).""" @classmethod - def from_pq(cls, fmt: pq.Format) -> "Format": + def from_pq(cls, fmt: pq.Format) -> "PyFormat": return _pg2py[fmt] @classmethod - def as_pq(cls, fmt: "Format") -> pq.Format: + def as_pq(cls, fmt: "PyFormat") -> pq.Format: return _py2pg[fmt] _py2pg = { - Format.TEXT: pq.Format.TEXT, - Format.BINARY: pq.Format.BINARY, + PyFormat.TEXT: pq.Format.TEXT, + PyFormat.BINARY: pq.Format.BINARY, } _pg2py = { - pq.Format.TEXT: Format.TEXT, - pq.Format.BINARY: Format.BINARY, + pq.Format.TEXT: PyFormat.TEXT, + pq.Format.BINARY: PyFormat.BINARY, } diff --git a/psycopg/psycopg/_queries.py b/psycopg/psycopg/_queries.py index 06ca2640c..4732c1fee 100644 --- a/psycopg/psycopg/_queries.py +++ b/psycopg/psycopg/_queries.py @@ -13,7 +13,7 @@ from . import pq from . import errors as e from .sql import Composable from .proto import Query, Params -from ._enums import Format +from ._enums import PyFormat if TYPE_CHECKING: from .proto import Transformer @@ -22,7 +22,7 @@ if TYPE_CHECKING: class QueryPart(NamedTuple): pre: bytes item: Union[int, str] - format: Format + format: PyFormat class PostgresQuery: @@ -43,7 +43,7 @@ class PostgresQuery: self.types: Tuple[int, ...] = () # The format requested by the user and the ones to really pass Postgres - self._want_formats: Optional[List[Format]] = None + self._want_formats: Optional[List[PyFormat]] = None self.formats: Optional[Sequence[pq.Format]] = None self._parts: List[QueryPart] @@ -103,7 +103,7 @@ class PostgresQuery: @lru_cache() def _query2pg( query: Union[bytes, str], encoding: str -) -> Tuple[bytes, List[Format], Optional[List[str]], List[QueryPart]]: +) -> Tuple[bytes, List[PyFormat], Optional[List[str]], List[QueryPart]]: """ Convert Python query and params into something Postgres understands. @@ -136,7 +136,7 @@ def _query2pg( formats.append(part.format) elif isinstance(parts[0].item, str): - seen: Dict[str, Tuple[bytes, Format]] = {} + seen: Dict[str, Tuple[bytes, PyFormat]] = {} order = [] for part in parts[:-1]: assert isinstance(part.item, str) @@ -250,7 +250,7 @@ def _split_query(query: bytes, encoding: str = "ascii") -> List[QueryPart]: pre, m = parts[i] if m is None: # last part - rv.append(QueryPart(pre, 0, Format.AUTO)) + rv.append(QueryPart(pre, 0, PyFormat.AUTO)) break ph = m.group(0) @@ -297,7 +297,7 @@ def _split_query(query: bytes, encoding: str = "ascii") -> List[QueryPart]: _ph_to_fmt = { - b"s": Format.AUTO, - b"t": Format.TEXT, - b"b": Format.BINARY, + b"s": PyFormat.AUTO, + b"t": PyFormat.TEXT, + b"b": PyFormat.BINARY, } diff --git a/psycopg/psycopg/_transform.py b/psycopg/psycopg/_transform.py index 00f1d555b..1d4723808 100644 --- a/psycopg/psycopg/_transform.py +++ b/psycopg/psycopg/_transform.py @@ -12,8 +12,7 @@ from . import pq from . import errors as e from .oids import INVALID_OID from .rows import Row, RowMaker -from .proto import LoadFunc, AdaptContext -from ._enums import Format +from .proto import LoadFunc, AdaptContext, PyFormat if TYPE_CHECKING: from .pq.proto import PGresult @@ -54,7 +53,7 @@ class Transformer(AdaptContext): self._conn = None # mapping class, fmt -> Dumper instance - self._dumpers_cache: DefaultDict[Format, DumperCache] = defaultdict( + self._dumpers_cache: DefaultDict[PyFormat, DumperCache] = defaultdict( dict ) @@ -112,7 +111,7 @@ class Transformer(AdaptContext): self._row_loaders = rc def dump_sequence( - self, params: Sequence[Any], formats: Sequence[Format] + self, params: Sequence[Any], formats: Sequence[PyFormat] ) -> Tuple[List[Any], Tuple[int, ...], Sequence[pq.Format]]: ps: List[Optional[bytes]] = [None] * len(params) ts = [INVALID_OID] * len(params) @@ -134,7 +133,7 @@ class Transformer(AdaptContext): return ps, tuple(ts), fs - def get_dumper(self, obj: Any, format: Format) -> "Dumper": + def get_dumper(self, obj: Any, format: PyFormat) -> "Dumper": """ Return a Dumper instance to dump *obj*. """ diff --git a/psycopg/psycopg/adapt.py b/psycopg/psycopg/adapt.py index ea2d78c59..f6b1c46d3 100644 --- a/psycopg/psycopg/adapt.py +++ b/psycopg/psycopg/adapt.py @@ -10,7 +10,7 @@ from typing import cast, TYPE_CHECKING, TypeVar from . import pq from . import errors as e -from ._enums import Format as Format +from ._enums import PyFormat as PyFormat from .oids import postgres_types from .proto import AdaptContext, Buffer as Buffer from ._cmodule import _psycopg @@ -60,7 +60,7 @@ class Dumper(ABC): return b"'%s'" % esc.escape_string(value) def get_key( - self, obj: Any, format: Format + self, obj: Any, format: PyFormat ) -> Union[type, Tuple[type, ...]]: """Return an alternative key to upgrade the dumper to represent *obj* @@ -80,7 +80,7 @@ class Dumper(ABC): """ return self.cls - def upgrade(self, obj: Any, format: Format) -> "Dumper": + def upgrade(self, obj: Any, format: PyFormat) -> "Dumper": """Return a new dumper to manage *obj*. Once `Transformer.get_dumper()` has been notified that this Dumper @@ -140,7 +140,7 @@ class AdaptersMap(AdaptContext): is cheap: a copy is made only on customisation. """ - _dumpers: Dict[Format, Dict[Union[type, str], Type["proto.Dumper"]]] + _dumpers: Dict[PyFormat, Dict[Union[type, str], Type["proto.Dumper"]]] _loaders: List[Dict[int, Type["Loader"]]] types: TypesRegistry @@ -161,7 +161,7 @@ class AdaptersMap(AdaptContext): template._own_loaders = [False, False] self.types = TypesRegistry(template.types) else: - self._dumpers = {fmt: {} for fmt in Format} + self._dumpers = {fmt: {} for fmt in PyFormat} self._own_dumpers = _dumpers_owned.copy() self._loaders = [{}, {}] self._own_loaders = [True, True] @@ -192,7 +192,7 @@ class AdaptersMap(AdaptContext): # Register the dumper both as its format and as auto # so that the last dumper registered is used in auto (%s) format - for fmt in (Format.from_pq(dumper.format), Format.AUTO): + for fmt in (PyFormat.from_pq(dumper.format), PyFormat.AUTO): if not self._own_dumpers[fmt]: self._dumpers[fmt] = self._dumpers[fmt].copy() self._own_dumpers[fmt] = True @@ -222,7 +222,7 @@ class AdaptersMap(AdaptContext): self._loaders[fmt][oid] = loader - def get_dumper(self, cls: type, format: Format) -> Type["proto.Dumper"]: + def get_dumper(self, cls: type, format: PyFormat) -> Type["proto.Dumper"]: """ Return the dumper class for the given type and format. @@ -247,7 +247,7 @@ class AdaptersMap(AdaptContext): raise e.ProgrammingError( f"cannot adapt type {cls.__name__}" - f" to format {Format(format).name}" + f" to format {PyFormat(format).name}" ) def get_loader( @@ -285,8 +285,8 @@ class AdaptersMap(AdaptContext): return cls -_dumpers_owned = dict.fromkeys(Format, True) -_dumpers_shared = dict.fromkeys(Format, False) +_dumpers_owned = dict.fromkeys(PyFormat, True) +_dumpers_shared = dict.fromkeys(PyFormat, False) global_adapters = AdaptersMap(types=postgres_types) diff --git a/psycopg/psycopg/copy.py b/psycopg/psycopg/copy.py index f258be088..5829189bd 100644 --- a/psycopg/psycopg/copy.py +++ b/psycopg/psycopg/copy.py @@ -17,7 +17,7 @@ from typing import Any, Dict, List, Match, Optional, Sequence, Type, Tuple from . import pq from . import errors as e from .pq import ExecStatus -from .adapt import Format +from .adapt import PyFormat from .proto import ConnectionType, PQGen, Transformer from .compat import create_task from ._cmodule import _psycopg @@ -541,7 +541,7 @@ def _format_row_text( for item in row: if item is not None: - dumper = tx.get_dumper(item, Format.TEXT) + dumper = tx.get_dumper(item, PyFormat.TEXT) b = dumper.dump(item) out += _dump_re.sub(_dump_sub, b) else: @@ -562,7 +562,7 @@ def _format_row_binary( out += _pack_int2(len(row)) for item in row: if item is not None: - dumper = tx.get_dumper(item, Format.BINARY) + dumper = tx.get_dumper(item, PyFormat.BINARY) b = dumper.dump(item) out += _pack_int4(len(b)) out += b diff --git a/psycopg/psycopg/proto.py b/psycopg/psycopg/proto.py index 8c96ff07b..8c1b574fe 100644 --- a/psycopg/psycopg/proto.py +++ b/psycopg/psycopg/proto.py @@ -9,7 +9,7 @@ from typing import List, Optional, Sequence, Tuple, TypeVar, Union from typing import TYPE_CHECKING from . import pq -from . import _enums +from ._enums import PyFormat as PyFormat from .compat import Protocol if TYPE_CHECKING: @@ -22,9 +22,6 @@ if TYPE_CHECKING: from .waiting import Wait, Ready from .connection import BaseConnection -# NOMERGE: change name of _enums.Format -PyFormat = _enums.Format - # An object implementing the buffer protocol Buffer = Union[bytes, bytearray, memoryview] diff --git a/psycopg/psycopg/sql.py b/psycopg/psycopg/sql.py index 24e26c628..740c35dcb 100644 --- a/psycopg/psycopg/sql.py +++ b/psycopg/psycopg/sql.py @@ -10,7 +10,7 @@ from abc import ABC, abstractmethod from typing import Any, Iterator, List, Optional, Sequence, Union from .pq import Escaping -from .adapt import Transformer, Format +from .adapt import Transformer, PyFormat from .proto import AdaptContext @@ -395,7 +395,7 @@ class Literal(Composable): def as_bytes(self, context: Optional[AdaptContext]) -> bytes: tx = Transformer(context) - dumper = tx.get_dumper(self._obj, Format.TEXT) + dumper = tx.get_dumper(self._obj, PyFormat.TEXT) return dumper.quote(self._obj) @@ -427,7 +427,7 @@ class Placeholder(Composable): """ - def __init__(self, name: str = "", format: Format = Format.AUTO): + def __init__(self, name: str = "", format: PyFormat = PyFormat.AUTO): super().__init__(name) if not isinstance(name, str): raise TypeError(f"expected string as name, got {name!r}") @@ -441,8 +441,8 @@ class Placeholder(Composable): parts = [] if self._obj: parts.append(repr(self._obj)) - if self._format != Format.AUTO: - parts.append(f"format={Format(self._format).name}") + if self._format != PyFormat.AUTO: + parts.append(f"format={PyFormat(self._format).name}") return f"{self.__class__.__name__}({', '.join(parts)})" diff --git a/psycopg/psycopg/types/array.py b/psycopg/psycopg/types/array.py index b5dfe2c9f..ad998dae5 100644 --- a/psycopg/psycopg/types/array.py +++ b/psycopg/psycopg/types/array.py @@ -12,7 +12,7 @@ from typing import cast from .. import pq from .. import errors as e from ..oids import postgres_types, TEXT_OID, TEXT_ARRAY_OID, INVALID_OID -from ..adapt import RecursiveDumper, RecursiveLoader, Format as Pg3Format +from ..adapt import RecursiveDumper, RecursiveLoader, PyFormat from ..proto import Dumper, AdaptContext, Buffer from .._struct import pack_len, unpack_len from .._typeinfo import TypeInfo @@ -35,7 +35,7 @@ class BaseListDumper(RecursiveDumper): self.sub_dumper: Optional[Dumper] = None self._types = context.adapters.types if context else postgres_types - def get_key(self, obj: List[Any], format: Pg3Format) -> Tuple[type, ...]: + def get_key(self, obj: List[Any], format: PyFormat) -> Tuple[type, ...]: item = self._find_list_element(obj) if item is not None: sd = self._tx.get_dumper(item, format) @@ -43,7 +43,7 @@ class BaseListDumper(RecursiveDumper): else: return (self.cls,) - def upgrade(self, obj: List[Any], format: Pg3Format) -> "BaseListDumper": + def upgrade(self, obj: List[Any], format: PyFormat) -> "BaseListDumper": item = self._find_list_element(obj) if item is None: # Empty lists can only be dumped as text if the type is unknown. diff --git a/psycopg/psycopg/types/composite.py b/psycopg/psycopg/types/composite.py index 07b3a2c94..b15ba0699 100644 --- a/psycopg/psycopg/types/composite.py +++ b/psycopg/psycopg/types/composite.py @@ -12,7 +12,7 @@ from typing import Sequence, Tuple, Type from .. import pq from ..oids import TEXT_OID -from ..adapt import Format, RecursiveDumper, RecursiveLoader +from ..adapt import PyFormat, RecursiveDumper, RecursiveLoader from ..proto import AdaptContext, Buffer from .._struct import unpack_len from .._typeinfo import CompositeInfo as CompositeInfo # exported here @@ -40,7 +40,7 @@ class SequenceDumper(RecursiveDumper): parts.append(sep) continue - dumper = self._tx.get_dumper(item, Format.from_pq(self.format)) + dumper = self._tx.get_dumper(item, PyFormat.from_pq(self.format)) ad = dumper.dump(item) if not ad: ad = b'""' diff --git a/psycopg/psycopg/types/datetime.py b/psycopg/psycopg/types/datetime.py index 3df7245cc..793001085 100644 --- a/psycopg/psycopg/types/datetime.py +++ b/psycopg/psycopg/types/datetime.py @@ -13,7 +13,7 @@ from typing import Any, Callable, cast, Optional, Tuple, Union, TYPE_CHECKING from ..pq import Format from .._tz import get_tzinfo from ..oids import postgres_types as builtins -from ..adapt import Buffer, Dumper, Loader, Format as Pg3Format +from ..adapt import Buffer, Dumper, Loader, PyFormat from ..proto import AdaptContext from ..errors import InterfaceError, DataError from .._struct import pack_int4, pack_int8, unpack_int4, unpack_int8 @@ -62,9 +62,7 @@ class DateBinaryDumper(Dumper): class _BaseTimeDumper(Dumper): - def get_key( - self, obj: time, format: Pg3Format - ) -> Union[type, Tuple[type]]: + def get_key(self, obj: time, format: PyFormat) -> Union[type, Tuple[type]]: # Use (cls,) to report the need to upgrade to a dumper for timetz (the # Frankenstein of the data types). if not obj.tzinfo: @@ -72,7 +70,7 @@ class _BaseTimeDumper(Dumper): else: return (self.cls,) - def upgrade(self, obj: time, format: Pg3Format) -> Dumper: + def upgrade(self, obj: time, format: PyFormat) -> Dumper: raise NotImplementedError @@ -88,7 +86,7 @@ class TimeDumper(_BaseTimeTextDumper): _oid = builtins["time"].oid - def upgrade(self, obj: time, format: Pg3Format) -> Dumper: + def upgrade(self, obj: time, format: PyFormat) -> Dumper: if not obj.tzinfo: return self else: @@ -111,7 +109,7 @@ class TimeBinaryDumper(_BaseTimeDumper): ) return pack_int8(us) - def upgrade(self, obj: time, format: Pg3Format) -> Dumper: + def upgrade(self, obj: time, format: PyFormat) -> Dumper: if not obj.tzinfo: return self else: @@ -134,7 +132,7 @@ class TimeTzBinaryDumper(_BaseTimeDumper): class _BaseDatetimeDumper(Dumper): def get_key( - self, obj: datetime, format: Pg3Format + self, obj: datetime, format: PyFormat ) -> Union[type, Tuple[type]]: # Use (cls,) to report the need to upgrade (downgrade, actually) to a # dumper for naive timestamp. @@ -143,7 +141,7 @@ class _BaseDatetimeDumper(Dumper): else: return (self.cls,) - def upgrade(self, obj: datetime, format: Pg3Format) -> Dumper: + def upgrade(self, obj: datetime, format: PyFormat) -> Dumper: raise NotImplementedError @@ -161,7 +159,7 @@ class DatetimeDumper(_BaseDatetimeTextDumper): _oid = builtins["timestamptz"].oid - def upgrade(self, obj: datetime, format: Pg3Format) -> Dumper: + def upgrade(self, obj: datetime, format: PyFormat) -> Dumper: if obj.tzinfo: return self else: @@ -185,7 +183,7 @@ class DatetimeBinaryDumper(_BaseDatetimeDumper): ) return pack_int8(micros) - def upgrade(self, obj: datetime, format: Pg3Format) -> Dumper: + def upgrade(self, obj: datetime, format: PyFormat) -> Dumper: if obj.tzinfo: return self else: diff --git a/psycopg/psycopg/types/numeric.py b/psycopg/psycopg/types/numeric.py index 2d12af3fe..7a38e7dc1 100644 --- a/psycopg/psycopg/types/numeric.py +++ b/psycopg/psycopg/types/numeric.py @@ -12,8 +12,7 @@ from decimal import Decimal, DefaultContext, Context from .. import errors as e from ..pq import Format from ..oids import postgres_types as builtins -from ..adapt import Buffer, Dumper, Loader -from ..adapt import Format as Pg3Format +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 @@ -124,7 +123,7 @@ class IntDumper(Dumper): " dump() is not supposed to be called" ) - def get_key(self, obj: int, format: Pg3Format) -> type: + def get_key(self, obj: int, format: PyFormat) -> type: return self.upgrade(obj, format).cls _int2_dumper = Int2Dumper(Int2) @@ -132,7 +131,7 @@ class IntDumper(Dumper): _int8_dumper = Int8Dumper(Int8) _int_numeric_dumper = IntNumericDumper(IntNumeric) - def upgrade(self, obj: int, format: Pg3Format) -> Dumper: + def upgrade(self, obj: int, format: PyFormat) -> Dumper: if -(2 ** 31) <= obj < 2 ** 31: if -(2 ** 15) <= obj < 2 ** 15: return self._int2_dumper diff --git a/psycopg/psycopg/types/range.py b/psycopg/psycopg/types/range.py index a55954a23..00a624eac 100644 --- a/psycopg/psycopg/types/range.py +++ b/psycopg/psycopg/types/range.py @@ -12,7 +12,7 @@ from datetime import date, datetime from ..pq import Format from ..oids import postgres_types as builtins, INVALID_OID -from ..adapt import RecursiveDumper, RecursiveLoader, Format as Pg3Format +from ..adapt import RecursiveDumper, RecursiveLoader, PyFormat from ..proto import Dumper, AdaptContext, Buffer from .._struct import pack_len, unpack_len from .._typeinfo import RangeInfo as RangeInfo # exported here @@ -257,10 +257,10 @@ class BaseRangeDumper(RecursiveDumper): super().__init__(cls, context) self.sub_dumper: Optional[Dumper] = None self._types = context.adapters.types if context else builtins - self._adapt_format = Pg3Format.from_pq(self.format) + self._adapt_format = PyFormat.from_pq(self.format) def get_key( - self, obj: Range[Any], format: Pg3Format + self, obj: Range[Any], format: PyFormat ) -> Union[type, Tuple[type, ...]]: # If we are a subclass whose oid is specified we don't need upgrade if self.oid != INVALID_OID: @@ -273,7 +273,7 @@ class BaseRangeDumper(RecursiveDumper): else: return (self.cls,) - def upgrade(self, obj: Range[Any], format: Pg3Format) -> "BaseRangeDumper": + def upgrade(self, obj: Range[Any], format: PyFormat) -> "BaseRangeDumper": # If we are a subclass whose oid is specified we don't need upgrade if self.oid != INVALID_OID: return self @@ -286,7 +286,7 @@ class BaseRangeDumper(RecursiveDumper): if type(item) is int: # postgres won't cast int4range -> int8range so we must use # text format and unknown oid here - sd = self._tx.get_dumper(item, Pg3Format.TEXT) + sd = self._tx.get_dumper(item, PyFormat.TEXT) dumper = RangeDumper(self.cls, self._tx) dumper.sub_dumper = sd dumper.oid = INVALID_OID diff --git a/psycopg_c/psycopg_c/_psycopg.pyi b/psycopg_c/psycopg_c/_psycopg.pyi index 90b372880..e5ba1ad1e 100644 --- a/psycopg_c/psycopg_c/_psycopg.pyi +++ b/psycopg_c/psycopg_c/_psycopg.pyi @@ -12,7 +12,7 @@ from typing import Any, Iterable, List, Optional, Sequence, Tuple from psycopg import pq from psycopg import proto from psycopg.rows import Row, RowMaker -from psycopg.adapt import Loader, AdaptersMap, Format +from psycopg.adapt import Loader, AdaptersMap, PyFormat from psycopg.proto import Dumper from psycopg.pq.proto import PGconn, PGresult from psycopg.connection import BaseConnection @@ -32,9 +32,9 @@ class Transformer(proto.AdaptContext): self, types: Sequence[int], formats: Sequence[pq.Format] ) -> None: ... def dump_sequence( - self, params: Sequence[Any], formats: Sequence[Format] + self, params: Sequence[Any], formats: Sequence[PyFormat] ) -> Tuple[List[Any], Tuple[int, ...], Sequence[pq.Format]]: ... - def get_dumper(self, obj: Any, format: Format) -> Dumper: ... + def get_dumper(self, obj: Any, format: PyFormat) -> Dumper: ... def load_rows( self, row0: int, row1: int, make_row: RowMaker[Row] ) -> List[Row]: ... diff --git a/psycopg_c/psycopg_c/_psycopg.pyx b/psycopg_c/psycopg_c/_psycopg.pyx index 0c97f7904..298f9635b 100644 --- a/psycopg_c/psycopg_c/_psycopg.pyx +++ b/psycopg_c/psycopg_c/_psycopg.pyx @@ -12,14 +12,14 @@ from psycopg_c.pq cimport libpq from psycopg_c._psycopg cimport oids from psycopg.pq import Format as _pq_Format -from psycopg._enums import Format as _pg_Format +from psycopg._enums import PyFormat as _py_Format PQ_TEXT = _pq_Format.TEXT PQ_BINARY = _pq_Format.BINARY -PG_AUTO = _pg_Format.AUTO -PG_TEXT = _pg_Format.TEXT -PG_BINARY = _pg_Format.BINARY +PG_AUTO = _py_Format.AUTO +PG_TEXT = _py_Format.TEXT +PG_BINARY = _py_Format.BINARY include "_psycopg/adapt.pyx" diff --git a/psycopg_c/psycopg_c/_psycopg/transform.pyx b/psycopg_c/psycopg_c/_psycopg/transform.pyx index 4fdc48022..61976b375 100644 --- a/psycopg_c/psycopg_c/_psycopg/transform.pyx +++ b/psycopg_c/psycopg_c/_psycopg/transform.pyx @@ -22,7 +22,6 @@ from cpython.object cimport PyObject, PyObject_CallFunctionObjArgs from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple from psycopg import errors as e -from psycopg._enums import Format as Pg3Format from psycopg.pq import Format as PqFormat from psycopg.rows import Row, RowMaker diff --git a/tests/fix_faker.py b/tests/fix_faker.py index 300786f90..d04e86699 100644 --- a/tests/fix_faker.py +++ b/tests/fix_faker.py @@ -11,7 +11,7 @@ import pytest import psycopg from psycopg import sql -from psycopg.adapt import Format +from psycopg.adapt import PyFormat from psycopg.types.range import Range from psycopg.types.numeric import Int4, Int8 @@ -34,7 +34,7 @@ class Faker: def __init__(self, connection): self.conn = connection - self._format = Format.BINARY + self._format = PyFormat.BINARY self.records = [] self._schema = None @@ -429,14 +429,18 @@ class Faker: (dt.datetime, False), ] # TODO: learn to dump numeric ranges in binary - if self.format != Format.BINARY: + if self.format != PyFormat.BINARY: subtypes.extend([Int4, Int8]) return (cls, choice(subtypes)) def make_Range(self, spec): # TODO: drop format check after fixing binary dumping of empty ranges - if random() < 0.02 and spec[0] is Range and self.format == Format.TEXT: + if ( + random() < 0.02 + and spec[0] is Range + and self.format == PyFormat.TEXT + ): return spec[0](empty=True) while True: @@ -459,7 +463,7 @@ class Faker: # avoid generating ranges with no type info if dumping in binary # TODO: lift this limitation after test_copy_in_empty xfail is fixed - if spec[0] is Range and self.format == Format.BINARY: + if spec[0] is Range and self.format == PyFormat.BINARY: if bounds[0] is bounds[1] is None: continue diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 4f60228c7..caf14d48e 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -5,7 +5,7 @@ import pytest import psycopg from psycopg import pq, sql -from psycopg.adapt import Transformer, Format, Dumper, Loader +from psycopg.adapt import Transformer, PyFormat as Format, Dumper, Loader from psycopg.oids import postgres_types as builtins, TEXT_OID from psycopg._cmodule import _psycopg diff --git a/tests/test_copy.py b/tests/test_copy.py index dabb8aafb..8e0fafbaf 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -11,7 +11,7 @@ from psycopg import pq from psycopg import sql from psycopg import errors as e from psycopg.pq import Format -from psycopg.adapt import Format as PgFormat +from psycopg.adapt import PyFormat as PgFormat from psycopg.types.numeric import Int4 from .utils import gc_collect diff --git a/tests/test_copy_async.py b/tests/test_copy_async.py index e0f7ce9e1..8a512a73d 100644 --- a/tests/test_copy_async.py +++ b/tests/test_copy_async.py @@ -11,7 +11,7 @@ from psycopg import pq from psycopg import sql from psycopg import errors as e from psycopg.pq import Format -from psycopg.adapt import Format as PgFormat +from psycopg.adapt import PyFormat as PgFormat from .utils import gc_collect from .test_copy import sample_text, sample_binary, sample_binary_rows # noqa diff --git a/tests/test_cursor.py b/tests/test_cursor.py index e575867fd..729a6ccfe 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -8,7 +8,7 @@ import pytest import psycopg from psycopg import pq, sql, rows from psycopg.oids import postgres_types as builtins -from psycopg.adapt import Format +from psycopg.adapt import PyFormat as Format from .utils import gc_collect diff --git a/tests/test_cursor_async.py b/tests/test_cursor_async.py index 9d80b2698..965d99894 100644 --- a/tests/test_cursor_async.py +++ b/tests/test_cursor_async.py @@ -5,7 +5,7 @@ import datetime as dt import psycopg from psycopg import pq, sql, rows -from psycopg.adapt import Format +from psycopg.adapt import PyFormat as Format from .utils import gc_collect from .test_cursor import my_row_factory diff --git a/tests/test_query.py b/tests/test_query.py index 12ac37e48..0314dd8cf 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -2,7 +2,7 @@ import pytest import psycopg from psycopg import pq -from psycopg.adapt import Transformer, Format +from psycopg.adapt import Transformer, PyFormat as Format from psycopg._queries import PostgresQuery, _split_query diff --git a/tests/test_sql.py b/tests/test_sql.py index 6813173d9..2232b336a 100644 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -8,7 +8,7 @@ import datetime as dt import pytest from psycopg import sql, ProgrammingError -from psycopg.adapt import Format +from psycopg.adapt import PyFormat as Format @pytest.mark.parametrize( diff --git a/tests/types/test_array.py b/tests/types/test_array.py index b04b75237..45fed9530 100644 --- a/tests/types/test_array.py +++ b/tests/types/test_array.py @@ -3,7 +3,7 @@ import psycopg from psycopg import pq from psycopg import sql from psycopg.oids import postgres_types as builtins -from psycopg.adapt import Format, Transformer +from psycopg.adapt import PyFormat as Format, Transformer from psycopg.types import TypeInfo diff --git a/tests/types/test_bool.py b/tests/types/test_bool.py index e8c3ba0ac..04fd61886 100644 --- a/tests/types/test_bool.py +++ b/tests/types/test_bool.py @@ -3,7 +3,7 @@ import pytest from psycopg import pq from psycopg import sql from psycopg.oids import postgres_types as builtins -from psycopg.adapt import Transformer, Format +from psycopg.adapt import Transformer, PyFormat as Format @pytest.mark.parametrize("fmt_in", [Format.AUTO, Format.TEXT, Format.BINARY]) diff --git a/tests/types/test_composite.py b/tests/types/test_composite.py index 4cbaa47dc..621309dfc 100644 --- a/tests/types/test_composite.py +++ b/tests/types/test_composite.py @@ -3,7 +3,7 @@ import pytest from psycopg import pq from psycopg.sql import Identifier from psycopg.oids import postgres_types as builtins -from psycopg.adapt import Format, global_adapters +from psycopg.adapt import PyFormat as Format, global_adapters from psycopg.types.composite import CompositeInfo diff --git a/tests/types/test_datetime.py b/tests/types/test_datetime.py index 2e91a79b2..1b4c62abe 100644 --- a/tests/types/test_datetime.py +++ b/tests/types/test_datetime.py @@ -4,7 +4,7 @@ import datetime as dt import pytest from psycopg import DataError, pq, sql -from psycopg.adapt import Format +from psycopg.adapt import PyFormat as Format class TestDate: diff --git a/tests/types/test_json.py b/tests/types/test_json.py index d273babbe..2b2bfe79f 100644 --- a/tests/types/test_json.py +++ b/tests/types/test_json.py @@ -6,7 +6,7 @@ import pytest import psycopg.types from psycopg import pq from psycopg import sql -from psycopg.adapt import Format +from psycopg.adapt import PyFormat as Format from psycopg.types.json import Json, Jsonb, set_json_dumps, set_json_loads samples = [ diff --git a/tests/types/test_net.py b/tests/types/test_net.py index 38058774a..154c74da0 100644 --- a/tests/types/test_net.py +++ b/tests/types/test_net.py @@ -6,7 +6,7 @@ import pytest from psycopg import pq from psycopg import sql -from psycopg.adapt import Format +from psycopg.adapt import PyFormat as Format @pytest.mark.parametrize("fmt_in", [Format.AUTO, Format.TEXT, Format.BINARY]) diff --git a/tests/types/test_none.py b/tests/types/test_none.py index 87f731e05..fd12f278f 100644 --- a/tests/types/test_none.py +++ b/tests/types/test_none.py @@ -1,5 +1,5 @@ from psycopg import sql -from psycopg.adapt import Transformer, Format +from psycopg.adapt import Transformer, PyFormat as Format def test_quote_none(conn): diff --git a/tests/types/test_numeric.py b/tests/types/test_numeric.py index db68c5df5..16e98cbe8 100644 --- a/tests/types/test_numeric.py +++ b/tests/types/test_numeric.py @@ -5,7 +5,7 @@ import pytest from psycopg import pq from psycopg import sql -from psycopg.adapt import Transformer, Format +from psycopg.adapt import Transformer, PyFormat as Format from psycopg.types.numeric import FloatLoader diff --git a/tests/types/test_range.py b/tests/types/test_range.py index 9ef9d93f8..634aadc15 100644 --- a/tests/types/test_range.py +++ b/tests/types/test_range.py @@ -7,7 +7,7 @@ import pytest import psycopg.errors from psycopg import pq from psycopg.sql import Identifier -from psycopg.adapt import Format +from psycopg.adapt import PyFormat as Format from psycopg.types.range import Range, RangeInfo diff --git a/tests/types/test_string.py b/tests/types/test_string.py index 33624b71e..04669f0fc 100644 --- a/tests/types/test_string.py +++ b/tests/types/test_string.py @@ -3,7 +3,7 @@ import pytest import psycopg from psycopg import pq from psycopg import sql -from psycopg.adapt import Format +from psycopg.adapt import PyFormat as Format from psycopg import Binary eur = "\u20ac" diff --git a/tests/types/test_uuid.py b/tests/types/test_uuid.py index 81db48ba8..34d5d08ed 100644 --- a/tests/types/test_uuid.py +++ b/tests/types/test_uuid.py @@ -6,7 +6,7 @@ import pytest from psycopg import pq from psycopg import sql -from psycopg.adapt import Format +from psycopg.adapt import PyFormat as Format @pytest.mark.parametrize("fmt_in", [Format.AUTO, Format.TEXT, Format.BINARY]) diff --git a/tests/typing_example.py b/tests/typing_example.py index ad4931355..7e3d6cf09 100644 --- a/tests/typing_example.py +++ b/tests/typing_example.py @@ -6,8 +6,8 @@ from dataclasses import dataclass from typing import Any, Callable, Optional, Sequence, Tuple from psycopg import AnyCursor, Connection, Cursor, ServerCursor, connect -from psycopg import pq, adapt -from psycopg.proto import Dumper, AdaptContext +from psycopg import pq +from psycopg.proto import Dumper, AdaptContext, PyFormat def int_row_factory(cursor: AnyCursor[int]) -> Callable[[Sequence[int]], int]: @@ -110,8 +110,8 @@ class MyStrDumper: esc = pq.Escaping() return b"'%s'" % esc.escape_string(value.replace(b"h", b"q")) - def get_key(self, obj: str, format: adapt.Format) -> type: + def get_key(self, obj: str, format: PyFormat) -> type: return self.cls - def upgrade(self, obj: str, format: adapt.Format) -> "MyStrDumper": + def upgrade(self, obj: str, format: PyFormat) -> "MyStrDumper": return self