diff --git a/haystack/preview/dataclasses/document.py b/haystack/preview/dataclasses/document.py index f269ffd56..2d882b2ce 100644 --- a/haystack/preview/dataclasses/document.py +++ b/haystack/preview/dataclasses/document.py @@ -1,4 +1,4 @@ -from typing import List, Any, Dict, Literal, Optional, TYPE_CHECKING +from typing import List, Any, Dict, Literal, Optional import json import hashlib @@ -6,20 +6,20 @@ import logging from pathlib import Path from dataclasses import asdict, dataclass, field -from haystack.preview.utils.import_utils import optional_import - -# We need to do this dance because ndarray is an optional dependency used as a type by dataclass -if TYPE_CHECKING: - from numpy import ndarray -else: - ndarray = optional_import("numpy", "ndarray", "You won't be able to use embeddings.", __name__) - -DataFrame = optional_import("pandas", "DataFrame", "You won't be able to use table related features.", __name__) +import numpy +import pandas logger = logging.getLogger(__name__) + ContentType = Literal["text", "table", "image", "audio"] -PYTHON_TYPES_FOR_CONTENT: Dict[ContentType, type] = {"text": str, "table": DataFrame, "image": Path, "audio": Path} + +PYTHON_TYPES_FOR_CONTENT: Dict[ContentType, type] = { + "text": str, + "table": pandas.DataFrame, + "image": Path, + "audio": Path, +} def _create_id( @@ -61,7 +61,7 @@ class Document: metadata: Dict[str, Any] = field(default_factory=dict, hash=False) id_hash_keys: List[str] = field(default_factory=lambda: [], hash=False) score: Optional[float] = field(default=None, compare=True) - embedding: Optional[ndarray] = field(default=None, repr=False) + embedding: Optional[numpy.ndarray] = field(default=None, repr=False) def __str__(self): return f"{self.__class__.__name__}('{self.content}')" diff --git a/haystack/preview/utils/import_utils.py b/haystack/preview/utils/import_utils.py deleted file mode 100644 index 282ce816e..000000000 --- a/haystack/preview/utils/import_utils.py +++ /dev/null @@ -1,24 +0,0 @@ -from typing import Optional, Any -import importlib -import logging - - -def optional_import(import_path: str, import_target: Optional[str], error_msg: str, importer_module: str) -> Any: - """ - Imports an optional dependency. Emits a DEBUG log if the dependency is missing. - """ - try: - module = importlib.import_module(import_path) - if import_target: - return getattr(module, import_target) - return module - except ImportError as exc: - logging.getLogger(importer_module).debug( - "%s%s%s can't be imported: %s Error raised: %s", - import_path, - "." if import_target else "", - import_target, - error_msg, - exc, - ) - return None