Skip to content

Commit 137b51f

Browse files
committed
lint: add typing annotations to pass pre-commit
Pre-commit seems to run mypy in parallel batches, each with about 20 files. In this mode, certain tests that pass when running mypy all in once or file-by-file, fail. In the current state, both running mypy without arguments or running it in pre-commit seem to work.
1 parent 0711adf commit 137b51f

9 files changed

+33
-26
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ repos:
2222
- id: mypy
2323
name: mypy
2424
language: system
25-
entry: mypy --pretty
25+
entry: mypy --pretty --follow-imports=silent
2626
files: \.py[i]?$

psycopg/psycopg/_cmodule.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# mypy: disable-error-code="import-not-found, attr-defined"
12
"""
23
Simplify access to the _psycopg module
34
"""
@@ -6,19 +7,29 @@
67

78
from __future__ import annotations
89

10+
from types import ModuleType
911
from . import pq
1012

1113
__version__: str | None = None
14+
_psycopg: ModuleType
1215

1316
# Note: "c" must the first attempt so that mypy associates the variable the
1417
# right module interface. It will not result Optional, but hey.
1518
if pq.__impl__ == "c":
16-
from psycopg_c import _psycopg as _psycopg
17-
from psycopg_c import __version__ as __version__ # noqa: F401
19+
import psycopg_c._psycopg
20+
21+
_psycopg = psycopg_c._psycopg
22+
__version__ = psycopg_c.__version__
23+
1824
elif pq.__impl__ == "binary":
19-
from psycopg_binary import _psycopg as _psycopg # type: ignore
20-
from psycopg_binary import __version__ as __version__ # type: ignore # noqa: F401
25+
import psycopg_binary._psycopg
26+
27+
_psycopg = psycopg_binary._psycopg
28+
__version__ = psycopg_binary.__version__
29+
2130
elif pq.__impl__ == "python":
22-
_psycopg = None # type: ignore
31+
32+
_psycopg = None # type: ignore[assignment]
33+
2334
else:
2435
raise ImportError(f"can't find _psycopg optimised module in {pq.__impl__!r}")

psycopg/psycopg/_connection_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ def _exec_command(
482482
else:
483483
self.pgconn.send_query_params(command, None, result_format=result_format)
484484

485-
result = (yield from generators.execute(self.pgconn))[-1]
485+
result: PGresult = (yield from generators.execute(self.pgconn))[-1]
486486
if result.status != COMMAND_OK and result.status != TUPLES_OK:
487487
if result.status == FATAL_ERROR:
488488
raise e.error_from_result(result, encoding=self.pgconn._encoding)

psycopg/psycopg/_copy_base.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ def __init__(self, transformer: Transformer, encoding: str = "utf-8"):
216216
self._encoding = encoding
217217

218218
def parse_row(self, data: Buffer) -> tuple[Any, ...] | None:
219+
rv: tuple[Any, ...] | None = None
219220
if data:
220-
return parse_row_text(data, self.transformer)
221-
else:
222-
return None
221+
rv = parse_row_text(data, self.transformer)
222+
223+
return rv
223224

224225
def write(self, buffer: Buffer | str) -> Buffer:
225226
data = self._ensure_bytes(buffer)
@@ -260,6 +261,8 @@ def __init__(self, transformer: Transformer):
260261
self._signature_sent = False
261262

262263
def parse_row(self, data: Buffer) -> tuple[Any, ...] | None:
264+
rv: tuple[Any, ...] | None = None
265+
263266
if not self._signature_sent:
264267
if data[: len(_binary_signature)] != _binary_signature:
265268
raise e.DataError(
@@ -268,10 +271,10 @@ def parse_row(self, data: Buffer) -> tuple[Any, ...] | None:
268271
self._signature_sent = True
269272
data = data[len(_binary_signature) :]
270273

271-
elif data == _binary_trailer:
272-
return None
274+
if data != _binary_trailer:
275+
rv = parse_row_binary(data, self.transformer)
273276

274-
return parse_row_binary(data, self.transformer)
277+
return rv
275278

276279
def write(self, buffer: Buffer | str) -> Buffer:
277280
data = self._ensure_bytes(buffer)

psycopg/psycopg/_cursor_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def _stream_send_gen(
334334
yield from send(self._pgconn)
335335

336336
def _stream_fetchone_gen(self, first: bool) -> PQGen[PGresult | None]:
337-
res = yield from fetch(self._pgconn)
337+
res: PGresult | None = yield from fetch(self._pgconn)
338338
if res is None:
339339
return None
340340

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ exclude = '''(?x)(
5050
[[tool.mypy.overrides]]
5151
module = [
5252
"numpy.*",
53+
"polib",
5354
"shapely.*",
5455
]
5556
ignore_missing_imports = true

tests/scripts/pipeline-demo.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,7 @@ def pipeline_demo_pq(rows_to_send: int, logger: logging.Logger) -> None:
190190
):
191191
while results_queue:
192192
fetched = waiting.wait(
193-
pipeline_communicate(
194-
pgconn, # type: ignore[arg-type]
195-
commands,
196-
),
193+
pipeline_communicate(pgconn, commands),
197194
pgconn.socket,
198195
)
199196
assert not commands, commands
@@ -216,10 +213,7 @@ async def pipeline_demo_pq_async(rows_to_send: int, logger: logging.Logger) -> N
216213
):
217214
while results_queue:
218215
fetched = await waiting.wait_async(
219-
pipeline_communicate(
220-
pgconn, # type: ignore[arg-type]
221-
commands,
222-
),
216+
pipeline_communicate(pgconn, commands),
223217
pgconn.socket,
224218
)
225219
assert not commands, commands

tests/test_adapt.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,7 @@ def test_optimised_adapters():
427427
obj = getattr(_psycopg, n)
428428
if not isinstance(obj, type):
429429
continue
430-
if not issubclass(
431-
obj,
432-
(_psycopg.CDumper, _psycopg.CLoader), # type: ignore[attr-defined]
433-
):
430+
if not issubclass(obj, (_psycopg.CDumper, _psycopg.CLoader)):
434431
continue
435432
c_adapters[n] = obj
436433

tests/types/test_array.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
import psycopg
11+
import psycopg.types.numeric
1112
from psycopg import pq
1213
from psycopg import sql
1314
from psycopg.adapt import PyFormat, Transformer, Dumper

0 commit comments

Comments
 (0)