]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
First pass of mypy annotation
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 29 Mar 2020 05:03:07 +0000 (18:03 +1300)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 29 Mar 2020 05:03:07 +0000 (18:03 +1300)
Just fix what doesn't make mypy happy

psycopg3/adaptation.py
psycopg3/pq/_pq_ctypes.py
tox.ini

index 3a5d6153433c13e53c99dc10de2a2d98de8fc840..710c30f2f72133770133e187fbe6d1eb326a1384 100644 (file)
@@ -5,6 +5,7 @@ Entry point into the adaptation system.
 # Copyright (C) 2020 The Psycopg Team
 
 import codecs
+from typing import Dict, Tuple
 from functools import partial
 
 from . import exceptions as exc
@@ -15,7 +16,7 @@ from .connection import BaseConnection
 
 
 class Adapter:
-    globals = {}
+    globals: Dict[Tuple[type, Format], "Adapter"] = {}  # TODO: incomplete type
 
     def __init__(self, cls, conn):
         self.cls = cls
@@ -62,7 +63,8 @@ class Adapter:
 
 
 class Typecaster:
-    globals = {}
+    # TODO: incomplete type
+    globals: Dict[Tuple[type, Format], "Typecaster"] = {}
 
     def __init__(self, oid, conn):
         self.oid = oid
index 16177776c1e0ec94682ebcc8a60a2807c9198ac5..31887b2418dc9233ac202aa8578f0dd58fbb7f30 100644 (file)
@@ -8,10 +8,15 @@ import ctypes
 import ctypes.util
 from ctypes import Structure, POINTER
 from ctypes import c_char, c_char_p, c_int, c_uint, c_void_p
+from typing import List, Tuple
 
 from psycopg3.exceptions import NotSupportedError
 
-pq = ctypes.pydll.LoadLibrary(ctypes.util.find_library("pq"))
+libname = ctypes.util.find_library("pq")
+if libname is None:
+    raise ImportError("libpq library not found")
+
+pq = ctypes.pydll.LoadLibrary(libname)
 
 # Get the libpq version to define what functions are available.
 
@@ -29,11 +34,11 @@ Oid = c_uint
 
 
 class PGconn_struct(Structure):
-    _fields_ = []
+    _fields_: List[Tuple[str, type]] = []
 
 
 class PGresult_struct(Structure):
-    _fields_ = []
+    _fields_: List[Tuple[str, type]] = []
 
 
 class PQconninfoOption_struct(Structure):
@@ -131,13 +136,18 @@ PQhost = pq.PQhost
 PQhost.argtypes = [PGconn_ptr]
 PQhost.restype = c_char_p
 
+_PQhostaddr = None
+
 if libpq_version >= 120000:
-    PQhostaddr = pq.PQhostaddr
-    PQhostaddr.argtypes = [PGconn_ptr]
-    PQhostaddr.restype = c_char_p
-else:
+    _PQhostaddr = pq.PQhostaddr
+    _PQhostaddr.argtypes = [PGconn_ptr]
+    _PQhostaddr.restype = c_char_p
+
 
-    def PQhostaddr(pgconn):
+def PQhostaddr(pgconn):
+    if _PQhostaddr is not None:
+        return _PQhostaddr(pgconn)
+    else:
         raise NotSupportedError(
             f"PQhostaddr requires libpq from PostgreSQL 12,"
             f" {libpq_version} available instead"
diff --git a/tox.ini b/tox.ini
index 95334dd6ec75f62d8050389eec5125404d4a0ccf..4ccd7e941e6a459467de9f97edbf8c9732684af6 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
 [tox]
-envlist = py{36,37,38}, black, flake8
+envlist = py{36,37,38}, black, flake8, mypy
 
 [testenv]
 commands = pytest {posargs}
@@ -14,6 +14,10 @@ deps = black
 commands = flake8
 deps = flake8 >= 3.7
 
+[testenv:mypy]
+commands = mypy psycopg3
+deps = mypy >= 0.770
+
 [flake8]
 max-line-length = 85
 exclude = env, .tox