From: Daniele Varrazzo Date: Fri, 3 Apr 2020 11:58:34 +0000 (+1300) Subject: Fixed TypeCasterType and AdapterType types X-Git-Tag: 3.0.dev0~616 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4f2845a9b4bf97945fd65d2ea2b4f7a32986e17;p=thirdparty%2Fpsycopg.git Fixed TypeCasterType and AdapterType types The previous signature meant an instance of Adapter/TypeCaster or a function, but we meant a class actually. The error costed some cursing and some casting to the right type (in mypy sense, not in psyco sense) --- diff --git a/psycopg3/adapt.py b/psycopg3/adapt.py index 4ddd50190..d9e46b8be 100644 --- a/psycopg3/adapt.py +++ b/psycopg3/adapt.py @@ -5,9 +5,8 @@ Entry point into the adaptation system. # Copyright (C) 2020 The Psycopg Team import codecs -from typing import cast from typing import Any, Callable, Dict, Generator, List, Optional, Sequence -from typing import Tuple, Union +from typing import Tuple, Type, Union from . import errors as e from .pq import Format, PGresult @@ -25,18 +24,18 @@ AdaptContext = Union[None, BaseConnection, BaseCursor] MaybeOid = Union[Optional[bytes], Tuple[Optional[bytes], int]] AdapterFunc = Callable[[Any], MaybeOid] -AdapterType = Union["Adapter", AdapterFunc] +AdapterType = Union[Type["Adapter"], AdapterFunc] AdaptersMap = Dict[Tuple[type, Format], AdapterType] TypeCasterFunc = Callable[[bytes], Any] -TypeCasterType = Union["TypeCaster", TypeCasterFunc] +TypeCasterType = Union[Type["TypeCaster"], TypeCasterFunc] TypeCastersMap = Dict[Tuple[int, Format], TypeCasterType] class Adapter: globals: AdaptersMap = {} - def __init__(self, cls: type, conn: BaseConnection): + def __init__(self, cls: type, conn: Optional[BaseConnection]): self.cls = cls self.conn = conn @@ -83,7 +82,7 @@ class Adapter: return Adapter.register(cls, adapter, context, format=Format.BINARY) @staticmethod - def text(cls: type) -> Callable[[Any], Any]: + def text(cls: type) -> Callable[[AdapterType], AdapterType]: def text_(adapter: AdapterType) -> AdapterType: Adapter.register(cls, adapter) return adapter @@ -91,7 +90,7 @@ class Adapter: return text_ @staticmethod - def binary(cls: type) -> Callable[[Any], Any]: + def binary(cls: type) -> Callable[[AdapterType], AdapterType]: def binary_(adapter: AdapterType) -> AdapterType: Adapter.register_binary(cls, adapter) return adapter @@ -149,7 +148,7 @@ class TypeCaster: return TypeCaster.register(oid, caster, context, format=Format.BINARY) @staticmethod - def text(oid: int) -> Callable[[Any], Any]: + def text(oid: int) -> Callable[[TypeCasterType], TypeCasterType]: def text_(caster: TypeCasterType) -> TypeCasterType: TypeCaster.register(oid, caster) return caster @@ -157,7 +156,7 @@ class TypeCaster: return text_ @staticmethod - def binary(oid: int) -> Callable[[Any], Any]: + def binary(oid: int) -> Callable[[TypeCasterType], TypeCasterType]: def binary_(caster: TypeCasterType) -> TypeCasterType: TypeCaster.register_binary(oid, caster) return caster @@ -259,7 +258,7 @@ class Transformer: if isinstance(adapter, type): return adapter(cls, self.connection).adapt else: - return cast(AdapterFunc, adapter) + return adapter def lookup_adapter(self, cls: type, fmt: Format) -> AdapterType: key = (cls, fmt) @@ -307,7 +306,7 @@ class Transformer: if isinstance(caster, type): return caster(oid, self.connection).cast else: - return cast(TypeCasterFunc, caster) + return caster def lookup_caster(self, oid: int, fmt: Format) -> TypeCasterType: key = (oid, fmt) diff --git a/psycopg3/types/array.py b/psycopg3/types/array.py index 69b1361de..2f16c6062 100644 --- a/psycopg3/types/array.py +++ b/psycopg3/types/array.py @@ -5,7 +5,7 @@ Adapters for arrays # Copyright (C) 2020 The Psycopg Team import re -from typing import Any, Callable, List, Optional, cast, TYPE_CHECKING +from typing import Any, Callable, List, Optional, TYPE_CHECKING from .. import errors as e from ..pq import Format @@ -102,7 +102,7 @@ class ArrayCasterBase(TypeCaster): if isinstance(self.base_caster, type): self.caster_func = self.base_caster(oid, conn).cast else: - self.caster_func = cast(TypeCasterFunc, type(self).base_caster) + self.caster_func = type(self).base_caster def cast(self, data: bytes) -> List[Any]: rv = None @@ -150,7 +150,7 @@ class ArrayCaster(TypeCaster): format: Format = Format.TEXT, ) -> TypeCasterType: t = type( - caster.__name__ + "_array", # type: ignore + caster.__name__ + "_array", (ArrayCasterBase,), {"base_caster": caster}, )