Fix mypy errors from minio 7.2.20 type stubs.
Code Quality Pipeline / code-quality (pull_request) Successful in 36s
PR Title Check / check-title (pull_request) Failing after 6s
Test Python Package / test (pull_request) Successful in 43s

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 <cursoragent@cursor.com>
This commit is contained in:
Brian Bjarke Jensen
2026-06-28 23:32:31 +02:00
co-authored by Cursor
parent e3cab5e0b6
commit 0f31170209
3 changed files with 22 additions and 19 deletions
+2
View File
@@ -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
@@ -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(
+16 -15
View File
@@ -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",
)