From: Daniele Varrazzo Date: Sun, 29 Mar 2020 15:36:14 +0000 (+1300) Subject: Added Oid type X-Git-Tag: 3.0.dev0~646 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=11e926abdf109189b2ed8d55e67c1e7b1e4022fc;p=thirdparty%2Fpsycopg.git Added Oid type --- diff --git a/psycopg3/adaptation.py b/psycopg3/adaptation.py index b776da02f..5ba57a649 100644 --- a/psycopg3/adaptation.py +++ b/psycopg3/adaptation.py @@ -23,21 +23,21 @@ from .pq import Format, PGresult from .cursor import BaseCursor from .types.oids import type_oid, INVALID_OID from .connection import BaseConnection -from .utils.typing import DecodeFunc +from .utils.typing import DecodeFunc, Oid # Type system AdaptContext = Union[BaseConnection, BaseCursor] -MaybeOid = Union[Optional[bytes], Tuple[Optional[bytes], int]] +MaybeOid = Union[Optional[bytes], Tuple[Optional[bytes], Oid]] AdapterFunc = Callable[[Any], MaybeOid] AdapterType = Union["Adapter", AdapterFunc] AdaptersMap = Dict[Tuple[type, Format], AdapterType] TypecasterFunc = Callable[[bytes], Any] TypecasterType = Union["Typecaster", TypecasterFunc] -TypecastersMap = Dict[Tuple[int, Format], TypecasterType] +TypecastersMap = Dict[Tuple[Oid, Format], TypecasterType] class Adapter: @@ -47,7 +47,7 @@ class Adapter: self.cls = cls self.conn = conn - def adapt(self, obj: Any) -> Union[bytes, Tuple[bytes, int]]: + def adapt(self, obj: Any) -> Union[bytes, Tuple[bytes, Oid]]: raise NotImplementedError() @staticmethod @@ -111,7 +111,7 @@ class Adapter: class Typecaster: globals: TypecastersMap = {} - def __init__(self, oid: int, conn: Optional[BaseConnection]): + def __init__(self, oid: Oid, conn: Optional[BaseConnection]): self.oid = oid self.conn = conn @@ -120,7 +120,7 @@ class Typecaster: @staticmethod def register( - oid: int, + oid: Oid, caster: TypecasterType, context: Optional[AdaptContext] = None, format: Format = Format.TEXT, @@ -153,14 +153,14 @@ class Typecaster: @staticmethod def register_binary( - oid: int, + oid: Oid, caster: TypecasterType, context: Optional[AdaptContext] = None, ) -> TypecasterType: return Typecaster.register(oid, caster, context, format=Format.BINARY) @staticmethod - def text(oid: int) -> Callable[[Any], Any]: + def text(oid: Oid) -> Callable[[Any], Any]: def register_caster_(caster: TypecasterType) -> TypecasterType: Typecaster.register(oid, caster) return caster @@ -168,7 +168,7 @@ class Typecaster: return register_caster_ @staticmethod - def binary(oid: int) -> Callable[[Any], Any]: + def binary(oid: Oid) -> Callable[[Any], Any]: def register_binary_caster_(caster: TypecasterType) -> TypecasterType: Typecaster.register_binary(oid, caster) return caster @@ -235,7 +235,7 @@ class Transformer: def adapt_sequence( self, objs: Sequence[Any], fmts: Sequence[Format] - ) -> Tuple[List[Optional[bytes]], List[int]]: + ) -> Tuple[List[Optional[bytes]], List[Oid]]: out = [] types = [] @@ -299,7 +299,7 @@ class Transformer: v = func(v) yield v - def get_cast_function(self, oid: int, fmt: Format) -> TypecasterFunc: + def get_cast_function(self, oid: Oid, fmt: Format) -> TypecasterFunc: try: return self._cast_funcs[oid, fmt] except KeyError: @@ -311,7 +311,7 @@ class Transformer: else: return cast(TypecasterFunc, caster) - def lookup_caster(self, oid: int, fmt: Format) -> TypecasterType: + def lookup_caster(self, oid: Oid, fmt: Format) -> TypecasterType: key = (oid, fmt) cur = self.cursor @@ -334,7 +334,7 @@ class UnknownCaster(Typecaster): Fallback object to convert unknown types to Python """ - def __init__(self, oid: int, conn: Optional[BaseConnection]): + def __init__(self, oid: Oid, conn: Optional[BaseConnection]): super().__init__(oid, conn) self.decode: DecodeFunc if conn is not None: diff --git a/psycopg3/pq/pq_ctypes.py b/psycopg3/pq/pq_ctypes.py index 0f1b47816..a7917e8ee 100644 --- a/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/pq/pq_ctypes.py @@ -24,6 +24,7 @@ from .enums import ( from .misc import error_message, ConninfoOption from . import _pq_ctypes as impl from ..exceptions import OperationalError +from ..utils.typing import Oid def version() -> int: @@ -196,7 +197,7 @@ class PGconn: self, command: bytes, param_values: List[Optional[bytes]], - param_types: Optional[List[int]] = None, + param_types: Optional[List[Oid]] = None, param_formats: Optional[List[Format]] = None, result_format: Format = Format.TEXT, ) -> "PGresult": @@ -212,7 +213,7 @@ class PGconn: self, command: bytes, param_values: List[Optional[bytes]], - param_types: Optional[List[int]] = None, + param_types: Optional[List[Oid]] = None, param_formats: Optional[List[Format]] = None, result_format: Format = Format.TEXT, ) -> None: @@ -228,7 +229,7 @@ class PGconn: self, command: bytes, param_values: List[Optional[bytes]], - param_types: Optional[List[int]] = None, + param_types: Optional[List[Oid]] = None, param_formats: Optional[List[Format]] = None, result_format: Format = Format.TEXT, ) -> Any: @@ -279,7 +280,7 @@ class PGconn: self, name: bytes, command: bytes, - param_types: Optional[List[int]] = None, + param_types: Optional[List[Oid]] = None, ) -> "PGresult": if not isinstance(name, bytes): raise TypeError(f"'name' must be bytes, got {type(name)} instead") @@ -442,7 +443,7 @@ class PGresult: def fformat(self, column_number: int) -> Format: return impl.PQfformat(self.pgresult_ptr, column_number) # type: ignore - def ftype(self, column_number: int) -> int: + def ftype(self, column_number: int) -> Oid: return impl.PQftype(self.pgresult_ptr, column_number) # type: ignore def fmod(self, column_number: int) -> int: @@ -452,8 +453,8 @@ class PGresult: return impl.PQfsize(self.pgresult_ptr, column_number) # type: ignore @property - def binary_tuples(self) -> int: - return impl.PQbinaryTuples(self.pgresult_ptr) # type: ignore + def binary_tuples(self) -> Format: + return Format(impl.PQbinaryTuples(self.pgresult_ptr)) def get_value( self, row_number: int, column_number: int @@ -474,7 +475,7 @@ class PGresult: def nparams(self) -> int: return impl.PQnparams(self.pgresult_ptr) # type: ignore - def param_type(self, param_number: int) -> int: + def param_type(self, param_number: int) -> Oid: return impl.PQparamtype( # type: ignore self.pgresult_ptr, param_number ) diff --git a/psycopg3/types/oids.py b/psycopg3/types/oids.py index d81b2c865..6a4651bec 100644 --- a/psycopg3/types/oids.py +++ b/psycopg3/types/oids.py @@ -8,8 +8,10 @@ to a Postgres server. # Copyright (C) 2020 The Psycopg Team import re +from typing import Dict +from ..utils.typing import Oid -INVALID_OID = 0 +INVALID_OID = Oid(0) # typname, oid, array oid, regtype @@ -89,7 +91,7 @@ _oids_table = [ # autogenerated end ] -type_oid = {name: oid for name, oid, _, _ in _oids_table} +type_oid: Dict[str, Oid] = {name: Oid(oid) for name, oid, _, _ in _oids_table} def self_update() -> None: diff --git a/psycopg3/types/text.py b/psycopg3/types/text.py index 20910e71c..986fdea39 100644 --- a/psycopg3/types/text.py +++ b/psycopg3/types/text.py @@ -12,7 +12,7 @@ from ..adaptation import ( Typecaster, ) from ..connection import BaseConnection -from ..utils.typing import EncodeFunc, DecodeFunc +from ..utils.typing import EncodeFunc, DecodeFunc, Oid from .oids import type_oid @@ -38,7 +38,7 @@ class StringCaster(Typecaster): decode: Optional[DecodeFunc] - def __init__(self, oid: int, conn: BaseConnection): + def __init__(self, oid: Oid, conn: BaseConnection): super().__init__(oid, conn) if conn is not None: diff --git a/psycopg3/utils/typing.py b/psycopg3/utils/typing.py index f96576ce8..00506abe8 100644 --- a/psycopg3/utils/typing.py +++ b/psycopg3/utils/typing.py @@ -4,7 +4,9 @@ Additional types for checking # Copyright (C) 2020 The Psycopg Team -from typing import Any, Callable, Mapping, Sequence, Tuple, Union +from typing import Any, Callable, Mapping, NewType, Sequence, Tuple, Union + +Oid = NewType("Oid", int) EncodeFunc = Callable[[str], Tuple[bytes, int]] DecodeFunc = Callable[[bytes], Tuple[str, int]]