From: Daniele Varrazzo Date: Thu, 26 Aug 2021 02:54:24 +0000 (+0200) Subject: Don't use the generic name register_adapters X-Git-Tag: 3.0.beta1~32 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=04d0ac543f26fcff60582eb1006c1856259e1e6e;p=thirdparty%2Fpsycopg.git Don't use the generic name register_adapters There will be cases in which there is more than one type in the same module... --- diff --git a/psycopg/psycopg/_typeinfo.py b/psycopg/psycopg/_typeinfo.py index 16cf6687f..17f572fda 100644 --- a/psycopg/psycopg/_typeinfo.py +++ b/psycopg/psycopg/_typeinfo.py @@ -136,9 +136,9 @@ class TypeInfo: types.add(self) if self.array_oid: - from .types.array import register_adapters + from .types.array import register_array - register_adapters(self, context) + register_array(self, context) _info_query = """\ SELECT @@ -162,9 +162,9 @@ class RangeInfo(TypeInfo): def register(self, context: Optional[AdaptContext] = None) -> None: super().register(context) - from .types.range import register_adapters + from .types.range import register_range - register_adapters(self, context) + register_range(self, context) _info_query = """\ SELECT t.typname AS name, t.oid AS oid, t.typarray AS array_oid, @@ -199,9 +199,9 @@ class CompositeInfo(TypeInfo): ) -> None: super().register(context) - from .types.composite import register_adapters + from .types.composite import register_composite - register_adapters(self, context, factory) + register_composite(self, context, factory) _info_query = """\ SELECT diff --git a/psycopg/psycopg/types/array.py b/psycopg/psycopg/types/array.py index bd3fa9b66..21601a413 100644 --- a/psycopg/psycopg/types/array.py +++ b/psycopg/psycopg/types/array.py @@ -414,7 +414,7 @@ class ArrayBinaryLoader(BaseArrayLoader): return agg(dims) -def register_adapters( +def register_array( info: TypeInfo, context: Optional[AdaptContext] = None ) -> None: adapters = context.adapters if context else postgres.adapters diff --git a/psycopg/psycopg/types/composite.py b/psycopg/psycopg/types/composite.py index 5cc567efe..711238400 100644 --- a/psycopg/psycopg/types/composite.py +++ b/psycopg/psycopg/types/composite.py @@ -183,7 +183,7 @@ class CompositeBinaryLoader(RecordBinaryLoader): return type(self).factory(*r) -def register_adapters( +def register_composite( info: CompositeInfo, context: Optional[AdaptContext] = None, factory: Optional[Callable[..., Any]] = None, diff --git a/psycopg/psycopg/types/hstore.py b/psycopg/psycopg/types/hstore.py index 3367bc07b..59a5013b7 100644 --- a/psycopg/psycopg/types/hstore.py +++ b/psycopg/psycopg/types/hstore.py @@ -105,7 +105,7 @@ class HstoreLoader(RecursiveLoader): return rv -def register_adapters( +def register_hstore( info: TypeInfo, context: Optional[AdaptContext] = None ) -> None: diff --git a/psycopg/psycopg/types/range.py b/psycopg/psycopg/types/range.py index b343fd644..44e1af425 100644 --- a/psycopg/psycopg/types/range.py +++ b/psycopg/psycopg/types/range.py @@ -435,9 +435,16 @@ class RangeBinaryLoader(RecursiveLoader, Generic[T]): _int2parens = {ord(c): c for c in "[]()"} -def register_adapters( +def register_range( info: RangeInfo, context: Optional[AdaptContext] = None ) -> None: + """ + Register custom range adapters on a context. + + Just register loaders associated to the range oid, loading bounds of the + right subtype. Dumping the range just works, navigating from tye Python + type to the type oid, to the range oid. + """ adapters = context.adapters if context else postgres.adapters # generate and register a customized text loader diff --git a/tests/test_copy.py b/tests/test_copy.py index d955ced01..6994ab4b1 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -13,7 +13,7 @@ from psycopg import errors as e from psycopg.pq import Format from psycopg.adapt import PyFormat as PgFormat from psycopg.types import TypeInfo -from psycopg.types.hstore import register_adapters as register_hstore +from psycopg.types.hstore import register_hstore 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 9d1f76311..fe5ac59f2 100644 --- a/tests/test_copy_async.py +++ b/tests/test_copy_async.py @@ -13,7 +13,7 @@ from psycopg import errors as e from psycopg.pq import Format from psycopg.types import TypeInfo from psycopg.adapt import PyFormat as PgFormat -from psycopg.types.hstore import register_adapters as register_hstore +from psycopg.types.hstore import register_hstore from .utils import gc_collect from .test_copy import sample_text, sample_binary, sample_binary_rows # noqa diff --git a/tests/types/test_hstore.py b/tests/types/test_hstore.py index f81a49291..6ec75662c 100644 --- a/tests/types/test_hstore.py +++ b/tests/types/test_hstore.py @@ -2,7 +2,7 @@ import pytest import psycopg from psycopg.types import TypeInfo -from psycopg.types.hstore import HstoreLoader, register_adapters +from psycopg.types.hstore import HstoreLoader, register_hstore @pytest.mark.parametrize( @@ -46,7 +46,7 @@ def test_parse_bad(s): def test_register_conn(hstore, conn): info = TypeInfo.fetch(conn, "hstore") - register_adapters(info, conn) + register_hstore(info, conn) assert conn.adapters.types[info.oid].name == "hstore" cur = conn.execute("select null::hstore, ''::hstore, 'a => b'::hstore") @@ -56,7 +56,7 @@ def test_register_conn(hstore, conn): def test_register_curs(hstore, conn): info = TypeInfo.fetch(conn, "hstore") cur = conn.cursor() - register_adapters(info, cur) + register_hstore(info, cur) assert conn.adapters.types.get(info.oid) is None assert cur.adapters.types[info.oid].name == "hstore" @@ -66,7 +66,7 @@ def test_register_curs(hstore, conn): def test_register_globally(hstore, dsn, svcconn, global_adapters): info = TypeInfo.fetch(svcconn, "hstore") - register_adapters(info) + register_hstore(info) assert psycopg.adapters.types[info.oid].name == "hstore" assert svcconn.adapters.types.get(info.oid) is None @@ -88,12 +88,12 @@ samp = [ @pytest.mark.parametrize("d", samp) def test_roundtrip(hstore, conn, d): - register_adapters(TypeInfo.fetch(conn, "hstore"), conn) + register_hstore(TypeInfo.fetch(conn, "hstore"), conn) d1 = conn.execute("select %s", [d]).fetchone()[0] assert d == d1 def test_roundtrip_array(hstore, conn): - register_adapters(TypeInfo.fetch(conn, "hstore"), conn) + register_hstore(TypeInfo.fetch(conn, "hstore"), conn) samp1 = conn.execute("select %s", (samp,)).fetchone()[0] assert samp1 == samp