From: Daniele Varrazzo Date: Fri, 3 Apr 2020 13:04:35 +0000 (+1300) Subject: Use classmethod instead of staticmethod to play better with inheritance X-Git-Tag: 3.0.dev0~614 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2b93183ba507fb16f5232dbb5116418c35bc7397;p=thirdparty%2Fpsycopg.git Use classmethod instead of staticmethod to play better with inheritance --- diff --git a/psycopg3/adapt.py b/psycopg3/adapt.py index d9e46b8be..d0f3b9d95 100644 --- a/psycopg3/adapt.py +++ b/psycopg3/adapt.py @@ -35,23 +35,24 @@ TypeCastersMap = Dict[Tuple[int, Format], TypeCasterType] class Adapter: globals: AdaptersMap = {} - def __init__(self, cls: type, conn: Optional[BaseConnection]): - self.cls = cls + def __init__(self, src: type, conn: Optional[BaseConnection]): + self.src = src self.conn = conn def adapt(self, obj: Any) -> Union[bytes, Tuple[bytes, int]]: raise NotImplementedError() - @staticmethod + @classmethod def register( - cls: type, + cls, + src: type, adapter: AdapterType, context: AdaptContext = None, format: Format = Format.TEXT, ) -> AdapterType: - if not isinstance(cls, type): + if not isinstance(src, type): raise TypeError( - f"adapters should be registered on classes, got {cls} instead" + f"adapters should be registered on classes, got {src} instead" ) if context is not None and not isinstance( @@ -72,27 +73,27 @@ class Adapter: ) where = context.adapters if context is not None else Adapter.globals - where[cls, format] = adapter + where[src, format] = adapter return adapter - @staticmethod + @classmethod def register_binary( - cls: type, adapter: AdapterType, context: AdaptContext = None, + cls, src: type, adapter: AdapterType, context: AdaptContext = None, ) -> AdapterType: - return Adapter.register(cls, adapter, context, format=Format.BINARY) + return cls.register(src, adapter, context, format=Format.BINARY) - @staticmethod - def text(cls: type) -> Callable[[AdapterType], AdapterType]: + @classmethod + def text(cls, src: type) -> Callable[[AdapterType], AdapterType]: def text_(adapter: AdapterType) -> AdapterType: - Adapter.register(cls, adapter) + cls.register(src, adapter) return adapter return text_ - @staticmethod - def binary(cls: type) -> Callable[[AdapterType], AdapterType]: + @classmethod + def binary(cls, src: type) -> Callable[[AdapterType], AdapterType]: def binary_(adapter: AdapterType) -> AdapterType: - Adapter.register_binary(cls, adapter) + cls.register_binary(src, adapter) return adapter return binary_ @@ -108,8 +109,9 @@ class TypeCaster: def cast(self, data: bytes) -> Any: raise NotImplementedError() - @staticmethod + @classmethod def register( + cls, oid: int, caster: TypeCasterType, context: AdaptContext = None, @@ -141,24 +143,24 @@ class TypeCaster: where[oid, format] = caster return caster - @staticmethod + @classmethod def register_binary( - oid: int, caster: TypeCasterType, context: AdaptContext = None, + cls, oid: int, caster: TypeCasterType, context: AdaptContext = None, ) -> TypeCasterType: - return TypeCaster.register(oid, caster, context, format=Format.BINARY) + return cls.register(oid, caster, context, format=Format.BINARY) - @staticmethod - def text(oid: int) -> Callable[[TypeCasterType], TypeCasterType]: + @classmethod + def text(cls, oid: int) -> Callable[[TypeCasterType], TypeCasterType]: def text_(caster: TypeCasterType) -> TypeCasterType: - TypeCaster.register(oid, caster) + cls.register(oid, caster) return caster return text_ - @staticmethod - def binary(oid: int) -> Callable[[TypeCasterType], TypeCasterType]: + @classmethod + def binary(cls, oid: int) -> Callable[[TypeCasterType], TypeCasterType]: def binary_(caster: TypeCasterType) -> TypeCasterType: - TypeCaster.register_binary(oid, caster) + cls.register_binary(oid, caster) return caster return binary_ diff --git a/psycopg3/types/array.py b/psycopg3/types/array.py index 2f16c6062..c0373a4bc 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, TYPE_CHECKING +from typing import Any, List, Optional, TYPE_CHECKING from .. import errors as e from ..pq import Format @@ -156,14 +156,6 @@ class ArrayCaster(TypeCaster): ) return TypeCaster.register(oid, t, context=context, format=format) - @staticmethod - def text(oid: int) -> Callable[[Any], Any]: - def text_(caster: TypeCasterType) -> TypeCasterType: - ArrayCaster.register(oid, caster, format=Format.TEXT) - return caster - - return text_ - class UnknownArrayCaster(ArrayCasterBase): base_caster = UnknownCaster