]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Make Dumper.oid a class attribute for every Dumper subclass
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 26 Aug 2021 22:01:12 +0000 (00:01 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 27 Aug 2021 22:23:47 +0000 (00:23 +0200)
14 files changed:
psycopg/psycopg/adapt.py
psycopg/psycopg/types/array.py
psycopg/psycopg/types/bool.py
psycopg/psycopg/types/composite.py
psycopg/psycopg/types/datetime.py
psycopg/psycopg/types/hstore.py
psycopg/psycopg/types/json.py
psycopg/psycopg/types/net.py
psycopg/psycopg/types/numeric.py
psycopg/psycopg/types/range.py
psycopg/psycopg/types/string.py
psycopg/psycopg/types/uuid.py
tests/adapters_example.py
tests/types/test_array.py

index b3d655ca80ba0a762576a1c123bb39041a7c0dab..5a99e3af024d07cef34b07174980bb5613e85032 100644 (file)
@@ -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__}"
index 21601a41310c3c85c62c163c62693d34d279025a..b58cb520a533e86d3afa5a283341d8dd6a978c07 100644 (file)
@@ -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):
index 47b77fb179a0bb12ccbd901b1798cf7bceeb1448..cdfddc4dabb672853288e3a8de91a6f4695c5691 100644 (file)
@@ -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"
index 0eedd24321f7f7b33aef6c67e895044ce7b62aa8..5e77d1c4f8488ee9007c3f2b362eca1a2649dce6 100644 (file)
@@ -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)
 
index b938c2351e301d75d5c6ceb399708b8ddd7c5eb4..f423d50885d93cd98d91e90611d45359f134da04 100644 (file)
@@ -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
index 675e1f4b9d3ac73715f45b216593cbdd8308c022..235cc7cb3d4ec3be774134f5f5a59a46cc893e9e 100644 (file)
@@ -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)
 
index efb9b876322e6d6e777df639b0e35438505b7111..9bf9574ffd71754bb18e9ea7d8924d34cc17da2b 100644 (file)
@@ -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
index 05f0d261cd64b1a30283a3e6430cda762a4127ed..3cdef7a09cab54661a590d80bd34645c2c3acede 100644 (file)
@@ -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
index 323a85dd3d1e3af0c5bd8b3e4f69b3dc6d02671e..0b126132c8237109d47827e9228a8f5b6ae089f6 100644 (file)
@@ -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()
index f68cdac36f50e5cdbe00d3a69ffe83591c40b220..5c16527599a6f744b4e448028969d18c0e21cbc8 100644 (file)
@@ -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
index b3bac6b0c9aa2b884fff0575621b51cd3060dcce..f256689566395971457d4ceeb71ae608a2186963 100644 (file)
@@ -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
index 133666c5c8819c078405d4bd733a62ca3dbabf43..252295de0c1b1dbd3656761586525cf294ee17c9 100644 (file)
@@ -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")
index 6460e76f99a5da274cda7404167f10afd214e099..322a09c7e33ffaa3473818f31d560fe995687aa6 100644 (file)
@@ -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")
index d3c084c779248bf6ddb4e438d071a0c55669c29a..d7f38291bacc047055e77ce2e338a3077566c1b0 100644 (file)
@@ -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")