Fix mypy context manager typing for adapter subclasses.
Code Quality Pipeline / code-quality (pull_request) Failing after 26s
Test Python Package / test (pull_request) Successful in 1m7s

Return Self from __enter__ so with-blocks preserve subclass types, and align mypy python_version with the 3.12 runtime target.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Brian Bjarke Jensen
2026-06-28 19:42:15 +02:00
co-authored by Cursor
parent e98fd3b90d
commit 497533f0e3
4 changed files with 7 additions and 5 deletions
+1 -1
View File
@@ -48,7 +48,7 @@ url = "https://gitea.lille-vemmelund.dk/api/packages/brian/pypi/simple/"
explicit = true
[tool.mypy]
python_version = "3.10"
python_version = "3.12"
warn_return_any = true # nudge to use stricter types
warn_unused_configs = true # nudge to remove unused configs
disallow_untyped_defs = true # disallow untyped function definitions
@@ -4,6 +4,7 @@ from __future__ import annotations
import os
from io import BytesIO
from importlib.util import find_spec
from typing import Self
import structlog
from python_utils import check_env
@@ -49,7 +50,7 @@ class MinioAdapter(
self._client: minio.Minio | None = None
self._bucket_name: str | None = None
def __enter__(self) -> MinioAdapter:
def __enter__(self) -> Self:
"""Enter the context."""
self.connect()
return self
@@ -1,7 +1,7 @@
"""Definition of RedisAdapter class."""
from __future__ import annotations
from typing import cast
from typing import Self, cast
from importlib.util import find_spec
import os
import structlog
@@ -42,7 +42,7 @@ class RedisAdapter(
self._client: redis.Redis | None = None
self.path: str = RedisPath.root_path()
def __enter__(self) -> RedisAdapter:
def __enter__(self) -> Self:
"""Enter the context."""
self.connect()
return self
@@ -3,13 +3,14 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Self
class ContextAwareInterface(ABC):
"""Interface that defines context-related methods."""
@abstractmethod
def __enter__(self) -> ContextAwareInterface:
def __enter__(self) -> Self:
"""Enter the context."""
...