]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
A more elegant way to customise TypeInfo registration in subclasses
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 26 Aug 2021 18:18:57 +0000 (20:18 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 26 Aug 2021 18:46:41 +0000 (20:46 +0200)
psycopg/psycopg/_typeinfo.py

index 991f1c74a59a10598485a925da96f36b3051ccf3..7accec54fceb7d0c6aed4a416004326c64da93eb 100644 (file)
@@ -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()