Skip to content

Commit 6366f65

Browse files
authored
chore: Use thread safe import in import_class_by_name utility function (#9028)
* Use thread safe import * fix debug string
1 parent 3d7d65a commit 6366f65

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

haystack/core/serialization.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import inspect
66
from collections.abc import Callable
77
from dataclasses import dataclass
8-
from importlib import import_module
98
from typing import Any, Dict, Iterable, Optional, Type
109

11-
from haystack.core.component.component import _hook_component_init, logger
10+
from haystack import logging
11+
from haystack.core.component.component import _hook_component_init
1212
from haystack.core.errors import DeserializationError, SerializationError
13+
from haystack.utils.type_serialization import thread_safe_import
14+
15+
logger = logging.getLogger(__name__)
1316

1417

1518
@dataclass(frozen=True)
@@ -251,9 +254,11 @@ def import_class_by_name(fully_qualified_name: str) -> Type[object]:
251254
"""
252255
try:
253256
module_path, class_name = fully_qualified_name.rsplit(".", 1)
254-
logger.debug(f"Attempting to import class '{class_name}' from module '{module_path}'")
255-
module = import_module(module_path)
257+
logger.debug(
258+
"Attempting to import class '{cls_name}' from module '{md_path}'", cls_name=class_name, md_path=module_path
259+
)
260+
module = thread_safe_import(module_path)
256261
return getattr(module, class_name)
257262
except (ImportError, AttributeError) as error:
258-
logger.error(f"Failed to import class '{fully_qualified_name}'")
263+
logger.error("Failed to import class '{full_name}'", full_name=fully_qualified_name)
259264
raise ImportError(f"Could not import class '{fully_qualified_name}'") from error

0 commit comments

Comments
 (0)