]> 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:52:11 +0000 (09:52 -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
(cherry picked from commit 408c936c77c6aaeceab0e0b001ed745ceb9d19d4)

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 bbc64cf710affd6c3e924861ea7d53c46f9187fb..7ba996a4a2dc153c7475b1458826f2e9d8fb443c 100644 (file)
@@ -1824,6 +1824,10 @@ class UUID(sqltypes.TypeEngine):
 
             return process
 
+    @property
+    def python_type(self):
+        return _python_UUID if self.as_uuid else str
+
 
 PGUuid = UUID
 
index 8ec345d170ab2cb5273d24106fb408645459e531..fe39672627090015b01b37d6a76db615d9e7503b 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"