chore(api): improve type hints for `DifyCoreRepositoryFactory`

pull/22581/head
QuantumGhost 10 months ago committed by -LAN-
parent 6c8aee28ad
commit ab28f9a292
No known key found for this signature in database
GPG Key ID: 6BA0D108DED011FF

@ -8,7 +8,7 @@ allowing users to configure different repository backends through string paths.
import importlib import importlib
import inspect import inspect
import logging import logging
from typing import Any, Union from typing import Protocol, Union
from sqlalchemy.engine import Engine from sqlalchemy.engine import Engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
@ -38,7 +38,7 @@ class DifyCoreRepositoryFactory:
""" """
@staticmethod @staticmethod
def _import_class(class_path: str) -> Any: def _import_class(class_path: str) -> type:
""" """
Import a class from a module path string. Import a class from a module path string.
@ -54,12 +54,14 @@ class DifyCoreRepositoryFactory:
try: try:
module_path, class_name = class_path.rsplit(".", 1) module_path, class_name = class_path.rsplit(".", 1)
module = importlib.import_module(module_path) 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: except (ValueError, ImportError, AttributeError) as e:
raise RepositoryImportError(f"Cannot import repository class '{class_path}': {e}") from e raise RepositoryImportError(f"Cannot import repository class '{class_path}': {e}") from e
@staticmethod @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. Validate that a class implements the expected repository interface.
@ -89,7 +91,7 @@ class DifyCoreRepositoryFactory:
) )
@staticmethod @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. Validate that a repository class constructor accepts required parameters.
@ -102,7 +104,14 @@ class DifyCoreRepositoryFactory:
""" """
try: 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()) param_names = list(signature.parameters.keys())
# Remove 'self' parameter # Remove 'self' parameter

Loading…
Cancel
Save