]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Fixed TypeCasterType and AdapterType types
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 3 Apr 2020 11:58:34 +0000 (00:58 +1300)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 3 Apr 2020 12:05:34 +0000 (01:05 +1300)
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)

psycopg3/adapt.py
psycopg3/types/array.py

index 4ddd50190eee50d96236c99bb0765bb5c75dafc1..d9e46b8be7d09a17f0c1c8d4e6cb95c8e3e0d58e 100644 (file)
@@ -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)
index 69b1361de01f7acbbdf8e4b0697810dbe95c46f7..2f16c606247c5b0202a67e78463fbac9fd49a23e 100644 (file)
@@ -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},
         )