From: Daniele Varrazzo Date: Wed, 17 Mar 2021 17:10:04 +0000 (+0100) Subject: Fix dump of enum subtypes X-Git-Tag: 3.0.dev0~80 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=256476e3a57356dadf6b8004504ba9745e88afae;p=thirdparty%2Fpsycopg.git Fix dump of enum subtypes --- diff --git a/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx b/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx index e75fb426a..35545e23f 100644 --- a/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx +++ b/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx @@ -35,7 +35,7 @@ cdef class CDumper: cdef public libpq.Oid oid cdef pq.PGconn _pgconn - def __init__(self, cls: type, context: Optional[AdaptContext] = None): + def __init__(self, cls, context: Optional[AdaptContext] = None): self.cls = cls conn = context.connection if context is not None else None self._pgconn = conn.pgconn if conn is not None else None diff --git a/psycopg3_c/psycopg3_c/types/text.pyx b/psycopg3_c/psycopg3_c/types/text.pyx index c80183aa6..808ce3142 100644 --- a/psycopg3_c/psycopg3_c/types/text.pyx +++ b/psycopg3_c/psycopg3_c/types/text.pyx @@ -30,7 +30,7 @@ cdef class _StringDumper(CDumper): cdef char *encoding cdef bytes _bytes_encoding # needed to keep `encoding` alive - def __init__(self, cls: type, context: Optional[AdaptContext] = None): + def __init__(self, cls, context: Optional[AdaptContext] = None): super().__init__(cls, context) self.is_utf8 = 0 diff --git a/tests/types/test_numeric.py b/tests/types/test_numeric.py index 437601504..886a31439 100644 --- a/tests/types/test_numeric.py +++ b/tests/types/test_numeric.py @@ -67,6 +67,18 @@ def test_dump_int_subtypes(conn, val, expr, fmt_in): assert cur.fetchone()[0] is True +@pytest.mark.parametrize("fmt_in", [Format.AUTO, Format.TEXT, Format.BINARY]) +def test_dump_enum(conn, fmt_in): + import enum + + class MyEnum(enum.IntEnum): + foo = 42 + + cur = conn.cursor() + (res,) = cur.execute("select %s", (MyEnum.foo,)).fetchone() + assert res == 42 + + @pytest.mark.parametrize( "val, expr", [ diff --git a/tests/types/test_text.py b/tests/types/test_text.py index c18dcd358..d8c27b456 100644 --- a/tests/types/test_text.py +++ b/tests/types/test_text.py @@ -102,6 +102,18 @@ def test_dump_utf8_badenc(conn, fmt_in): cur.execute(f"select %{fmt_in}", ("\uddf8",)) +@pytest.mark.parametrize("fmt_in", [Format.AUTO, Format.TEXT, Format.BINARY]) +def test_dump_enum(conn, fmt_in): + from enum import Enum + + class MyEnum(str, Enum): + foo = "foo" + + cur = conn.cursor() + (res,) = cur.execute("select %s", (MyEnum.foo,)).fetchone() + assert res == "foo" + + @pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY]) @pytest.mark.parametrize("encoding", ["utf8", "latin9"]) @pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"])