From: Daniele Varrazzo Date: Thu, 26 Aug 2021 22:01:12 +0000 (+0200) Subject: Make Dumper.oid a class attribute for every Dumper subclass X-Git-Tag: 3.0.beta1~29^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4223f24fd4b17acecf1541dfeba2f52a2ebd4c54;p=thirdparty%2Fpsycopg.git Make Dumper.oid a class attribute for every Dumper subclass --- diff --git a/psycopg/psycopg/adapt.py b/psycopg/psycopg/adapt.py index b3d655ca8..5a99e3af0 100644 --- a/psycopg/psycopg/adapt.py +++ b/psycopg/psycopg/adapt.py @@ -26,11 +26,7 @@ class Dumper(abc.Dumper, ABC): Convert Python object of the type *cls* to PostgreSQL representation. """ - # A class-wide oid, which will be used by default by instances unless - # the subclass overrides it in init. - _oid: int = 0 - - oid: int + oid: int = 0 """The oid to pass to the server, if known.""" def __init__(self, cls: type, context: Optional[abc.AdaptContext] = None): @@ -39,8 +35,6 @@ class Dumper(abc.Dumper, ABC): context.connection if context else None ) - self.oid = self._oid - def __repr__(self) -> str: return ( f"<{type(self).__module__}.{type(self).__qualname__}" diff --git a/psycopg/psycopg/types/array.py b/psycopg/psycopg/types/array.py index 21601a413..b58cb520a 100644 --- a/psycopg/psycopg/types/array.py +++ b/psycopg/psycopg/types/array.py @@ -213,7 +213,7 @@ class MixedNumbersListDumper(MixedItemsListDumper): NUMBERS_TYPES = (int, float, Decimal) - _oid = postgres.types["numeric"].array_oid + oid = postgres.types["numeric"].array_oid class ListBinaryDumper(BaseListDumper): diff --git a/psycopg/psycopg/types/bool.py b/psycopg/psycopg/types/bool.py index 47b77fb17..cdfddc4da 100644 --- a/psycopg/psycopg/types/bool.py +++ b/psycopg/psycopg/types/bool.py @@ -13,7 +13,7 @@ from ..adapt import Buffer, Dumper, Loader class BoolDumper(Dumper): format = Format.TEXT - _oid = postgres.types["bool"].oid + oid = postgres.types["bool"].oid def dump(self, obj: bool) -> bytes: return b"t" if obj else b"f" @@ -25,7 +25,7 @@ class BoolDumper(Dumper): class BoolBinaryDumper(Dumper): format = Format.BINARY - _oid = postgres.types["bool"].oid + oid = postgres.types["bool"].oid def dump(self, obj: bool) -> bytes: return b"\x01" if obj else b"\x00" diff --git a/psycopg/psycopg/types/composite.py b/psycopg/psycopg/types/composite.py index 0eedd2432..5e77d1c4f 100644 --- a/psycopg/psycopg/types/composite.py +++ b/psycopg/psycopg/types/composite.py @@ -64,7 +64,7 @@ class SequenceDumper(RecursiveDumper): class TupleDumper(SequenceDumper): # Should be this, but it doesn't work - # _oid = postgres_types["record"].oid + # oid = postgres_types["record"].oid def dump(self, obj: Tuple[Any, ...]) -> bytes: return self._dump_sequence(obj, b"(", b")", b",") @@ -253,13 +253,13 @@ def register_composite( dumper = type( f"{info.name.title()}BinaryDumper", (TupleBinaryDumper,), - {"_oid": info.oid, "info": info}, + {"oid": info.oid, "info": info}, ) adapters.register_dumper(factory, dumper) # Default to the text dumper because it is more flexible dumper = type( - f"{info.name.title()}Dumper", (TupleDumper,), {"_oid": info.oid} + f"{info.name.title()}Dumper", (TupleDumper,), {"oid": info.oid} ) adapters.register_dumper(factory, dumper) diff --git a/psycopg/psycopg/types/datetime.py b/psycopg/psycopg/types/datetime.py index b938c2351..f423d5088 100644 --- a/psycopg/psycopg/types/datetime.py +++ b/psycopg/psycopg/types/datetime.py @@ -43,7 +43,7 @@ _py_date_min_days = date.min.toordinal() class DateDumper(Dumper): format = Format.TEXT - _oid = postgres.types["date"].oid + oid = postgres.types["date"].oid def dump(self, obj: date) -> bytes: # NOTE: whatever the PostgreSQL DateStyle input format (DMY, MDY, YMD) @@ -54,7 +54,7 @@ class DateDumper(Dumper): class DateBinaryDumper(Dumper): format = Format.BINARY - _oid = postgres.types["date"].oid + oid = postgres.types["date"].oid def dump(self, obj: date) -> bytes: days = obj.toordinal() - _pg_date_epoch_days @@ -84,7 +84,7 @@ class _BaseTimeTextDumper(_BaseTimeDumper): class TimeDumper(_BaseTimeTextDumper): - _oid = postgres.types["time"].oid + oid = postgres.types["time"].oid def upgrade(self, obj: time, format: PyFormat) -> Dumper: if not obj.tzinfo: @@ -95,13 +95,13 @@ class TimeDumper(_BaseTimeTextDumper): class TimeTzDumper(_BaseTimeTextDumper): - _oid = postgres.types["timetz"].oid + oid = postgres.types["timetz"].oid class TimeBinaryDumper(_BaseTimeDumper): format = Format.BINARY - _oid = postgres.types["time"].oid + oid = postgres.types["time"].oid def dump(self, obj: time) -> bytes: us = obj.microsecond + 1_000_000 * ( @@ -119,7 +119,7 @@ class TimeBinaryDumper(_BaseTimeDumper): class TimeTzBinaryDumper(_BaseTimeDumper): format = Format.BINARY - _oid = postgres.types["timetz"].oid + oid = postgres.types["timetz"].oid def dump(self, obj: time) -> bytes: us = obj.microsecond + 1_000_000 * ( @@ -155,7 +155,7 @@ class _BaseDatetimeTextDumper(_BaseDatetimeDumper): class DatetimeDumper(_BaseDatetimeTextDumper): - _oid = postgres.types["timestamptz"].oid + oid = postgres.types["timestamptz"].oid def upgrade(self, obj: datetime, format: PyFormat) -> Dumper: if obj.tzinfo: @@ -166,13 +166,13 @@ class DatetimeDumper(_BaseDatetimeTextDumper): class DatetimeNoTzDumper(_BaseDatetimeTextDumper): - _oid = postgres.types["timestamp"].oid + oid = postgres.types["timestamp"].oid class DatetimeBinaryDumper(_BaseDatetimeDumper): format = Format.BINARY - _oid = postgres.types["timestamptz"].oid + oid = postgres.types["timestamptz"].oid def dump(self, obj: datetime) -> bytes: delta = obj - _pg_datetimetz_epoch @@ -191,7 +191,7 @@ class DatetimeBinaryDumper(_BaseDatetimeDumper): class DatetimeNoTzBinaryDumper(_BaseDatetimeDumper): format = Format.BINARY - _oid = postgres.types["timestamp"].oid + oid = postgres.types["timestamp"].oid def dump(self, obj: datetime) -> bytes: delta = obj - _pg_datetime_epoch @@ -204,7 +204,7 @@ class DatetimeNoTzBinaryDumper(_BaseDatetimeDumper): class TimedeltaDumper(Dumper): format = Format.TEXT - _oid = postgres.types["interval"].oid + oid = postgres.types["interval"].oid def __init__(self, cls: type, context: Optional[AdaptContext] = None): super().__init__(cls, context) @@ -231,7 +231,7 @@ class TimedeltaDumper(Dumper): class TimedeltaBinaryDumper(Dumper): format = Format.BINARY - _oid = postgres.types["interval"].oid + oid = postgres.types["interval"].oid def dump(self, obj: timedelta) -> bytes: micros = 1_000_000 * obj.seconds + obj.microseconds diff --git a/psycopg/psycopg/types/hstore.py b/psycopg/psycopg/types/hstore.py index 675e1f4b9..235cc7cb3 100644 --- a/psycopg/psycopg/types/hstore.py +++ b/psycopg/psycopg/types/hstore.py @@ -116,7 +116,7 @@ def register_hstore( # Generate and register a customized text dumper dumper: Type[BaseHstoreDumper] = type( - "HstoreDumper", (BaseHstoreDumper,), {"_oid": info.oid} + "HstoreDumper", (BaseHstoreDumper,), {"oid": info.oid} ) adapters.register_dumper(dict, dumper) diff --git a/psycopg/psycopg/types/json.py b/psycopg/psycopg/types/json.py index efb9b8763..9bf9574ff 100644 --- a/psycopg/psycopg/types/json.py +++ b/psycopg/psycopg/types/json.py @@ -130,25 +130,25 @@ class _JsonDumper(Dumper): class JsonDumper(_JsonDumper): format = Format.TEXT - _oid = postgres.types["json"].oid + oid = postgres.types["json"].oid class JsonBinaryDumper(_JsonDumper): format = Format.BINARY - _oid = postgres.types["json"].oid + oid = postgres.types["json"].oid class JsonbDumper(_JsonDumper): format = Format.TEXT - _oid = postgres.types["jsonb"].oid + oid = postgres.types["jsonb"].oid class JsonbBinaryDumper(_JsonDumper): format = Format.BINARY - _oid = postgres.types["jsonb"].oid + oid = postgres.types["jsonb"].oid def dump(self, obj: _JsonWrapper) -> bytes: dumps = obj.dumps or self.dumps diff --git a/psycopg/psycopg/types/net.py b/psycopg/psycopg/types/net.py index 05f0d261c..3cdef7a09 100644 --- a/psycopg/psycopg/types/net.py +++ b/psycopg/psycopg/types/net.py @@ -39,7 +39,7 @@ IPV6_PREFIXLEN = 128 class InterfaceDumper(Dumper): format = Format.TEXT - _oid = postgres.types["inet"].oid + oid = postgres.types["inet"].oid def dump(self, obj: Interface) -> bytes: return str(obj).encode("utf8") @@ -48,7 +48,7 @@ class InterfaceDumper(Dumper): class NetworkDumper(Dumper): format = Format.TEXT - _oid = postgres.types["cidr"].oid + oid = postgres.types["cidr"].oid def dump(self, obj: Network) -> bytes: return str(obj).encode("utf8") @@ -57,7 +57,7 @@ class NetworkDumper(Dumper): class AddressBinaryDumper(Dumper): format = Format.BINARY - _oid = postgres.types["inet"].oid + oid = postgres.types["inet"].oid def dump(self, obj: Address) -> bytes: packed = obj.packed @@ -69,7 +69,7 @@ class AddressBinaryDumper(Dumper): class InterfaceBinaryDumper(Dumper): format = Format.BINARY - _oid = postgres.types["inet"].oid + oid = postgres.types["inet"].oid def dump(self, obj: Interface) -> bytes: packed = obj.packed @@ -81,7 +81,7 @@ class InterfaceBinaryDumper(Dumper): class NetworkBinaryDumper(Dumper): format = Format.BINARY - _oid = postgres.types["cidr"].oid + oid = postgres.types["cidr"].oid def dump(self, obj: Network) -> bytes: packed = obj.network_address.packed diff --git a/psycopg/psycopg/types/numeric.py b/psycopg/psycopg/types/numeric.py index 323a85dd3..0b126132c 100644 --- a/psycopg/psycopg/types/numeric.py +++ b/psycopg/psycopg/types/numeric.py @@ -59,7 +59,7 @@ class _SpecialValuesDumper(_NumberDumper): class FloatDumper(_SpecialValuesDumper): format = Format.TEXT - _oid = postgres.types["float8"].oid + oid = postgres.types["float8"].oid _special = { b"inf": b"'Infinity'::float8", @@ -69,13 +69,13 @@ class FloatDumper(_SpecialValuesDumper): class Float4Dumper(FloatDumper): - _oid = postgres.types["float4"].oid + oid = postgres.types["float4"].oid class FloatBinaryDumper(Dumper): format = Format.BINARY - _oid = postgres.types["float8"].oid + oid = postgres.types["float8"].oid def dump(self, obj: float) -> bytes: return pack_float8(obj) @@ -83,7 +83,7 @@ class FloatBinaryDumper(Dumper): class Float4BinaryDumper(FloatBinaryDumper): - _oid = postgres.types["float4"].oid + oid = postgres.types["float4"].oid def dump(self, obj: float) -> bytes: return pack_float4(obj) @@ -91,7 +91,7 @@ class Float4BinaryDumper(FloatBinaryDumper): class DecimalDumper(_SpecialValuesDumper): - _oid = postgres.types["numeric"].oid + oid = postgres.types["numeric"].oid def dump(self, obj: Decimal) -> bytes: if obj.is_nan(): @@ -108,23 +108,23 @@ class DecimalDumper(_SpecialValuesDumper): class Int2Dumper(_NumberDumper): - _oid = postgres.types["int2"].oid + oid = postgres.types["int2"].oid class Int4Dumper(_NumberDumper): - _oid = postgres.types["int4"].oid + oid = postgres.types["int4"].oid class Int8Dumper(_NumberDumper): - _oid = postgres.types["int8"].oid + oid = postgres.types["int8"].oid class IntNumericDumper(_NumberDumper): - _oid = postgres.types["numeric"].oid + oid = postgres.types["numeric"].oid class OidDumper(_NumberDumper): - _oid = postgres.types["oid"].oid + oid = postgres.types["oid"].oid class IntDumper(Dumper): @@ -386,7 +386,7 @@ NUMERIC_NINF_BIN = _pack_numeric_head(0, 0, NUMERIC_NINF, 0) class DecimalBinaryDumper(Dumper): format = Format.BINARY - _oid = postgres.types["numeric"].oid + oid = postgres.types["numeric"].oid def dump(self, obj: Decimal) -> Union[bytearray, bytes]: sign, digits, exp = obj.as_tuple() diff --git a/psycopg/psycopg/types/range.py b/psycopg/psycopg/types/range.py index f68cdac36..5c1652759 100644 --- a/psycopg/psycopg/types/range.py +++ b/psycopg/psycopg/types/range.py @@ -474,27 +474,27 @@ def register_range( class Int4RangeDumper(RangeDumper): - _oid = postgres.types["int4range"].oid + oid = postgres.types["int4range"].oid class Int8RangeDumper(RangeDumper): - _oid = postgres.types["int8range"].oid + oid = postgres.types["int8range"].oid class NumericRangeDumper(RangeDumper): - _oid = postgres.types["numrange"].oid + oid = postgres.types["numrange"].oid class DateRangeDumper(RangeDumper): - _oid = postgres.types["daterange"].oid + oid = postgres.types["daterange"].oid class TimestampRangeDumper(RangeDumper): - _oid = postgres.types["tsrange"].oid + oid = postgres.types["tsrange"].oid class TimestamptzRangeDumper(RangeDumper): - _oid = postgres.types["tstzrange"].oid + oid = postgres.types["tstzrange"].oid # Binary dumpers for builtin range types wrappers @@ -503,27 +503,27 @@ class TimestamptzRangeDumper(RangeDumper): class Int4RangeBinaryDumper(RangeBinaryDumper): - _oid = postgres.types["int4range"].oid + oid = postgres.types["int4range"].oid class Int8RangeBinaryDumper(RangeBinaryDumper): - _oid = postgres.types["int8range"].oid + oid = postgres.types["int8range"].oid class NumericRangeBinaryDumper(RangeBinaryDumper): - _oid = postgres.types["numrange"].oid + oid = postgres.types["numrange"].oid class DateRangeBinaryDumper(RangeBinaryDumper): - _oid = postgres.types["daterange"].oid + oid = postgres.types["daterange"].oid class TimestampRangeBinaryDumper(RangeBinaryDumper): - _oid = postgres.types["tsrange"].oid + oid = postgres.types["tsrange"].oid class TimestamptzRangeBinaryDumper(RangeBinaryDumper): - _oid = postgres.types["tstzrange"].oid + oid = postgres.types["tstzrange"].oid # Text loaders for builtin range types diff --git a/psycopg/psycopg/types/string.py b/psycopg/psycopg/types/string.py index b3bac6b0c..f25668956 100644 --- a/psycopg/psycopg/types/string.py +++ b/psycopg/psycopg/types/string.py @@ -33,7 +33,7 @@ class _BaseStrDumper(Dumper): class StrBinaryDumper(_BaseStrDumper): format = Format.BINARY - _oid = postgres.types["text"].oid + oid = postgres.types["text"].oid def dump(self, obj: str) -> bytes: # the server will raise DataError subclass if the string contains 0x00 @@ -63,7 +63,7 @@ class StrDumper(_StrDumper): text oid is required, for instance with variadic functions. """ - _oid = postgres.types["text"].oid + oid = postgres.types["text"].oid class StrDumperUnknown(_StrDumper): @@ -111,7 +111,7 @@ class TextBinaryLoader(TextLoader): class BytesDumper(Dumper): format = Format.TEXT - _oid = postgres.types["bytea"].oid + oid = postgres.types["bytea"].oid _qprefix = b"" def __init__(self, cls: type, context: Optional[AdaptContext] = None): @@ -154,7 +154,7 @@ class BytesDumper(Dumper): class BytesBinaryDumper(Dumper): format = Format.BINARY - _oid = postgres.types["bytea"].oid + oid = postgres.types["bytea"].oid def dump(self, obj: Buffer) -> Buffer: return obj diff --git a/psycopg/psycopg/types/uuid.py b/psycopg/psycopg/types/uuid.py index 133666c5c..252295de0 100644 --- a/psycopg/psycopg/types/uuid.py +++ b/psycopg/psycopg/types/uuid.py @@ -22,7 +22,7 @@ UUID: Callable[..., "uuid.UUID"] class UUIDDumper(Dumper): format = Format.TEXT - _oid = postgres.types["uuid"].oid + oid = postgres.types["uuid"].oid def dump(self, obj: "uuid.UUID") -> bytes: return obj.hex.encode("utf8") diff --git a/tests/adapters_example.py b/tests/adapters_example.py index 6460e76f9..322a09c7e 100644 --- a/tests/adapters_example.py +++ b/tests/adapters_example.py @@ -15,10 +15,10 @@ def f() -> None: class MyStrDumper: format = pq.Format.TEXT + oid = 25 # text def __init__(self, cls: type, context: Optional[AdaptContext] = None): self._cls = cls - self.oid = 25 # text def dump(self, obj: str) -> bytes: return (obj * 2).encode("utf-8") diff --git a/tests/types/test_array.py b/tests/types/test_array.py index d3c084c77..d7f38291b 100644 --- a/tests/types/test_array.py +++ b/tests/types/test_array.py @@ -230,7 +230,7 @@ def test_dump_list_no_comma_separator(conn): class BoxDumper(Dumper): format = pq.Format.TEXT - _oid = psycopg.postgres.types["box"].oid + oid = psycopg.postgres.types["box"].oid def dump(self, box): return ("(%s,%s),(%s,%s)" % box.coords).encode("utf8")