chore: remove optional imports in v2 (#4855)

* remove optional imports in v2

* unused import
This commit is contained in:
ZanSara 2023-05-10 14:02:18 +02:00 committed by GitHub
parent 9cb153d0f4
commit 6a7d31fb5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 36 deletions

View File

@ -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}')"

View File

@ -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