From: Daniele Varrazzo Date: Fri, 5 Feb 2021 02:13:23 +0000 (+0100) Subject: Add AdaptersMap.types map X-Git-Tag: 3.0.dev0~127 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=36ae4d40d1ed2f27954e5b68db5764cc8db08e93;p=thirdparty%2Fpsycopg.git Add AdaptersMap.types map Use the map from the adapters when it's needed to look up for a type info. Probably there are other places where to make use of it. --- diff --git a/docs/copy.rst b/docs/copy.rst index c9665cb80..f8045ade5 100644 --- a/docs/copy.rst +++ b/docs/copy.rst @@ -104,13 +104,6 @@ you have to specify them yourselves. for row in copy.rows(): print(row) # (10, datetime.date(2046, 12, 24)) -.. admonition:: TODO - - Currently only builtin names are recognised; custom types must be - specified by numeric oid. This wll change after the `TypeRegistry` and - `AdaptContext` get integrated, none of which I have documented, so you - haven't seen anything... 👀 - Copying block-by-block ---------------------- diff --git a/psycopg3/psycopg3/_column.py b/psycopg3/psycopg3/_column.py index e5b7d9873..260c39913 100644 --- a/psycopg3/psycopg3/_column.py +++ b/psycopg3/psycopg3/_column.py @@ -8,7 +8,6 @@ from typing import Any, NamedTuple, Optional, Sequence, TYPE_CHECKING from operator import attrgetter from . import errors as e -from .oids import builtins if TYPE_CHECKING: from .cursor import BaseCursor @@ -39,6 +38,7 @@ class Column(Sequence[Any]): fmod=res.fmod(index), fsize=res.fsize(index), ) + self._type = cursor.adapters.types.get(self._data.ftype) _attrs = tuple( attrgetter(attr) @@ -55,8 +55,7 @@ class Column(Sequence[Any]): def _type_display(self) -> str: parts = [] - t = builtins.get(self.type_code) - parts.append(t.name if t else str(self.type_code)) + parts.append(self._type.name if self._type else str(self.type_code)) mod1 = self.precision if mod1 is None: @@ -67,7 +66,7 @@ class Column(Sequence[Any]): parts.append(f", {self.scale}") parts.append(")") - if t and self.type_code == t.array_oid: + if self._type and self.type_code == self._type.array_oid: parts.append("[]") return "".join(parts) @@ -91,11 +90,10 @@ class Column(Sequence[Any]): @property def display_size(self) -> Optional[int]: """The field size, for :sql:`varchar(n)`, None otherwise.""" - t = builtins.get(self.type_code) - if not t: + if not self._type: return None - if t.name in ("varchar", "char"): + if self._type.name in ("varchar", "char"): fmod = self._data.fmod if fmod >= 0: return fmod - 4 @@ -111,17 +109,16 @@ class Column(Sequence[Any]): @property def precision(self) -> Optional[int]: """The number of digits for fixed precision types.""" - t = builtins.get(self.type_code) - if not t: + if not self._type: return None dttypes = ("time", "timetz", "timestamp", "timestamptz", "interval") - if t.name == "numeric": + if self._type.name == "numeric": fmod = self._data.fmod if fmod >= 0: return fmod >> 16 - elif t.name in dttypes: + elif self._type.name in dttypes: fmod = self._data.fmod if fmod >= 0: return fmod & 0xFFFF @@ -134,7 +131,7 @@ class Column(Sequence[Any]): TODO: probably better than precision for datetime objects? review. """ - if self.type_code == builtins["numeric"].oid: + if self._type and self._type.name == "numeric": fmod = self._data.fmod - 4 if fmod >= 0: return fmod & 0xFFFF diff --git a/psycopg3/psycopg3/adapt.py b/psycopg3/psycopg3/adapt.py index fc8a9cbbf..0b4ffe43d 100644 --- a/psycopg3/psycopg3/adapt.py +++ b/psycopg3/psycopg3/adapt.py @@ -12,7 +12,7 @@ from . import pq from . import proto from . import errors as e from ._enums import Format as Format -from .oids import builtins +from .oids import TypesRegistry, postgres_types from .proto import AdaptContext, Buffer as Buffer if TYPE_CHECKING: @@ -124,8 +124,6 @@ class Loader(ABC): """ Configure *context* to use this loader to convert values with OID *oid*. """ - if isinstance(oid, str): - oid = builtins[oid].oid adapters = context.adapters if context else global_adapters adapters.register_loader(oid, cls) @@ -142,21 +140,28 @@ class AdaptersMap(AdaptContext): _dumpers: List[Dict[Union[type, str], Type["Dumper"]]] _loaders: List[Dict[int, Type["Loader"]]] + types: TypesRegistry # Record if a dumper or loader has an optimised version. _optimised: Dict[type, type] = {} - def __init__(self, extend: Optional["AdaptersMap"] = None): - if extend: - self._dumpers = extend._dumpers[:] + def __init__( + self, + template: Optional["AdaptersMap"] = None, + types: Optional[TypesRegistry] = None, + ): + if template: + self._dumpers = template._dumpers[:] self._own_dumpers = [False, False] - self._loaders = extend._loaders[:] + self._loaders = template._loaders[:] self._own_loaders = [False, False] + self.types = TypesRegistry(template.types) else: self._dumpers = [{}, {}] self._own_dumpers = [True, True] self._loaders = [{}, {}] self._own_loaders = [True, True] + self.types = types or TypesRegistry() # implement the AdaptContext protocol too @property @@ -186,10 +191,14 @@ class AdaptersMap(AdaptContext): self._dumpers[fmt][cls] = dumper - def register_loader(self, oid: int, loader: Type[Loader]) -> None: + def register_loader( + self, oid: Union[int, str], loader: Type[Loader] + ) -> None: """ Configure the context to use *loader* to convert data of oid *oid*. """ + if isinstance(oid, str): + oid = self.types[oid].oid if not isinstance(oid, int): raise TypeError( f"loaders should be registered on oid, got {oid} instead" @@ -282,7 +291,7 @@ class AdaptersMap(AdaptContext): return cls -global_adapters = AdaptersMap() +global_adapters = AdaptersMap(types=postgres_types) Transformer: Type[proto.Transformer] diff --git a/psycopg3/psycopg3/copy.py b/psycopg3/psycopg3/copy.py index b55506906..996b8e5d4 100644 --- a/psycopg3/psycopg3/copy.py +++ b/psycopg3/psycopg3/copy.py @@ -17,7 +17,6 @@ from typing import Any, Dict, List, Match, Optional, Sequence, Type, Tuple from . import pq from . import errors as e from .pq import ExecStatus -from .oids import builtins from .adapt import Format from .proto import ConnectionType, PQGen, Transformer from .generators import copy_from, copy_to, copy_end @@ -95,10 +94,9 @@ class BaseCopy(Generic[ConnectionType]): custom data types you must use their oid. """ - # TODO: should allow names of non-builtin types - # Must put a types map on the context. + registry = self.cursor.adapters.types oids = [ - t if isinstance(t, int) else builtins.get_oid(t) for t in types + t if isinstance(t, int) else registry.get_oid(t) for t in types ] self.formatter.transformer.set_row_types( oids, [self.formatter.format] * len(types) diff --git a/psycopg3/psycopg3/dbapi20.py b/psycopg3/psycopg3/dbapi20.py index 8972662d4..dc7e55156 100644 --- a/psycopg3/psycopg3/dbapi20.py +++ b/psycopg3/psycopg3/dbapi20.py @@ -10,7 +10,7 @@ from math import floor from typing import Any, Sequence from .pq import Format -from .oids import builtins +from .oids import postgres_types as builtins from .adapt import Dumper diff --git a/psycopg3/psycopg3/oids.py b/psycopg3/psycopg3/oids.py index daedcf312..2942f3b56 100644 --- a/psycopg3/psycopg3/oids.py +++ b/psycopg3/psycopg3/oids.py @@ -43,12 +43,24 @@ class TypesRegistry: Container for the information about types in a database. """ - def __init__(self) -> None: - self._by_oid: Dict[int, TypeInfo] = {} - self._by_name: Dict[str, TypeInfo] = {} - self._by_range_subtype: Dict[int, TypeInfo] = {} + def __init__(self, template: Optional["TypesRegistry"] = None): + self._by_oid: Dict[int, TypeInfo] + self._by_name: Dict[str, TypeInfo] + self._by_range_subtype: Dict[int, TypeInfo] + + if template: + self._by_oid = template._by_oid + self._by_name = template._by_name + self._by_range_subtype = template._by_range_subtype + self._own_state = False + else: + self._by_oid = {} + self._by_name = {} + self._by_range_subtype = {} + self._own_state = True def add(self, info: TypeInfo) -> None: + self._ensure_own_state() self._by_oid[info.oid] = info if info.array_oid: self._by_oid[info.array_oid] = info @@ -126,8 +138,16 @@ class TypesRegistry: return None return self._by_range_subtype.get(info.oid) + def _ensure_own_state(self) -> None: + # Time to write! so, copy. + if not self._own_state: + self._by_oid = self._by_oid.copy() + self._by_name = self._by_name.copy() + self._by_range_subtype = self._by_range_subtype.copy() + self._own_state = True + -builtins = TypesRegistry() +postgres_types = TypesRegistry() # Use tools/update_oids.py to update this data. for r in [ @@ -220,10 +240,10 @@ for r in [ ("xml", 142, 143, 0, "xml", ","), # autogenerated: end ]: - builtins.add(BuiltinTypeInfo(*r)) + postgres_types.add(BuiltinTypeInfo(*r)) # A few oids used a bit everywhere INVALID_OID = 0 -TEXT_OID = builtins["text"].oid -TEXT_ARRAY_OID = builtins["text"].array_oid +TEXT_OID = postgres_types["text"].oid +TEXT_ARRAY_OID = postgres_types["text"].array_oid diff --git a/psycopg3/psycopg3/types/__init__.py b/psycopg3/psycopg3/types/__init__.py index 828c36421..dc70082b9 100644 --- a/psycopg3/psycopg3/types/__init__.py +++ b/psycopg3/psycopg3/types/__init__.py @@ -4,7 +4,7 @@ psycopg3 types package # Copyright (C) 2020-2021 The Psycopg Team -from ..oids import builtins, INVALID_OID +from ..oids import INVALID_OID from ..proto import AdaptContext # Register default adapters @@ -227,4 +227,4 @@ def register_default_globals(ctx: AdaptContext) -> None: RecordLoader.register("record", ctx) RecordBinaryLoader.register("record", ctx) - array.register_all_arrays() + array.register_all_arrays(ctx) diff --git a/psycopg3/psycopg3/types/array.py b/psycopg3/psycopg3/types/array.py index 6baa3b2a5..b68bf77c3 100644 --- a/psycopg3/psycopg3/types/array.py +++ b/psycopg3/psycopg3/types/array.py @@ -10,7 +10,7 @@ from typing import Any, Iterator, List, Optional, Set, Tuple, Type from .. import pq from .. import errors as e -from ..oids import builtins, TEXT_OID, TEXT_ARRAY_OID, INVALID_OID +from ..oids import postgres_types, TEXT_OID, TEXT_ARRAY_OID, INVALID_OID from ..adapt import Buffer, Dumper, Loader, Transformer from ..adapt import Format as Pg3Format from ..proto import AdaptContext @@ -21,6 +21,7 @@ class BaseListDumper(Dumper): super().__init__(cls, context) self._tx = Transformer(context) 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, ...]: item = self._find_list_element(obj) @@ -87,12 +88,10 @@ class BaseListDumper(Dumper): Return the oid of the array from the oid of the base item. Fall back on text[]. - TODO: we shouldn't consider builtins only, but other adaptation - contexts too """ oid = 0 if base_oid: - info = builtins.get(base_oid) + info = self._types.get(base_oid) if info: oid = info.array_oid @@ -332,14 +331,14 @@ def register( loader.register(array_oid, context=context) -def register_all_arrays() -> None: +def register_all_arrays(ctx: AdaptContext) -> None: """ Associate the array oid of all the types in Loader.globals. This function is designed to be called once at import time, after having registered all the base loaders. """ - for t in builtins: + for t in ctx.adapters.types: # TODO: handle different delimiters (box) if t.array_oid and getattr(t, "delimiter", None) == ",": register(t.array_oid, t.oid, name=t.name) diff --git a/psycopg3/psycopg3/types/composite.py b/psycopg3/psycopg3/types/composite.py index 612a547ec..c43a68b1c 100644 --- a/psycopg3/psycopg3/types/composite.py +++ b/psycopg3/psycopg3/types/composite.py @@ -186,7 +186,7 @@ class SequenceDumper(Dumper): class TupleDumper(SequenceDumper): # Should be this, but it doesn't work - # _oid = builtins["record"].oid + # _oid = postgres_types["record"].oid def dump(self, obj: Tuple[Any, ...]) -> bytes: return self._dump_sequence(obj, b"(", b")", b",") diff --git a/psycopg3/psycopg3/types/date.py b/psycopg3/psycopg3/types/date.py index ad918dbd0..478e969d3 100644 --- a/psycopg3/psycopg3/types/date.py +++ b/psycopg3/psycopg3/types/date.py @@ -10,7 +10,7 @@ from datetime import date, datetime, time, timedelta from typing import cast, Optional, Tuple, Union from ..pq import Format -from ..oids import builtins +from ..oids import postgres_types as builtins from ..adapt import Buffer, Dumper, Loader, Format as Pg3Format from ..proto import AdaptContext from ..errors import InterfaceError, DataError diff --git a/psycopg3/psycopg3/types/json.py b/psycopg3/psycopg3/types/json.py index 85ad07289..1e021cabc 100644 --- a/psycopg3/psycopg3/types/json.py +++ b/psycopg3/psycopg3/types/json.py @@ -8,7 +8,7 @@ import json from typing import Any, Callable, Optional from ..pq import Format -from ..oids import builtins +from ..oids import postgres_types as builtins from ..adapt import Buffer, Dumper, Loader from ..errors import DataError diff --git a/psycopg3/psycopg3/types/network.py b/psycopg3/psycopg3/types/network.py index 9166a74be..c08e6971d 100644 --- a/psycopg3/psycopg3/types/network.py +++ b/psycopg3/psycopg3/types/network.py @@ -7,7 +7,7 @@ Adapters for network types. from typing import Callable, Optional, Union, TYPE_CHECKING from ..pq import Format -from ..oids import builtins +from ..oids import postgres_types as builtins from ..adapt import Buffer, Dumper, Loader from ..proto import AdaptContext diff --git a/psycopg3/psycopg3/types/numeric.py b/psycopg3/psycopg3/types/numeric.py index 13c02252d..36b4e9cd4 100644 --- a/psycopg3/psycopg3/types/numeric.py +++ b/psycopg3/psycopg3/types/numeric.py @@ -9,7 +9,7 @@ from typing import Any, Callable, Dict, Tuple, cast from decimal import Decimal from ..pq import Format -from ..oids import builtins +from ..oids import postgres_types as builtins from ..adapt import Buffer, Dumper, Loader from ..adapt import Format as Pg3Format from ..wrappers.numeric import Int2, Int4, Int8, IntNumeric diff --git a/psycopg3/psycopg3/types/range.py b/psycopg3/psycopg3/types/range.py index 37c7e0af0..5e2165ee0 100644 --- a/psycopg3/psycopg3/types/range.py +++ b/psycopg3/psycopg3/types/range.py @@ -13,7 +13,7 @@ from datetime import date, datetime from .. import sql from .. import errors as e from ..pq import Format -from ..oids import builtins, TypeInfo, INVALID_OID +from ..oids import postgres_types as builtins, TypeInfo, INVALID_OID from ..adapt import Buffer, Dumper, Loader, Format as Pg3Format from ..proto import AdaptContext @@ -227,6 +227,7 @@ class RangeDumper(SequenceDumper): def __init__(self, cls: type, context: Optional[AdaptContext] = None): super().__init__(cls, context) self.sub_dumper: Optional[Dumper] = None + self._types = context.adapters.types if context else builtins def dump(self, obj: Range[Any]) -> bytes: if not obj: @@ -283,7 +284,7 @@ class RangeDumper(SequenceDumper): TODO: we shouldn't consider builtins only, but other adaptation contexts too """ - info = builtins.get_range(sub_oid) + info = self._types.get_range(sub_oid) return info.oid if info else INVALID_OID diff --git a/psycopg3/psycopg3/types/singletons.py b/psycopg3/psycopg3/types/singletons.py index 443a4ec31..539c15052 100644 --- a/psycopg3/psycopg3/types/singletons.py +++ b/psycopg3/psycopg3/types/singletons.py @@ -5,7 +5,7 @@ Adapters for None and boolean. # Copyright (C) 2020-2021 The Psycopg Team from ..pq import Format -from ..oids import builtins +from ..oids import postgres_types as builtins from ..adapt import Buffer, Dumper, Loader diff --git a/psycopg3/psycopg3/types/text.py b/psycopg3/psycopg3/types/text.py index d87cf8ea7..c298b1cc4 100644 --- a/psycopg3/psycopg3/types/text.py +++ b/psycopg3/psycopg3/types/text.py @@ -7,7 +7,7 @@ Adapters for textual types. from typing import Optional, Union, TYPE_CHECKING from ..pq import Format, Escaping -from ..oids import builtins +from ..oids import postgres_types as builtins from ..adapt import Buffer, Dumper, Loader from ..proto import AdaptContext from ..errors import DataError diff --git a/psycopg3/psycopg3/types/uuid.py b/psycopg3/psycopg3/types/uuid.py index ec973ecac..67987322b 100644 --- a/psycopg3/psycopg3/types/uuid.py +++ b/psycopg3/psycopg3/types/uuid.py @@ -7,7 +7,7 @@ Adapters for the UUID type. from typing import Callable, Optional, TYPE_CHECKING from ..pq import Format -from ..oids import builtins +from ..oids import postgres_types as builtins from ..adapt import Buffer, Dumper, Loader from ..proto import AdaptContext diff --git a/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx b/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx index 1a1c188c6..e75fb426a 100644 --- a/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx +++ b/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx @@ -160,10 +160,6 @@ cdef class CLoader: context: Optional["AdaptContext"] = None, int format = PQ_TEXT, ) -> None: - if isinstance(oid, str): - from psycopg3.oids import builtins - oid = builtins[oid].oid - if context is not None: adapters = context.adapters else: diff --git a/tests/fix_faker.py b/tests/fix_faker.py index fbd806bec..17cc5803b 100644 --- a/tests/fix_faker.py +++ b/tests/fix_faker.py @@ -8,7 +8,6 @@ import pytest import psycopg3 from psycopg3 import sql -from psycopg3.oids import builtins from psycopg3.adapt import Format @@ -70,10 +69,11 @@ class Faker: record = self.make_record(nulls=0) tx = psycopg3.adapt.Transformer(self.conn) types = [] + registry = self.conn.adapters.types for value in record: dumper = tx.get_dumper(value, self.format) dumper.dump(value) # load the oid if it's dynamic (e.g. array) - info = builtins.get(dumper.oid) or builtins.get("text") + info = registry.get(dumper.oid) or registry.get("text") if dumper.oid == info.array_oid: types.append(sql.SQL("{}[]").format(sql.Identifier(info.name))) else: diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 11759560d..27607d926 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -5,7 +5,7 @@ import pytest import psycopg3 from psycopg3 import pq from psycopg3.adapt import Transformer, Format, Dumper, Loader -from psycopg3.oids import builtins, TEXT_OID +from psycopg3.oids import postgres_types as builtins, TEXT_OID @pytest.mark.parametrize( diff --git a/tests/test_copy.py b/tests/test_copy.py index 0ff86ea83..f0aca9940 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -11,7 +11,6 @@ from psycopg3 import pq from psycopg3 import sql from psycopg3 import errors as e from psycopg3.pq import Format -from psycopg3.oids import builtins from psycopg3.adapt import Format as PgFormat from psycopg3.types.numeric import Int4 @@ -94,10 +93,7 @@ def test_read_rows(conn, format, typetype): select 10::int4, 'hello'::text, '{{0.0,1.0}}'::float8[] ) to stdout (format {format.name})""" ) as copy: - types = ["int4", "text", "float8[]"] - if typetype == "oids": - types = [builtins.get_oid(t) for t in types] - copy.set_types(types) + copy.set_types(["int4", "text", "float8[]"]) row = copy.read_row() assert copy.read_row() is None @@ -111,9 +107,7 @@ def test_rows(conn, format): with cur.copy( f"copy ({sample_values}) to stdout (format {format.name})" ) as copy: - copy.set_types( - [builtins["int4"].oid, builtins["int4"].oid, builtins["text"].oid] - ) + copy.set_types(["int4", "int4", "text"]) rows = list(copy.rows()) assert rows == sample_records @@ -130,7 +124,7 @@ def test_copy_out_allchars(conn, format): "copy (select unnest({}::text[])) to stdout (format {})" ).format(chars, sql.SQL(format.name)) with cur.copy(query) as copy: - copy.set_types([builtins["text"].oid]) + copy.set_types(["text"]) while 1: row = copy.read_row() if not row: diff --git a/tests/test_copy_async.py b/tests/test_copy_async.py index 236b86d6d..bf3a5ef90 100644 --- a/tests/test_copy_async.py +++ b/tests/test_copy_async.py @@ -11,7 +11,6 @@ from psycopg3 import pq from psycopg3 import sql from psycopg3 import errors as e from psycopg3.pq import Format -from psycopg3.oids import builtins from psycopg3.adapt import Format as PgFormat from .test_copy import sample_text, sample_binary, sample_binary_rows # noqa @@ -72,12 +71,7 @@ async def test_read_rows(aconn, format): async with cur.copy( f"copy ({sample_values}) to stdout (format {format.name})" ) as copy: - # TODO: should be passed by name - # big refactoring to be had, to have builtins not global and merged - # to adaptation context I guess... - copy.set_types( - [builtins["int4"].oid, builtins["int4"].oid, builtins["text"].oid] - ) + copy.set_types("int4 int4 text".split()) rows = [] while 1: row = await copy.read_row() @@ -95,9 +89,7 @@ async def test_rows(aconn, format): async with cur.copy( f"copy ({sample_values}) to stdout (format {format.name})" ) as copy: - copy.set_types( - [builtins["int4"].oid, builtins["int4"].oid, builtins["text"].oid] - ) + copy.set_types("int4 int4 text".split()) rows = [] async for row in copy.rows(): rows.append(row) @@ -116,7 +108,7 @@ async def test_copy_out_allchars(aconn, format): "copy (select unnest({}::text[])) to stdout (format {})" ).format(chars, sql.SQL(format.name)) async with cur.copy(query) as copy: - copy.set_types([builtins["text"].oid]) + copy.set_types(["text"]) while 1: row = await copy.read_row() if not row: diff --git a/tests/test_cursor.py b/tests/test_cursor.py index b2d905b1b..481dbf5e4 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -7,7 +7,7 @@ import pytest import psycopg3 from psycopg3 import sql -from psycopg3.oids import builtins +from psycopg3.oids import postgres_types as builtins from psycopg3.adapt import Format diff --git a/tests/types/test_array.py b/tests/types/test_array.py index f1b19ae7a..220876831 100644 --- a/tests/types/test_array.py +++ b/tests/types/test_array.py @@ -2,7 +2,7 @@ import pytest import psycopg3 from psycopg3 import pq from psycopg3 import sql -from psycopg3.oids import builtins +from psycopg3.oids import postgres_types as builtins from psycopg3.adapt import Format, Transformer from psycopg3.types import array diff --git a/tests/types/test_composite.py b/tests/types/test_composite.py index b9aadbd3c..32d2ad75a 100644 --- a/tests/types/test_composite.py +++ b/tests/types/test_composite.py @@ -2,7 +2,7 @@ import pytest from psycopg3 import pq from psycopg3.sql import Identifier -from psycopg3.oids import builtins +from psycopg3.oids import postgres_types as builtins from psycopg3.adapt import Format, global_adapters from psycopg3.types.composite import CompositeInfo diff --git a/tests/types/test_numeric.py b/tests/types/test_numeric.py index e02e3db81..437601504 100644 --- a/tests/types/test_numeric.py +++ b/tests/types/test_numeric.py @@ -5,7 +5,6 @@ import pytest from psycopg3 import pq from psycopg3 import sql -from psycopg3.oids import builtins from psycopg3.adapt import Transformer, Format from psycopg3.types.numeric import FloatLoader @@ -115,7 +114,7 @@ def test_load_int(conn, val, pgtype, want, fmt_out): cur = conn.cursor(binary=fmt_out) cur.execute(f"select %s::{pgtype}", (val,)) assert cur.pgresult.fformat(0) == fmt_out - assert cur.pgresult.ftype(0) == builtins[pgtype].oid + assert cur.pgresult.ftype(0) == conn.adapters.types[pgtype].oid result = cur.fetchone()[0] assert result == want assert type(result) is type(want) @@ -123,7 +122,7 @@ def test_load_int(conn, val, pgtype, want, fmt_out): # arrays work too cur.execute(f"select array[%s::{pgtype}]", (val,)) assert cur.pgresult.fformat(0) == fmt_out - assert cur.pgresult.ftype(0) == builtins[pgtype].array_oid + assert cur.pgresult.ftype(0) == conn.adapters.types[pgtype].array_oid result = cur.fetchone()[0] assert result == [want] assert type(result[0]) is type(want) @@ -232,7 +231,7 @@ def test_load_float(conn, val, pgtype, want, fmt_out): cur = conn.cursor(binary=fmt_out) cur.execute(f"select %s::{pgtype}", (val,)) assert cur.pgresult.fformat(0) == fmt_out - assert cur.pgresult.ftype(0) == builtins[pgtype].oid + assert cur.pgresult.ftype(0) == conn.adapters.types[pgtype].oid result = cur.fetchone()[0] def check(result, want): @@ -249,7 +248,7 @@ def test_load_float(conn, val, pgtype, want, fmt_out): cur.execute(f"select array[%s::{pgtype}]", (val,)) assert cur.pgresult.fformat(0) == fmt_out - assert cur.pgresult.ftype(0) == builtins[pgtype].array_oid + assert cur.pgresult.ftype(0) == conn.adapters.types[pgtype].array_oid result = cur.fetchone()[0] assert isinstance(result, list) check(result[0], want) @@ -357,7 +356,7 @@ def test_load_numeric_binary(conn): ) def test_numeric_as_float(conn, val): cur = conn.cursor() - FloatLoader.register(builtins["numeric"].oid, cur) + FloatLoader.register(conn.adapters.types["numeric"].oid, cur) val = Decimal(val) cur.execute("select %s as val", (val,)) diff --git a/tests/types/test_range.py b/tests/types/test_range.py index 294b6b5c8..b69371c91 100644 --- a/tests/types/test_range.py +++ b/tests/types/test_range.py @@ -5,7 +5,6 @@ from decimal import Decimal import pytest from psycopg3.sql import Identifier -from psycopg3.oids import builtins from psycopg3.types import range as mrange from psycopg3.types.range import Range @@ -164,7 +163,7 @@ def test_fetch_info(conn, testrange, name, subtype): assert info.name == "testrange" assert info.oid > 0 assert info.oid != info.array_oid > 0 - assert info.range_subtype == builtins[subtype].oid + assert info.range_subtype == conn.adapters.types[subtype].oid def test_fetch_info_not_found(conn): @@ -179,7 +178,7 @@ async def test_fetch_info_async(aconn, testrange, name, subtype): assert info.name == "testrange" assert info.oid > 0 assert info.oid != info.array_oid > 0 - assert info.range_subtype == builtins[subtype].oid + assert info.range_subtype == aconn.adapters.types[subtype].oid @pytest.mark.asyncio diff --git a/tests/types/test_singletons.py b/tests/types/test_singletons.py index 03b26d068..8ec3102fe 100644 --- a/tests/types/test_singletons.py +++ b/tests/types/test_singletons.py @@ -2,7 +2,7 @@ import pytest from psycopg3 import pq from psycopg3 import sql -from psycopg3.oids import builtins +from psycopg3.oids import postgres_types as builtins from psycopg3.adapt import Transformer, Format