Configure the context to use `!dumper` to convert objects of type `!cls`. If two dumpers with different `~Dumper.format` are registered for the same type, the last one registered will be chosen when the query doesn't specify a format (i.e. when the value is used wit
(self, cls: type | str | None, dumper: type[Dumper])
| 112 | return None |
| 113 | |
| 114 | def register_dumper(self, cls: type | str | None, dumper: type[Dumper]) -> None: |
| 115 | """ |
| 116 | Configure the context to use `!dumper` to convert objects of type `!cls`. |
| 117 | |
| 118 | If two dumpers with different `~Dumper.format` are registered for the |
| 119 | same type, the last one registered will be chosen when the query |
| 120 | doesn't specify a format (i.e. when the value is used with a ``%s`` |
| 121 | "`~PyFormat.AUTO`" placeholder). |
| 122 | |
| 123 | :param cls: The type to manage. |
| 124 | :param dumper: The dumper to register for `!cls`. |
| 125 | |
| 126 | If `!cls` is specified as string it will be lazy-loaded, so that it |
| 127 | will be possible to register it without importing it before. In this |
| 128 | case it should be the fully qualified name of the object (e.g. |
| 129 | ``"uuid.UUID"``). |
| 130 | |
| 131 | If `!cls` is None, only use the dumper when looking up using |
| 132 | `get_dumper_by_oid()`, which happens when we know the Postgres type to |
| 133 | adapt to, but not the Python type that will be adapted (e.g. in COPY |
| 134 | after using `~psycopg.Copy.set_types()`). |
| 135 | |
| 136 | """ |
| 137 | if not (cls is None or isinstance(cls, (str, type))): |
| 138 | raise TypeError( |
| 139 | f"dumpers should be registered on classes, got {cls} instead" |
| 140 | ) |
| 141 | |
| 142 | if _psycopg: |
| 143 | dumper = self._get_optimised(dumper) |
| 144 | |
| 145 | # Register the dumper both as its format and as auto |
| 146 | # so that the last dumper registered is used in auto (%s) format |
| 147 | if cls: |
| 148 | for fmt in (PyFormat.from_pq(dumper.format), PyFormat.AUTO): |
| 149 | if not self._own_dumpers[fmt]: |
| 150 | self._dumpers[fmt] = self._dumpers[fmt].copy() |
| 151 | self._own_dumpers[fmt] = True |
| 152 | |
| 153 | self._dumpers[fmt][cls] = dumper |
| 154 | |
| 155 | # Register the dumper by oid, if the oid of the dumper is fixed |
| 156 | if dumper.oid: |
| 157 | if not self._own_dumpers_by_oid[dumper.format]: |
| 158 | self._dumpers_by_oid[dumper.format] = self._dumpers_by_oid[ |
| 159 | dumper.format |
| 160 | ].copy() |
| 161 | self._own_dumpers_by_oid[dumper.format] = True |
| 162 | |
| 163 | self._dumpers_by_oid[dumper.format][dumper.oid] = dumper |
| 164 | |
| 165 | def register_loader(self, oid: int | str, loader: type[Loader]) -> None: |
| 166 | """ |