From: Daniele Varrazzo Date: Sun, 20 Mar 2022 15:21:40 +0000 (+0100) Subject: refactor: `TypeInfo.alt_name` renamed to `regtype` X-Git-Tag: 3.1~109^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ee0359f37d981f0d77eebdd45e1b70d51e28d93e;p=thirdparty%2Fpsycopg.git refactor: `TypeInfo.alt_name` renamed to `regtype` This has now acquired a precise meaning: it is the name of the type as SQL snippet (it may contain invalid names, which get quoted, or a schema name). It can be included literally in a query to make a valid cast. The previous name wasn't documented and wasn't part of any public API so we can probably rename it without much problem. --- diff --git a/psycopg/psycopg/_typeinfo.py b/psycopg/psycopg/_typeinfo.py index c69642a6b..173691332 100644 --- a/psycopg/psycopg/_typeinfo.py +++ b/psycopg/psycopg/_typeinfo.py @@ -37,13 +37,13 @@ class TypeInfo: oid: int, array_oid: int, *, - alt_name: str = "", + regtype: str = "", delimiter: str = ",", ): self.name = name self.oid = oid self.array_oid = array_oid - self.alt_name = alt_name or name + self.regtype = regtype or name self.delimiter = delimiter def __repr__(self) -> str: @@ -160,7 +160,7 @@ class TypeInfo: return """\ SELECT typname AS name, oid, typarray AS array_oid, - oid::regtype::text AS alt_name, typdelim AS delimiter + oid::regtype::text AS regtype, typdelim AS delimiter FROM pg_type t WHERE t.oid = %(name)s::regtype ORDER BY t.oid @@ -182,10 +182,10 @@ class RangeInfo(TypeInfo): oid: int, array_oid: int, *, - alt_name: str = "", + regtype: str = "", subtype_oid: int, ): - super().__init__(name, oid, array_oid, alt_name=alt_name) + super().__init__(name, oid, array_oid, regtype=regtype) self.subtype_oid = subtype_oid @classmethod @@ -194,7 +194,7 @@ class RangeInfo(TypeInfo): ) -> str: return """\ SELECT t.typname AS name, t.oid AS oid, t.typarray AS array_oid, - t.oid::regtype::text AS alt_name, + t.oid::regtype::text AS regtype, r.rngsubtype AS subtype_oid FROM pg_type t JOIN pg_range r ON t.oid = r.rngtypid @@ -218,11 +218,11 @@ class MultirangeInfo(TypeInfo): oid: int, array_oid: int, *, - alt_name: str = "", + regtype: str = "", range_oid: int, subtype_oid: int, ): - super().__init__(name, oid, array_oid, alt_name=alt_name) + super().__init__(name, oid, array_oid, regtype=regtype) self.range_oid = range_oid self.subtype_oid = subtype_oid @@ -236,7 +236,7 @@ class MultirangeInfo(TypeInfo): ) return """\ SELECT t.typname AS name, t.oid AS oid, t.typarray AS array_oid, - t.oid::regtype::text AS alt_name, + t.oid::regtype::text AS regtype, r.rngtypid AS range_oid, r.rngsubtype AS subtype_oid FROM pg_type t JOIN pg_range r ON t.oid = r.rngmultitypid @@ -260,11 +260,11 @@ class CompositeInfo(TypeInfo): oid: int, array_oid: int, *, - alt_name: str = "", + regtype: str = "", field_names: Sequence[str], field_types: Sequence[int], ): - super().__init__(name, oid, array_oid, alt_name=alt_name) + super().__init__(name, oid, array_oid, regtype=regtype) self.field_names = field_names self.field_types = field_types # Will be set by register() if the `factory` is a type @@ -277,7 +277,7 @@ class CompositeInfo(TypeInfo): return """\ SELECT t.typname AS name, t.oid AS oid, t.typarray AS array_oid, - t.oid::regtype::text AS alt_name, + t.oid::regtype::text AS regtype, coalesce(a.fnames, '{}') AS field_names, coalesce(a.ftypes, '{}') AS field_types FROM pg_type t @@ -368,8 +368,8 @@ class TypesRegistry: self._registry[info.array_oid] = info self._registry[info.name] = info - if info.alt_name and info.alt_name not in self._registry: - self._registry[info.alt_name] = info + if info.regtype and info.regtype not in self._registry: + self._registry[info.regtype] = info # Allow info to customise further their relation with the registry info._added(self) diff --git a/psycopg/psycopg/postgres.py b/psycopg/psycopg/postgres.py index 29816c248..713bba701 100644 --- a/psycopg/psycopg/postgres.py +++ b/psycopg/psycopg/postgres.py @@ -20,23 +20,23 @@ for t in [ # Generated from PostgreSQL 14.0 TypeInfo("aclitem", 1033, 1034), TypeInfo("bit", 1560, 1561), - TypeInfo("bool", 16, 1000, alt_name="boolean"), + TypeInfo("bool", 16, 1000, regtype="boolean"), TypeInfo("box", 603, 1020, delimiter=";"), - TypeInfo("bpchar", 1042, 1014, alt_name="character"), + TypeInfo("bpchar", 1042, 1014, regtype="character"), TypeInfo("bytea", 17, 1001), - TypeInfo("char", 18, 1002, alt_name='"char"'), + TypeInfo("char", 18, 1002, regtype='"char"'), TypeInfo("cid", 29, 1012), TypeInfo("cidr", 650, 651), TypeInfo("circle", 718, 719), TypeInfo("date", 1082, 1182), - TypeInfo("float4", 700, 1021, alt_name="real"), - TypeInfo("float8", 701, 1022, alt_name="double precision"), + TypeInfo("float4", 700, 1021, regtype="real"), + TypeInfo("float8", 701, 1022, regtype="double precision"), TypeInfo("gtsvector", 3642, 3644), TypeInfo("inet", 869, 1041), - TypeInfo("int2", 21, 1005, alt_name="smallint"), + TypeInfo("int2", 21, 1005, regtype="smallint"), TypeInfo("int2vector", 22, 1006), - TypeInfo("int4", 23, 1007, alt_name="integer"), - TypeInfo("int8", 20, 1016, alt_name="bigint"), + TypeInfo("int4", 23, 1007, regtype="integer"), + TypeInfo("int8", 20, 1016, regtype="bigint"), TypeInfo("interval", 1186, 1187), TypeInfo("json", 114, 199), TypeInfo("jsonb", 3802, 3807), @@ -69,16 +69,16 @@ for t in [ TypeInfo("regtype", 2206, 2211), TypeInfo("text", 25, 1009), TypeInfo("tid", 27, 1010), - TypeInfo("time", 1083, 1183, alt_name="time without time zone"), - TypeInfo("timestamp", 1114, 1115, alt_name="timestamp without time zone"), - TypeInfo("timestamptz", 1184, 1185, alt_name="timestamp with time zone"), - TypeInfo("timetz", 1266, 1270, alt_name="time with time zone"), + TypeInfo("time", 1083, 1183, regtype="time without time zone"), + TypeInfo("timestamp", 1114, 1115, regtype="timestamp without time zone"), + TypeInfo("timestamptz", 1184, 1185, regtype="timestamp with time zone"), + TypeInfo("timetz", 1266, 1270, regtype="time with time zone"), TypeInfo("tsquery", 3615, 3645), TypeInfo("tsvector", 3614, 3643), TypeInfo("txid_snapshot", 2970, 2949), TypeInfo("uuid", 2950, 2951), - TypeInfo("varbit", 1562, 1563, alt_name="bit varying"), - TypeInfo("varchar", 1043, 1015, alt_name="character varying"), + TypeInfo("varbit", 1562, 1563, regtype="bit varying"), + TypeInfo("varchar", 1043, 1015, regtype="character varying"), TypeInfo("xid", 28, 1011), TypeInfo("xid8", 5069, 271), TypeInfo("xml", 142, 143), diff --git a/psycopg/psycopg/sql.py b/psycopg/psycopg/sql.py index bcf604bb0..f2b0ac1cc 100644 --- a/psycopg/psycopg/sql.py +++ b/psycopg/psycopg/sql.py @@ -402,10 +402,10 @@ class Literal(Composable): ti = tx.adapters.types.get(dumper.oid) if ti: try: - type_name = self._names_cache[ti.alt_name, tx.encoding] + type_name = self._names_cache[ti.regtype, tx.encoding] except KeyError: - type_name = ti.alt_name.encode(tx.encoding) - self._names_cache[ti.alt_name, tx.encoding] = type_name + type_name = ti.regtype.encode(tx.encoding) + self._names_cache[ti.regtype, tx.encoding] = type_name rv = b"%s::%s" % (rv, type_name) return rv diff --git a/tests/test_typeinfo.py b/tests/test_typeinfo.py index fd1367e37..d0316ad3a 100644 --- a/tests/test_typeinfo.py +++ b/tests/test_typeinfo.py @@ -23,7 +23,7 @@ def test_fetch(conn, name, status): assert info.oid == psycopg.adapters.types["text"].oid assert info.array_oid == psycopg.adapters.types["text"].array_oid - assert info.alt_name == "text" + assert info.regtype == "text" @pytest.mark.asyncio diff --git a/tools/update_oids.py b/tools/update_oids.py index 743ad91ad..98d8fce6c 100755 --- a/tools/update_oids.py +++ b/tools/update_oids.py @@ -32,7 +32,7 @@ select oid::text, typarray::text, case when oid::regtype::text != typname - then format('alt_name=%L', oid::regtype) + then format('regtype=%L', oid::regtype) end, case when typdelim != ',' then format('delimiter=%L', typdelim)