Skip to content

Commit 99af14c

Browse files
authored
Fix import (#2821)
Make intra import not global
1 parent e75c56c commit 99af14c

File tree

10 files changed

+31
-68
lines changed

10 files changed

+31
-68
lines changed

deeplake/core/index/index.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,15 @@ def __getitem__(self, item: IndexValue):
198198

199199
def subscriptable(self):
200200
"""Returns whether an IndexEntry can be further subscripted."""
201-
from indra import api # type: ignore
202201

203-
if isinstance(self.value, api.core.IndexMappingInt64):
204-
return self.value.subscriptable()
202+
from deeplake.enterprise.util import INDRA_INSTALLED
203+
204+
if INDRA_INSTALLED:
205+
from indra import api # type: ignore
206+
207+
if isinstance(self.value, api.core.IndexMappingInt64):
208+
return self.value.subscriptable()
209+
205210
return not isinstance(self.value, int)
206211

207212
def indices(self, length: int):

deeplake/core/storage/indra.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from deeplake.core.storage.provider import StorageProvider
22
from deeplake.core.partial_reader import PartialReader
33
from deeplake.core.storage.deeplake_memory_object import DeepLakeMemoryObject
4-
from indra.api import storage # type: ignore
54
from typing import Optional, Union, Dict
65

76

@@ -10,10 +9,12 @@ class IndraProvider(StorageProvider):
109

1110
def __init__(
1211
self,
13-
root: Union[str, storage.provider],
12+
root, # Union[str, storage.provider],
1413
read_only: Optional[bool] = False,
1514
**kwargs,
1615
):
16+
from indra.api import storage # type: ignore
17+
1718
if isinstance(root, str):
1819
self.core = storage.create(root, read_only, **kwargs)
1920
else:

deeplake/core/vectorstore/dataset_handlers/dataset_handler_base.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ def __init__(
4040
logger: logging.Logger,
4141
**kwargs: Any,
4242
):
43-
try:
44-
from indra import api # type: ignore
45-
46-
self.indra_installed = True
47-
except Exception: # pragma: no cover
48-
self.indra_installed = False # pragma: no cover
49-
5043
self._exec_option = exec_option
5144

5245
self.path: Optional[str] = None
@@ -104,9 +97,7 @@ def token(self):
10497

10598
@property
10699
def exec_option(self) -> str:
107-
return utils.parse_exec_option(
108-
self.dataset, self._exec_option, self.indra_installed, self.username
109-
)
100+
return utils.parse_exec_option(self.dataset, self._exec_option, self.username)
110101

111102
@property
112103
def username(self) -> str:

deeplake/core/vectorstore/deep_memory/deep_memory.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import numpy as np
1111

1212
import deeplake
13+
from deeplake.enterprise.util import INDRA_INSTALLED
1314
from deeplake.util.exceptions import (
1415
DeepMemoryAccessError,
1516
IncorrectRelevanceTypeError,
@@ -460,13 +461,6 @@ def evaluate(
460461
token=self.token,
461462
)
462463

463-
try:
464-
from indra import api # type: ignore
465-
466-
INDRA_INSTALLED = True
467-
except Exception:
468-
INDRA_INSTALLED = False
469-
470464
if not INDRA_INSTALLED:
471465
raise ImportError(
472466
"indra is not installed. Please install indra to use this functionality with: pip install `deeplake[enterprise]`"

deeplake/core/vectorstore/vector_search/dataset/dataset.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,7 @@ def create_or_load_dataset(
3838
branch="main",
3939
**kwargs,
4040
):
41-
try:
42-
from indra import api # type: ignore
43-
44-
_INDRA_INSTALLED = True # pragma: no cover
45-
except ImportError: # pragma: no cover
46-
_INDRA_INSTALLED = False # pragma: no cover
47-
48-
utils.check_indra_installation(
49-
exec_option=exec_option, indra_installed=_INDRA_INSTALLED
50-
)
41+
utils.check_indra_installation(exec_option=exec_option)
5142

5243
if not overwrite and dataset_exists(dataset_path, token, creds, **kwargs):
5344
if tensor_params is not None and tensor_params != DEFAULT_VECTORSTORE_TENSORS:

deeplake/core/vectorstore/vector_search/indra/search_algorithm.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from abc import ABC, abstractmethod
33
from typing import Union, Dict, List, Optional
44

5+
from deeplake.enterprise.util import INDRA_INSTALLED
56
from deeplake.core.vectorstore.vector_search.indra import query
67
from deeplake.core.vectorstore.vector_search import utils
78
from deeplake.core.dataset import Dataset as DeepLakeDataset
@@ -110,14 +111,6 @@ def _get_view(self, tql_query, runtime: Optional[Dict] = None):
110111
return view
111112

112113
def _get_indra_dataset(self):
113-
try:
114-
from indra import api # type: ignore
115-
116-
INDRA_INSTALLED = True
117-
except ImportError:
118-
INDRA_INSTALLED = False
119-
pass
120-
121114
if not INDRA_INSTALLED:
122115
from deeplake.enterprise.util import raise_indra_installation_error
123116

deeplake/core/vectorstore/vector_search/indra/vector_search.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,14 @@ def vector_search(
2222
org_id,
2323
return_tql,
2424
) -> Union[Dict, DeepLakeDataset]:
25-
try:
26-
from indra import api # type: ignore
27-
28-
_INDRA_INSTALLED = True # pragma: no cover
29-
except ImportError: # pragma: no cover
30-
_INDRA_INSTALLED = False # pragma: no cover
31-
3225
runtime = utils.get_runtime_from_exec_option(exec_option)
3326

3427
if callable(filter):
3528
raise ValueError(
3629
f"UDF filter functions are not supported with the current `exec_option`={exec_option}. "
3730
)
3831

39-
utils.check_indra_installation(exec_option, indra_installed=_INDRA_INSTALLED)
32+
utils.check_indra_installation(exec_option)
4033

4134
view, tql_filter = filter_utils.attribute_based_filtering_tql(
4235
view=dataset,

deeplake/core/vectorstore/vector_search/utils.py

+12-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import deeplake
99
from deeplake.constants import MB, DEFAULT_VECTORSTORE_INDEX_PARAMS, TARGET_BYTE_SIZE
10+
from deeplake.enterprise.util import INDRA_INSTALLED
1011
from deeplake.util.exceptions import TensorDoesNotExistError
1112
from deeplake.util.warnings import always_warn
1213
from deeplake.core.dataset import DeepLakeCloudDataset, Dataset
@@ -41,9 +42,8 @@ def get_exec_option(self):
4142

4243

4344
class ExecOptionCloudDataset(ExecOptionBase):
44-
def __init__(self, dataset, indra_installed, username, path_type):
45+
def __init__(self, dataset, username, path_type):
4546
self.dataset = dataset
46-
self.indra_installed = indra_installed
4747
self.client = dataset.client
4848
self.token = self.dataset.token
4949
self.username = username
@@ -59,20 +59,15 @@ def get_exec_option(self):
5959
return "tensor_db"
6060
# option 2: dataset is created in a linked storage or locally,
6161
# indra is installed user/org has access to indra
62-
elif (
63-
self.path_type == "hub"
64-
and self.indra_installed
65-
and self.username != "public"
66-
):
62+
elif self.path_type == "hub" and INDRA_INSTALLED and self.username != "public":
6763
return "compute_engine"
6864
else:
6965
return "python"
7066

7167

7268
class ExecOptionLocalDataset(ExecOptionBase):
73-
def __init__(self, dataset, indra_installed, username):
69+
def __init__(self, dataset, username):
7470
self.dataset = dataset
75-
self.indra_installed = indra_installed
7671
self.token = self.dataset.token
7772
self.username = username
7873

@@ -83,21 +78,21 @@ def get_exec_option(self):
8378
if "mem://" in self.dataset.path:
8479
return "python"
8580

86-
if self.indra_installed and self.username != "public":
81+
if INDRA_INSTALLED and self.username != "public":
8782
return "compute_engine"
8883
return "python"
8984

9085

91-
def exec_option_factory(dataset, indra_installed, username):
86+
def exec_option_factory(dataset, username):
9287
path_type = get_path_type(dataset.path)
9388
if path_type == "local":
94-
return ExecOptionLocalDataset(dataset, indra_installed, username)
95-
return ExecOptionCloudDataset(dataset, indra_installed, username, path_type)
89+
return ExecOptionLocalDataset(dataset, username)
90+
return ExecOptionCloudDataset(dataset, username, path_type)
9691

9792

98-
def parse_exec_option(dataset, exec_option, indra_installed, username):
93+
def parse_exec_option(dataset, exec_option, username):
9994
if exec_option is None or exec_option == "auto":
100-
exec_option = exec_option_factory(dataset, indra_installed, username)
95+
exec_option = exec_option_factory(dataset, username)
10196
return exec_option.get_exec_option()
10297
return exec_option
10398

@@ -136,8 +131,8 @@ def parse_return_tensors(dataset, return_tensors, embedding_tensor, return_view)
136131
return return_tensors
137132

138133

139-
def check_indra_installation(exec_option, indra_installed):
140-
if exec_option == "compute_engine" and not indra_installed:
134+
def check_indra_installation(exec_option):
135+
if exec_option == "compute_engine" and not INDRA_INSTALLED:
141136
from deeplake.enterprise.util import raise_indra_installation_error
142137

143138
raise raise_indra_installation_error(

deeplake/enterprise/convert_to_libdeeplake.py

-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ def import_indra_api():
4343
return api
4444

4545

46-
INDRA_INSTALLED = bool(importlib.util.find_spec("indra"))
47-
48-
4946
def _get_indra_ds_from_native_provider(provider: IndraProvider):
5047
api = import_indra_api()
5148
return api.dataset(provider.core)

deeplake/enterprise/util.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
from typing import Optional
23
from deeplake.integrations.pytorch.common import collate_fn as pytorch_collate_fn
34
from deeplake.integrations.tf.common import collate_fn as tf_collate_fn
@@ -6,6 +7,8 @@
67

78
import os
89

10+
INDRA_INSTALLED = bool(importlib.util.find_spec("indra"))
11+
912

1013
def raise_indra_installation_error(indra_import_error: Optional[Exception] = None):
1114
if not indra_import_error:

0 commit comments

Comments
 (0)