Skip to content

Commit 066d4ed

Browse files
committed
lint: avoid mypy warning in _pq_ctypes.py
I can't find how my editor is set up, but it doesn't complain on this file, and yet calling mypy on this file only will find several error. Having seen @dlax adding `type: ignore[no-any-return]` comments makes me want to fix this.
1 parent cae87c4 commit 066d4ed

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

psycopg/psycopg/pq/_pq_ctypes.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import ctypes.util
1010
from ctypes import Structure, CFUNCTYPE, POINTER
1111
from ctypes import c_char, c_char_p, c_int, c_size_t, c_ubyte, c_uint, c_void_p
12-
from typing import List, Optional, Tuple
12+
from typing import Any, List, Optional, Tuple
1313

1414
from .misc import find_libpq_full_path
1515
from ..errors import NotSupportedError
@@ -200,7 +200,8 @@ def PQhostaddr(pgconn: PGconn_struct) -> bytes:
200200
f" {libpq_version} available instead"
201201
)
202202

203-
return _PQhostaddr(pgconn)
203+
rv: bytes = _PQhostaddr(pgconn)
204+
return rv
204205

205206

206207
PQport = pq.PQport
@@ -324,7 +325,8 @@ def PQclosePrepared(pgconn: PGconn_struct, name: str) -> int:
324325
"PQclosePrepared requires libpq from PostgreSQL 17,"
325326
f" {libpq_version} available instead"
326327
)
327-
return _PQclosePrepared(pgconn, name)
328+
rv: int = _PQclosePrepared(pgconn, name)
329+
return rv
328330

329331

330332
def PQclosePortal(pgconn: PGconn_struct, name: str) -> int:
@@ -333,7 +335,8 @@ def PQclosePortal(pgconn: PGconn_struct, name: str) -> int:
333335
"PQclosePortal requires libpq from PostgreSQL 17,"
334336
f" {libpq_version} available instead"
335337
)
336-
return _PQclosePortal(pgconn, name)
338+
rv: int = _PQclosePortal(pgconn, name)
339+
return rv
337340

338341

339342
PQresultStatus = pq.PQresultStatus
@@ -548,7 +551,8 @@ def PQsendClosePrepared(pgconn: PGconn_struct, name: str) -> int:
548551
"PQsendClosePrepared requires libpq from PostgreSQL 17,"
549552
f" {libpq_version} available instead"
550553
)
551-
return _PQsendClosePrepared(pgconn, name)
554+
rv: int = _PQsendClosePrepared(pgconn, name)
555+
return rv
552556

553557

554558
def PQsendClosePortal(pgconn: PGconn_struct, name: str) -> int:
@@ -557,7 +561,8 @@ def PQsendClosePortal(pgconn: PGconn_struct, name: str) -> int:
557561
"PQsendClosePortal requires libpq from PostgreSQL 17,"
558562
f" {libpq_version} available instead"
559563
)
560-
return _PQsendClosePortal(pgconn, name)
564+
rv: int = _PQsendClosePortal(pgconn, name)
565+
return rv
561566

562567

563568
PQgetResult = pq.PQgetResult
@@ -683,7 +688,8 @@ def PQencryptPasswordConn(
683688
f" {libpq_version} available instead"
684689
)
685690

686-
return _PQencryptPasswordConn(pgconn, passwd, user, algorithm)
691+
rv: Optional[bytes] = _PQencryptPasswordConn(pgconn, passwd, user, algorithm)
692+
return rv
687693

688694

689695
PQmakeEmptyPGresult = pq.PQmakeEmptyPGresult
@@ -739,7 +745,8 @@ def PQpipelineStatus(pgconn: PGconn_struct) -> int:
739745
"PQpipelineStatus requires libpq from PostgreSQL 14,"
740746
f" {libpq_version} available instead"
741747
)
742-
return _PQpipelineStatus(pgconn)
748+
rv: int = _PQpipelineStatus(pgconn)
749+
return rv
743750

744751

745752
def PQenterPipelineMode(pgconn: PGconn_struct) -> int:
@@ -748,7 +755,8 @@ def PQenterPipelineMode(pgconn: PGconn_struct) -> int:
748755
"PQenterPipelineMode requires libpq from PostgreSQL 14,"
749756
f" {libpq_version} available instead"
750757
)
751-
return _PQenterPipelineMode(pgconn)
758+
rv: int = _PQenterPipelineMode(pgconn)
759+
return rv
752760

753761

754762
def PQexitPipelineMode(pgconn: PGconn_struct) -> int:
@@ -757,7 +765,8 @@ def PQexitPipelineMode(pgconn: PGconn_struct) -> int:
757765
"PQexitPipelineMode requires libpq from PostgreSQL 14,"
758766
f" {libpq_version} available instead"
759767
)
760-
return _PQexitPipelineMode(pgconn)
768+
rv: int = _PQexitPipelineMode(pgconn)
769+
return rv
761770

762771

763772
def PQpipelineSync(pgconn: PGconn_struct) -> int:
@@ -766,7 +775,8 @@ def PQpipelineSync(pgconn: PGconn_struct) -> int:
766775
"PQpipelineSync requires libpq from PostgreSQL 14,"
767776
f" {libpq_version} available instead"
768777
)
769-
return _PQpipelineSync(pgconn)
778+
rv: int = _PQpipelineSync(pgconn)
779+
return rv
770780

771781

772782
def PQsendFlushRequest(pgconn: PGconn_struct) -> int:
@@ -775,7 +785,8 @@ def PQsendFlushRequest(pgconn: PGconn_struct) -> int:
775785
"PQsendFlushRequest requires libpq from PostgreSQL 14,"
776786
f" {libpq_version} available instead"
777787
)
778-
return _PQsendFlushRequest(pgconn)
788+
rv: int = _PQsendFlushRequest(pgconn)
789+
return rv
779790

780791

781792
# 33.18. SSL Support
@@ -787,9 +798,9 @@ def PQsendFlushRequest(pgconn: PGconn_struct) -> int:
787798

788799
def generate_stub() -> None:
789800
import re
790-
from ctypes import _CFuncPtr # type: ignore
801+
from ctypes import _CFuncPtr # type: ignore[attr-defined]
791802

792-
def type2str(fname, narg, t):
803+
def type2str(fname: str, narg: int | None, t: Any) -> str:
793804
if t is None:
794805
return "None"
795806
elif t is c_void_p:
@@ -810,7 +821,7 @@ def type2str(fname, narg, t):
810821
if narg is not None:
811822
return f"Optional[{t.__name__[3:]}]"
812823
else:
813-
return t.__name__[3:]
824+
return str(t.__name__[3:])
814825

815826
elif t.__name__ in ("LP_PQconninfoOption_struct",):
816827
return f"Sequence[{t.__name__[3:]}]"

0 commit comments

Comments
 (0)