From: Daniele Varrazzo Date: Fri, 27 Dec 2024 02:30:28 +0000 (+0100) Subject: lint: add typing annotations to pass pre-commit X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=137b51f85cd95d796d242541921fc057e181e19f;p=thirdparty%2Fpsycopg.git 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. --- diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 06cacf82d..329b09d4f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,5 +22,5 @@ repos: - id: mypy name: mypy language: system - entry: mypy --pretty + entry: mypy --pretty --follow-imports=silent files: \.py[i]?$ diff --git a/psycopg/psycopg/_cmodule.py b/psycopg/psycopg/_cmodule.py index 33162bc10..b3abf12fd 100644 --- a/psycopg/psycopg/_cmodule.py +++ b/psycopg/psycopg/_cmodule.py @@ -1,3 +1,4 @@ +# mypy: disable-error-code="import-not-found, attr-defined" """ Simplify access to the _psycopg module """ @@ -6,19 +7,29 @@ Simplify access to the _psycopg module from __future__ import annotations +from types import ModuleType from . import pq __version__: str | None = None +_psycopg: ModuleType # Note: "c" must the first attempt so that mypy associates the variable the # right module interface. It will not result Optional, but hey. if pq.__impl__ == "c": - from psycopg_c import _psycopg as _psycopg - from psycopg_c import __version__ as __version__ # noqa: F401 + import psycopg_c._psycopg + + _psycopg = psycopg_c._psycopg + __version__ = psycopg_c.__version__ + elif pq.__impl__ == "binary": - from psycopg_binary import _psycopg as _psycopg # type: ignore - from psycopg_binary import __version__ as __version__ # type: ignore # noqa: F401 + import psycopg_binary._psycopg + + _psycopg = psycopg_binary._psycopg + __version__ = psycopg_binary.__version__ + elif pq.__impl__ == "python": - _psycopg = None # type: ignore + + _psycopg = None # type: ignore[assignment] + else: raise ImportError(f"can't find _psycopg optimised module in {pq.__impl__!r}") diff --git a/psycopg/psycopg/_connection_base.py b/psycopg/psycopg/_connection_base.py index ab34a7cc0..90cd237ca 100644 --- a/psycopg/psycopg/_connection_base.py +++ b/psycopg/psycopg/_connection_base.py @@ -482,7 +482,7 @@ class BaseConnection(Generic[Row]): else: self.pgconn.send_query_params(command, None, result_format=result_format) - result = (yield from generators.execute(self.pgconn))[-1] + result: PGresult = (yield from generators.execute(self.pgconn))[-1] if result.status != COMMAND_OK and result.status != TUPLES_OK: if result.status == FATAL_ERROR: raise e.error_from_result(result, encoding=self.pgconn._encoding) diff --git a/psycopg/psycopg/_copy_base.py b/psycopg/psycopg/_copy_base.py index 9217a90e1..21608c972 100644 --- a/psycopg/psycopg/_copy_base.py +++ b/psycopg/psycopg/_copy_base.py @@ -216,10 +216,11 @@ class TextFormatter(Formatter): self._encoding = encoding def parse_row(self, data: Buffer) -> tuple[Any, ...] | None: + rv: tuple[Any, ...] | None = None if data: - return parse_row_text(data, self.transformer) - else: - return None + rv = parse_row_text(data, self.transformer) + + return rv def write(self, buffer: Buffer | str) -> Buffer: data = self._ensure_bytes(buffer) @@ -260,6 +261,8 @@ class BinaryFormatter(Formatter): self._signature_sent = False def parse_row(self, data: Buffer) -> tuple[Any, ...] | None: + rv: tuple[Any, ...] | None = None + if not self._signature_sent: if data[: len(_binary_signature)] != _binary_signature: raise e.DataError( @@ -268,10 +271,10 @@ class BinaryFormatter(Formatter): self._signature_sent = True data = data[len(_binary_signature) :] - elif data == _binary_trailer: - return None + if data != _binary_trailer: + rv = parse_row_binary(data, self.transformer) - return parse_row_binary(data, self.transformer) + return rv def write(self, buffer: Buffer | str) -> Buffer: data = self._ensure_bytes(buffer) diff --git a/psycopg/psycopg/_cursor_base.py b/psycopg/psycopg/_cursor_base.py index e09f17ab0..45b600674 100644 --- a/psycopg/psycopg/_cursor_base.py +++ b/psycopg/psycopg/_cursor_base.py @@ -334,7 +334,7 @@ class BaseCursor(Generic[ConnectionType, Row]): yield from send(self._pgconn) def _stream_fetchone_gen(self, first: bool) -> PQGen[PGresult | None]: - res = yield from fetch(self._pgconn) + res: PGresult | None = yield from fetch(self._pgconn) if res is None: return None diff --git a/pyproject.toml b/pyproject.toml index 4e76bc8e6..ce539c1d4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,6 +50,7 @@ exclude = '''(?x)( [[tool.mypy.overrides]] module = [ "numpy.*", + "polib", "shapely.*", ] ignore_missing_imports = true diff --git a/tests/scripts/pipeline-demo.py b/tests/scripts/pipeline-demo.py index 1d44e4070..8f44e15e0 100644 --- a/tests/scripts/pipeline-demo.py +++ b/tests/scripts/pipeline-demo.py @@ -190,10 +190,7 @@ def pipeline_demo_pq(rows_to_send: int, logger: logging.Logger) -> None: ): while results_queue: fetched = waiting.wait( - pipeline_communicate( - pgconn, # type: ignore[arg-type] - commands, - ), + pipeline_communicate(pgconn, commands), pgconn.socket, ) assert not commands, commands @@ -216,10 +213,7 @@ async def pipeline_demo_pq_async(rows_to_send: int, logger: logging.Logger) -> N ): while results_queue: fetched = await waiting.wait_async( - pipeline_communicate( - pgconn, # type: ignore[arg-type] - commands, - ), + pipeline_communicate(pgconn, commands), pgconn.socket, ) assert not commands, commands diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 376f0de26..49f9c46ec 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -427,10 +427,7 @@ def test_optimised_adapters(): obj = getattr(_psycopg, n) if not isinstance(obj, type): continue - if not issubclass( - obj, - (_psycopg.CDumper, _psycopg.CLoader), # type: ignore[attr-defined] - ): + if not issubclass(obj, (_psycopg.CDumper, _psycopg.CLoader)): continue c_adapters[n] = obj diff --git a/tests/types/test_array.py b/tests/types/test_array.py index 7831a15eb..c1df5dae5 100644 --- a/tests/types/test_array.py +++ b/tests/types/test_array.py @@ -8,6 +8,7 @@ from decimal import Decimal import pytest import psycopg +import psycopg.types.numeric from psycopg import pq from psycopg import sql from psycopg.adapt import PyFormat, Transformer, Dumper