forked from iterative/datachain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_module_exports.py
96 lines (86 loc) · 2.68 KB
/
test_module_exports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# flake8: noqa: F401
import builtins
import sys
import warnings
import pytest
from sqlalchemy.exc import SAWarning
def test_module_exports():
try:
from datachain import (
AbstractUDF,
Aggregator,
C,
Column,
DataChain,
DataChainError,
DataModel,
File,
FileError,
Generator,
ImageFile,
IndexedFile,
Mapper,
Session,
TarVFile,
TextFile,
)
from datachain.torch import (
PytorchDataset,
clip_similarity_scores,
convert_image,
convert_images,
convert_text,
label_to_int,
)
except Exception as e: # noqa: BLE001
pytest.fail(f"Importing raised an exception: {e}")
@pytest.mark.parametrize("dep", ["torch", "torchvision", "transformers"])
def test_no_torch_deps(monkeypatch, dep):
real_import = builtins.__import__
def monkey_import_importerror(
name, globals=None, locals=None, fromlist=(), level=0
):
if name.startswith(dep):
raise ImportError(f"Mocked import error {name}")
return real_import(
name, globals=globals, locals=locals, fromlist=fromlist, level=level
)
for module in list(sys.modules):
if module.startswith((dep, "datachain")):
monkeypatch.delitem(sys.modules, module)
monkeypatch.setattr(builtins, "__import__", monkey_import_importerror)
try:
with warnings.catch_warnings():
# Ignore SQLAlchemy warnings about redeclaring functions due to multiple
# imports of the same code in this test, after deleting them from
# sys.modules, which would not happen in normal user code.
warnings.filterwarnings("ignore", category=SAWarning)
from datachain import (
AbstractUDF,
Aggregator,
C,
Column,
DataChain,
DataChainError,
DataModel,
File,
FileError,
Generator,
ImageFile,
IndexedFile,
Mapper,
Session,
TarVFile,
TextFile,
)
except Exception as e: # noqa: BLE001
pytest.fail(f"Importing raised an exception: {e}")
with pytest.raises(ImportError):
from datachain.torch import (
PytorchDataset,
clip_similarity_scores,
convert_image,
convert_images,
convert_text,
label_to_int,
)