From: Daniele Varrazzo Date: Thu, 26 Aug 2021 18:18:57 +0000 (+0200) Subject: A more elegant way to customise TypeInfo registration in subclasses X-Git-Tag: 3.0.beta1~30^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=975caf3eb9185cc928a9156de6f176b9efc118fa;p=thirdparty%2Fpsycopg.git A more elegant way to customise TypeInfo registration in subclasses --- diff --git a/psycopg/psycopg/_typeinfo.py b/psycopg/psycopg/_typeinfo.py index 991f1c74a..7accec54f 100644 --- a/psycopg/psycopg/_typeinfo.py +++ b/psycopg/psycopg/_typeinfo.py @@ -149,6 +149,10 @@ WHERE t.oid = %(name)s::regtype ORDER BY t.oid """ + def _added(self, registry: "TypesRegistry") -> None: + """Method called by the *registry* when the object is added there.""" + pass + class RangeInfo(TypeInfo): """Manage information about a range type.""" @@ -167,6 +171,11 @@ JOIN pg_range r ON t.oid = r.rngtypid WHERE t.oid = %(name)s::regtype """ + def _added(self, registry: "TypesRegistry") -> None: + """Method called by the *registry* when the object is added there.""" + # Map ranges subtypes to info + registry._by_range_subtype[self.subtype_oid] = self + class CompositeInfo(TypeInfo): """Manage information about a composite type.""" @@ -253,9 +262,8 @@ class TypesRegistry: if info.alt_name and info.alt_name not in self._by_name: self._by_name[info.alt_name] = info - # Map ranges subtypes to info - if isinstance(info, RangeInfo): - self._by_range_subtype[info.subtype_oid] = info + # Allow info to customise further their relation with the registry + info._added(self) def __iter__(self) -> Iterator[TypeInfo]: seen = set()