From: Daniele Varrazzo Date: Fri, 4 Nov 2022 15:47:08 +0000 (+0100) Subject: fix: use a correct recursive definition for DumperKey X-Git-Tag: pool-3.1.4~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2e924a2900561111f0806c00adf2fcc84229849f;p=thirdparty%2Fpsycopg.git fix: use a correct recursive definition for DumperKey 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. --- diff --git a/psycopg/psycopg/abc.py b/psycopg/psycopg/abc.py index 570a22363..2ed345de4 100644 --- a/psycopg/psycopg/abc.py +++ b/psycopg/psycopg/abc.py @@ -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 diff --git a/psycopg/psycopg/adapt.py b/psycopg/psycopg/adapt.py index 989ab1b89..7ec4a5597 100644 --- a/psycopg/psycopg/adapt.py +++ b/psycopg/psycopg/adapt.py @@ -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. diff --git a/psycopg/psycopg/types/array.py b/psycopg/psycopg/types/array.py index 566f253c8..202cd5e3e 100644 --- a/psycopg/psycopg/types/array.py +++ b/psycopg/psycopg/types/array.py @@ -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 diff --git a/psycopg/psycopg/types/multirange.py b/psycopg/psycopg/types/multirange.py index 7b9f54570..3eaa7f127 100644 --- a/psycopg/psycopg/types/multirange.py +++ b/psycopg/psycopg/types/multirange.py @@ -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,) diff --git a/psycopg/psycopg/types/range.py b/psycopg/psycopg/types/range.py index ade9b37ce..c41848023 100644 --- a/psycopg/psycopg/types/range.py +++ b/psycopg/psycopg/types/range.py @@ -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,) diff --git a/pyproject.toml b/pyproject.toml index 72bb520dc..5448777ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,7 @@ files = [ warn_unused_ignores = true show_error_codes = true strict = true +enable_recursive_aliases = true [[tool.mypy.overrides]] module = [ diff --git a/tests/adapters_example.py b/tests/adapters_example.py index 8930b1a68..a184e6aa4 100644 --- a/tests/adapters_example.py +++ b/tests/adapters_example.py @@ -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