]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Implement UUID.python_type
authorAlex Grönholm <alex.gronholm@nextday.fi>
Mon, 18 Apr 2022 17:07:19 +0000 (13:07 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 21 Apr 2022 13:51:28 +0000 (09:51 -0400)
Implemented :attr:`_postgresql.UUID.python_type` attribute for the
:class:`_postgresql.UUID` type object. The attribute will return either
``str`` or ``uuid.UUID`` based on the :paramref:`_postgresql.UUID.as_uuid`
parameter setting. Previously, this attribute was unimplemented. Pull
request courtesy Alex Grönholm.

Fixes: #7943
Closes: #7944
Change-Id: Ic4fbaeee134d586b08339801968e787cc7e14285

doc/build/changelog/unreleased_14/7943.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_types.py

diff --git a/doc/build/changelog/unreleased_14/7943.rst b/doc/build/changelog/unreleased_14/7943.rst
new file mode 100644 (file)
index 0000000..e5ed12e
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, postgresql
+    :tickets: 7943
+
+    Implemented :attr:`_postgresql.UUID.python_type` attribute for the
+    :class:`_postgresql.UUID` type object. The attribute will return either
+    ``str`` or ``uuid.UUID`` based on the :paramref:`_postgresql.UUID.as_uuid`
+    parameter setting. Previously, this attribute was unimplemented. Pull
+    request courtesy Alex Grönholm.
\ No newline at end of file
index 6c3bc4e7cb450d852e2a5c1c1719dc56367f4114..5d1298cf7782a5486c1fe0be94548749f9e2743a 100644 (file)
@@ -1815,6 +1815,10 @@ class UUID(sqltypes.TypeEngine):
 
             return process
 
+    @property
+    def python_type(self):
+        return _python_UUID if self.as_uuid else str
+
 
 PGUuid = UUID
 
index a59dd0ac7e52ed0e348ef088783a73df76d9e8fb..8c4bb7fe73fc8b21f3cb43c5c6ab3f52dc0569f7 100644 (file)
@@ -2832,6 +2832,10 @@ class UUIDTest(fixtures.TestBase):
         )
         eq_(v1.fetchone()[0], value1)
 
+    def test_python_type(self):
+        eq_(postgresql.UUID(as_uuid=True).python_type, uuid.UUID)
+        eq_(postgresql.UUID(as_uuid=False).python_type, str)
+
 
 class HStoreTest(AssertsCompiledSQL, fixtures.TestBase):
     __dialect__ = "postgresql"