]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: use a correct recursive definition for DumperKey
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 4 Nov 2022 15:47:08 +0000 (16:47 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 4 Nov 2022 15:52:22 +0000 (16:52 +0100)
Already supported in current Mypy: https://github.com/python/mypy/issues/731.

The definition of DumperKey was wrong anyway, in a way causing mypy to
crash: see https://github.com/python/mypy/issues/14000.

psycopg/psycopg/abc.py
psycopg/psycopg/adapt.py
psycopg/psycopg/types/array.py
psycopg/psycopg/types/multirange.py
psycopg/psycopg/types/range.py
pyproject.toml
tests/adapters_example.py

index 570a22363247f470101d6fa017ad4cf8dfdaebaf..2ed345de49fea9887317bd821938b6c8d9d0aa6f 100644 (file)
@@ -29,10 +29,7 @@ Query: TypeAlias = Union[LiteralString, bytes, "sql.SQL", "sql.Composed"]
 Params: TypeAlias = Union[Sequence[Any], Mapping[str, Any]]
 ConnectionType = TypeVar("ConnectionType", bound="BaseConnection[Any]")
 PipelineCommand: TypeAlias = Callable[[], None]
-
-# TODO: make it recursive when mypy will support it
-# DumperKey: TypeAlias = Union[type, Tuple[Union[type, "DumperKey"]]]
-DumperKey: TypeAlias = Union[type, Tuple[type, ...]]
+DumperKey: TypeAlias = Union[type, Tuple["DumperKey", ...]]
 
 # Waiting protocol types
 
index 989ab1b89e421937974d5d2701000d2d636addfb..7ec4a5597e79b55b2fefce36b0adba4e0cb29a83 100644 (file)
@@ -5,7 +5,7 @@ Entry point into the adaptation system.
 # Copyright (C) 2020 The Psycopg Team
 
 from abc import ABC, abstractmethod
-from typing import Any, Optional, Type, Tuple, Union, TYPE_CHECKING
+from typing import Any, Optional, Type, TYPE_CHECKING
 
 from . import pq, abc
 from . import _adapters_map
@@ -91,7 +91,7 @@ class Dumper(abc.Dumper, ABC):
             rv = rv.replace(b"\\", b"\\\\")
         return rv
 
-    def get_key(self, obj: Any, format: PyFormat) -> Union[type, Tuple[type, ...]]:
+    def get_key(self, obj: Any, format: PyFormat) -> abc.DumperKey:
         """
         Implementation of the `~psycopg.abc.Dumper.get_key()` member of the
         `~psycopg.abc.Dumper` protocol. Look at its definition for details.
index 566f253c8bb7d6257038630025c5833962f9ef96..202cd5e3e3857f5f5ff40bc07ce5c7bbfd442669 100644 (file)
@@ -121,7 +121,7 @@ class ListDumper(BaseListDumper):
             return self.cls
 
         sd = self._tx.get_dumper(item, format)
-        return (self.cls, sd.get_key(item, format))  # type: ignore
+        return (self.cls, sd.get_key(item, format))
 
     def upgrade(self, obj: List[Any], format: PyFormat) -> "BaseListDumper":
         # If we have an oid we don't need to upgrade
@@ -223,7 +223,7 @@ class ListBinaryDumper(BaseListDumper):
             return (self.cls,)
 
         sd = self._tx.get_dumper(item, format)
-        return (self.cls, sd.get_key(item, format))  # type: ignore
+        return (self.cls, sd.get_key(item, format))
 
     def upgrade(self, obj: List[Any], format: PyFormat) -> "BaseListDumper":
         # If we have an oid we don't need to upgrade
index 7b9f545706af0d8bb0f4626fef1ef6c20a9ea728..3eaa7f127a04746dd56b85ad9728c8930dd36806 100644 (file)
@@ -154,7 +154,7 @@ class BaseMultirangeDumper(RecursiveDumper):
         item = self._get_item(obj)
         if item is not None:
             sd = self._tx.get_dumper(item, self._adapt_format)
-            return (self.cls, sd.get_key(item, format))  # type: ignore
+            return (self.cls, sd.get_key(item, format))
         else:
             return (self.cls,)
 
index ade9b37cee818ffbb95076f525e39375fe12f8ee..c41848023d03e929e17e47ecb64af3a2769780bb 100644 (file)
@@ -259,7 +259,7 @@ class BaseRangeDumper(RecursiveDumper):
         item = self._get_item(obj)
         if item is not None:
             sd = self._tx.get_dumper(item, self._adapt_format)
-            return (self.cls, sd.get_key(item, format))  # type: ignore
+            return (self.cls, sd.get_key(item, format))
         else:
             return (self.cls,)
 
index 72bb520dc5b1dc18d2de3a47eb5335ec9172bc81..5448777ea25a300f408e50808ffa513280cf2c82 100644 (file)
@@ -35,6 +35,7 @@ files = [
 warn_unused_ignores = true
 show_error_codes = true
 strict = true
+enable_recursive_aliases = true
 
 [[tool.mypy.overrides]]
 module = [
index 8930b1a689c134ddfd54b8a78e570adfa0c98e9a..a184e6aa418124cd03082d48b964fd5241bb4198 100644 (file)
@@ -1,4 +1,4 @@
-from typing import Optional, Tuple, Union
+from typing import Optional
 
 from psycopg import pq
 from psycopg.abc import Dumper, Loader, AdaptContext, PyFormat, Buffer
@@ -43,9 +43,3 @@ class MyTextLoader:
 
     def load(self, data: Buffer) -> str:
         return (bytes(data) * 2).decode()
-
-
-# This should be the definition of psycopg.adapt.DumperKey, but mypy doesn't
-# support recursive types. When it will, this statement will give an error
-# (unused type: ignore) so we can fix our definition.
-_DumperKey = Union[type, Tuple[Union[type, "_DumperKey"]]]  # type: ignore