From 0f311702098e90c283c8be674ee75031e534d4d9 Mon Sep 17 00:00:00 2001 From: Brian Bjarke Jensen Date: Sun, 28 Jun 2026 23:32:31 +0200 Subject: [PATCH] Fix mypy errors from minio 7.2.20 type stubs. Guard bucket_name before Minio API calls, update S3Error test mocks for the new constructor signature, and add types-redis to the pre-commit mypy hook. Co-authored-by: Cursor --- .pre-commit-config.yaml | 2 ++ python_repositories/adapters/minio_adapter.py | 8 ++--- tests/integration/minio_adapter_test.py | 31 ++++++++++--------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b7e0522..8b3462b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,6 +24,8 @@ repos: rev: v1.8.0 hooks: - id: mypy + additional_dependencies: + - types-redis # Python syntax modernization with pyupgrade - repo: https://github.com/asottile/pyupgrade diff --git a/python_repositories/adapters/minio_adapter.py b/python_repositories/adapters/minio_adapter.py index afcabb3..c255769 100644 --- a/python_repositories/adapters/minio_adapter.py +++ b/python_repositories/adapters/minio_adapter.py @@ -131,7 +131,7 @@ class MinioAdapter( if not isinstance(content_type, str) or len(content_type) == 0: raise ValueError("content_type must be a non-empty string") # Check connection - if self._client is None or not self.is_connected: + if self._client is None or self._bucket_name is None or not self.is_connected: raise ConnectionError("Not connected to Minio") # Prepare buffer for reading num_bytes = data.getbuffer().nbytes @@ -156,7 +156,7 @@ class MinioAdapter( if not isinstance(object_name, str) or len(object_name) == 0: raise ValueError("object_name must be a non-empty string") # Check connection - if self._client is None or not self.is_connected: + if self._client is None or self._bucket_name is None or not self.is_connected: raise ConnectionError("Not connected to Minio") # Get data from bucket # N.B. bucket name is set when connecting @@ -191,7 +191,7 @@ class MinioAdapter( if not isinstance(object_name, str) or len(object_name) == 0: raise ValueError("object_name must be a non-empty string") # Check connection - if self._client is None or not self.is_connected: + if self._client is None or self._bucket_name is None or not self.is_connected: raise ConnectionError("Not connected to Minio") # Delete object from bucket # N.B. bucket name is set when connecting @@ -210,7 +210,7 @@ class MinioAdapter( raise ValueError("prefix must be a string") # Check connection # N.B. bucket name is set when connecting - if self._client is None or not self.is_connected: + if self._client is None or self._bucket_name is None or not self.is_connected: raise ConnectionError("Not connected to Minio") # List objects in bucket objects = self._client.list_objects( diff --git a/tests/integration/minio_adapter_test.py b/tests/integration/minio_adapter_test.py index fa66a47..3d8586f 100644 --- a/tests/integration/minio_adapter_test.py +++ b/tests/integration/minio_adapter_test.py @@ -9,6 +9,7 @@ import random import os import logging from minio import S3Error +from urllib3.response import BaseHTTPResponse from python_repositories.adapters.minio_adapter import MinioAdapter from python_repositories.interfaces import ObjectRepositoryInterface @@ -42,7 +43,7 @@ def same_data( @pytest.fixture(scope="module") -def data() -> Generator[BytesIO]: +def data() -> Generator[BytesIO, None, None]: """Provide a sample data bytes for tests.""" # Generate random bytes random_bytes = random.randbytes(2**21) # 2 MiB @@ -53,7 +54,7 @@ def data() -> Generator[BytesIO]: def data_in_minio( raw_minio_client: Minio, data: BytesIO, -) -> Generator[tuple[str, BytesIO]]: +) -> Generator[tuple[str, BytesIO], None, None]: """Fixture to set up a known value in Minio before each test.""" object_name = "test_object" bucket_name = str(os.getenv("MINIO_BUCKET")) @@ -77,7 +78,7 @@ def data_in_minio( @pytest.fixture(scope="module") -def minio_adapter() -> Generator[MinioAdapter]: +def minio_adapter() -> Generator[MinioAdapter, None, None]: """Fixture to provide a connected MinioAdapter instance.""" adapter = MinioAdapter() adapter.connect() @@ -234,12 +235,12 @@ def test_should_log_warning_when_getting_nonexistent_object( adapter = MinioAdapter() adapter._client = MagicMock(spec=Minio) adapter._client.get_object.side_effect = S3Error( - code="NoSuchKey", - message="", - resource="", - request_id="", - host_id="", - response="", + MagicMock(spec=BaseHTTPResponse), + "NoSuchKey", + "", + "", + "", + "", bucket_name="test-bucket", object_name="missing-object", ) @@ -264,12 +265,12 @@ def test_should_log_error_when_getting_with_s3error_other_than_no_such_key( adapter = MinioAdapter() adapter._client = MagicMock(spec=Minio) other_s3error = S3Error( - code="UnhandledError", - message="", - resource="", - request_id="", - host_id="", - response="", + MagicMock(spec=BaseHTTPResponse), + "UnhandledError", + "", + "", + "", + "", bucket_name="test-bucket", object_name="missing-object", )