]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added Oid type
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 29 Mar 2020 15:36:14 +0000 (04:36 +1300)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 29 Mar 2020 15:38:26 +0000 (04:38 +1300)
psycopg3/adaptation.py
psycopg3/pq/pq_ctypes.py
psycopg3/types/oids.py
psycopg3/types/text.py
psycopg3/utils/typing.py

index b776da02f18824125f394a03a2a730d53a0933b9..5ba57a649677a75c9bc06d5f0521e91db40bff72 100644 (file)
@@ -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:
index 0f1b47816e13975fb16de9977f8532f02b509824..a7917e8ee99e44ebe31e2f6932062ac04202e264 100644 (file)
@@ -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
         )
index d81b2c8651525df465696db54e36cd92b18de2f3..6a4651becad28027c7ee2bf6fb0d6976a4117de9 100644 (file)
@@ -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:
index 20910e71c2e10d2b753704f5d64c2892ffd42605..986fdea3913c2a89ea934343da1d87e0ce1206bc 100644 (file)
@@ -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:
index f96576ce88be3b98c309c42ec84cd341c91bf66b..00506abe8107427a47502a64024cd86da8cb65fe 100644 (file)
@@ -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]]