From ab28f9a292a1c7a6e9da0cd9dd847f41ea2914c4 Mon Sep 17 00:00:00 2001 From: QuantumGhost Date: Thu, 10 Jul 2025 17:07:53 +0800 Subject: [PATCH] chore(api): improve type hints for `DifyCoreRepositoryFactory` --- api/core/repositories/factory.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/api/core/repositories/factory.py b/api/core/repositories/factory.py index 21565cdf41..4118aa61c7 100644 --- a/api/core/repositories/factory.py +++ b/api/core/repositories/factory.py @@ -8,7 +8,7 @@ allowing users to configure different repository backends through string paths. import importlib import inspect import logging -from typing import Any, Union +from typing import Protocol, Union from sqlalchemy.engine import Engine from sqlalchemy.orm import sessionmaker @@ -38,7 +38,7 @@ class DifyCoreRepositoryFactory: """ @staticmethod - def _import_class(class_path: str) -> Any: + def _import_class(class_path: str) -> type: """ Import a class from a module path string. @@ -54,12 +54,14 @@ class DifyCoreRepositoryFactory: try: module_path, class_name = class_path.rsplit(".", 1) module = importlib.import_module(module_path) - return getattr(module, class_name) + repo_class = getattr(module, class_name) + assert isinstance(repo_class, type) + return repo_class except (ValueError, ImportError, AttributeError) as e: raise RepositoryImportError(f"Cannot import repository class '{class_path}': {e}") from e @staticmethod - def _validate_repository_interface(repository_class: Any, expected_interface: Any) -> None: + def _validate_repository_interface(repository_class: type, expected_interface: type[Protocol]) -> None: # type: ignore """ Validate that a class implements the expected repository interface. @@ -89,7 +91,7 @@ class DifyCoreRepositoryFactory: ) @staticmethod - def _validate_constructor_signature(repository_class: Any, required_params: list[str]) -> None: + def _validate_constructor_signature(repository_class: type, required_params: list[str]) -> None: """ Validate that a repository class constructor accepts required parameters. @@ -102,7 +104,14 @@ class DifyCoreRepositoryFactory: """ try: - signature = inspect.signature(repository_class.__init__) + # MyPy may flag the line below with the following error: + # + # > Accessing "__init__" on an instance is unsound, since + # > instance.__init__ could be from an incompatible subclass. + # + # Despite this, we need to ensure that the constructor of `repository_class` + # has a compatible signature. + signature = inspect.signature(repository_class.__init__) # type: ignore[misc] param_names = list(signature.parameters.keys()) # Remove 'self' parameter