-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathtest_create_dynamic_code_class.py
More file actions
358 lines (309 loc) · 12.9 KB
/
test_create_dynamic_code_class.py
File metadata and controls
358 lines (309 loc) · 12.9 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from typing import Any, Dict, List, Optional, Union, ClassVar
import numpy as np
import pytest
from pydantic import Field
from docarray import BaseDoc, DocList
from docarray.documents import TextDoc
from docarray.typing import AnyTensor, ImageUrl
from docarray.utils.create_dynamic_doc_class import (
create_base_doc_from_schema,
create_pure_python_type_model,
)
from docarray.utils._internal.pydantic import is_pydantic_v2
@pytest.mark.parametrize('transformation', ['proto', 'json'])
def test_create_pydantic_model_from_schema(transformation):
class Nested2Doc(BaseDoc):
value: str
classvar: ClassVar[str] = 'classvar2'
class Nested1Doc(BaseDoc):
nested: Nested2Doc
classvar: ClassVar[str] = 'classvar1'
class CustomDoc(BaseDoc):
tensor: Optional[AnyTensor] = None
url: ImageUrl
num: float = 0.5
num_num: List[float] = [1.5, 2.5]
lll: List[List[List[int]]] = [[[5]]]
fff: List[List[List[float]]] = [[[5.2]]]
single_text: TextDoc
texts: DocList[TextDoc]
d: Dict[str, str] = {'a': 'b'}
di: Optional[Dict[str, int]] = None
u: Union[str, int]
lu: List[Union[str, int]] = [0, 1, 2]
tags: Optional[Dict[str, Any]] = None
nested: Nested1Doc
classvar: ClassVar[str] = 'classvar'
CustomDocCopy = create_pure_python_type_model(CustomDoc)
new_custom_doc_model = create_base_doc_from_schema(
CustomDocCopy.schema(), 'CustomDoc', {}
)
print(f'new_custom_doc_model {new_custom_doc_model.schema()}')
original_custom_docs = DocList[CustomDoc](
[
CustomDoc(
num=3.5,
num_num=[4.5, 5.5],
url='photo.jpg',
lll=[[[40]]],
fff=[[[40.2]]],
d={'b': 'a'},
texts=DocList[TextDoc]([TextDoc(text='hey ha', embedding=np.zeros(3))]),
single_text=TextDoc(text='single hey ha', embedding=np.zeros(2)),
u='a',
lu=[3, 4],
nested=Nested1Doc(nested=Nested2Doc(value='hello world')),
)
]
)
for doc in original_custom_docs:
doc.tensor = np.zeros((10, 10, 10))
doc.di = {'a': 2}
if transformation == 'proto':
custom_partial_da = DocList[new_custom_doc_model].from_protobuf(
original_custom_docs.to_protobuf()
)
original_back = DocList[CustomDoc].from_protobuf(
custom_partial_da.to_protobuf()
)
elif transformation == 'json':
custom_partial_da = DocList[new_custom_doc_model].from_json(
original_custom_docs.to_json()
)
original_back = DocList[CustomDoc].from_json(custom_partial_da.to_json())
assert len(custom_partial_da) == 1
assert custom_partial_da[0].url == 'photo.jpg'
assert custom_partial_da[0].num == 3.5
assert custom_partial_da[0].num_num == [4.5, 5.5]
assert custom_partial_da[0].lll == [[[40]]]
if is_pydantic_v2:
assert custom_partial_da[0].lu == [3, 4]
else:
assert custom_partial_da[0].lu == ['3', '4'] # Union validates back to string
assert custom_partial_da[0].fff == [[[40.2]]]
assert custom_partial_da[0].di == {'a': 2}
assert custom_partial_da[0].d == {'b': 'a'}
assert len(custom_partial_da[0].texts) == 1
assert custom_partial_da[0].texts[0].text == 'hey ha'
assert custom_partial_da[0].texts[0].embedding.shape == (3,)
assert custom_partial_da[0].tensor.shape == (10, 10, 10)
assert custom_partial_da[0].u == 'a'
assert custom_partial_da[0].single_text.text == 'single hey ha'
assert custom_partial_da[0].single_text.embedding.shape == (2,)
assert original_back[0].nested.nested.value == 'hello world'
assert original_back[0].num == 3.5
assert original_back[0].num_num == [4.5, 5.5]
assert original_back[0].classvar == 'classvar'
assert original_back[0].nested.classvar == 'classvar1'
assert original_back[0].nested.nested.classvar == 'classvar2'
assert len(original_back) == 1
assert original_back[0].url == 'photo.jpg'
assert original_back[0].lll == [[[40]]]
if is_pydantic_v2:
assert original_back[0].lu == [3, 4] # Union validates back to string
else:
assert original_back[0].lu == ['3', '4'] # Union validates back to string
assert original_back[0].fff == [[[40.2]]]
assert original_back[0].di == {'a': 2}
assert original_back[0].d == {'b': 'a'}
assert len(original_back[0].texts) == 1
assert original_back[0].texts[0].text == 'hey ha'
assert original_back[0].texts[0].embedding.shape == (3,)
assert original_back[0].tensor.shape == (10, 10, 10)
assert original_back[0].u == 'a'
assert original_back[0].single_text.text == 'single hey ha'
assert original_back[0].single_text.embedding.shape == (2,)
class TextDocWithId(BaseDoc):
ia: str
TextDocWithIdCopy = create_pure_python_type_model(TextDocWithId)
new_textdoc_with_id_model = create_base_doc_from_schema(
TextDocWithIdCopy.schema(), 'TextDocWithId', {}
)
print(f'new_textdoc_with_id_model {new_textdoc_with_id_model.schema()}')
original_text_doc_with_id = DocList[TextDocWithId](
[TextDocWithId(ia=f'ID {i}') for i in range(10)]
)
if transformation == 'proto':
custom_da = DocList[new_textdoc_with_id_model].from_protobuf(
original_text_doc_with_id.to_protobuf()
)
original_back = DocList[TextDocWithId].from_protobuf(custom_da.to_protobuf())
elif transformation == 'json':
custom_da = DocList[new_textdoc_with_id_model].from_json(
original_text_doc_with_id.to_json()
)
original_back = DocList[TextDocWithId].from_json(custom_da.to_json())
assert len(custom_da) == 10
for i, doc in enumerate(custom_da):
assert doc.ia == f'ID {i}'
assert len(original_back) == 10
for i, doc in enumerate(original_back):
assert doc.ia == f'ID {i}'
class ResultTestDoc(BaseDoc):
matches: DocList[TextDocWithId]
ResultTestDocCopy = create_pure_python_type_model(ResultTestDoc)
new_result_test_doc_with_id_model = create_base_doc_from_schema(
ResultTestDocCopy.schema(), 'ResultTestDoc', {}
)
result_test_docs = DocList[ResultTestDoc](
[ResultTestDoc(matches=original_text_doc_with_id)]
)
if transformation == 'proto':
custom_da = DocList[new_result_test_doc_with_id_model].from_protobuf(
result_test_docs.to_protobuf()
)
original_back = DocList[ResultTestDoc].from_protobuf(custom_da.to_protobuf())
elif transformation == 'json':
custom_da = DocList[new_result_test_doc_with_id_model].from_json(
result_test_docs.to_json()
)
original_back = DocList[ResultTestDoc].from_json(custom_da.to_json())
assert len(custom_da) == 1
assert len(custom_da[0].matches) == 10
for i, doc in enumerate(custom_da[0].matches):
assert doc.ia == f'ID {i}'
assert len(original_back) == 1
assert len(original_back[0].matches) == 10
for i, doc in enumerate(original_back[0].matches):
assert doc.ia == f'ID {i}'
@pytest.mark.parametrize('transformation', ['proto', 'json'])
def test_create_empty_doc_list_from_schema(transformation):
class CustomDoc(BaseDoc):
tensor: Optional[AnyTensor]
url: ImageUrl
lll: List[List[List[int]]] = [[[5]]]
fff: List[List[List[float]]] = [[[5.2]]]
single_text: TextDoc
texts: DocList[TextDoc]
d: Dict[str, str] = {'a': 'b'}
di: Optional[Dict[str, int]] = None
u: Union[str, int]
lu: List[Union[str, int]] = [0, 1, 2]
tags: Optional[Dict[str, Any]] = None
lf: List[float] = [3.0, 4.1]
CustomDocCopy = create_pure_python_type_model(CustomDoc)
new_custom_doc_model = create_base_doc_from_schema(
CustomDocCopy.schema(), 'CustomDoc'
)
print(f'new_custom_doc_model {new_custom_doc_model.schema()}')
original_custom_docs = DocList[CustomDoc]()
if transformation == 'proto':
custom_partial_da = DocList[new_custom_doc_model].from_protobuf(
original_custom_docs.to_protobuf()
)
original_back = DocList[CustomDoc].from_protobuf(
custom_partial_da.to_protobuf()
)
elif transformation == 'json':
custom_partial_da = DocList[new_custom_doc_model].from_json(
original_custom_docs.to_json()
)
original_back = DocList[CustomDoc].from_json(custom_partial_da.to_json())
assert len(custom_partial_da) == 0
assert len(original_back) == 0
class TextDocWithId(BaseDoc):
ia: str
TextDocWithIdCopy = create_pure_python_type_model(TextDocWithId)
new_textdoc_with_id_model = create_base_doc_from_schema(
TextDocWithIdCopy.schema(), 'TextDocWithId', {}
)
print(f'new_textdoc_with_id_model {new_textdoc_with_id_model.schema()}')
original_text_doc_with_id = DocList[TextDocWithId]()
if transformation == 'proto':
custom_da = DocList[new_textdoc_with_id_model].from_protobuf(
original_text_doc_with_id.to_protobuf()
)
original_back = DocList[TextDocWithId].from_protobuf(custom_da.to_protobuf())
elif transformation == 'json':
custom_da = DocList[new_textdoc_with_id_model].from_json(
original_text_doc_with_id.to_json()
)
original_back = DocList[TextDocWithId].from_json(custom_da.to_json())
assert len(original_back) == 0
assert len(custom_da) == 0
class ResultTestDoc(BaseDoc):
matches: DocList[TextDocWithId]
ResultTestDocCopy = create_pure_python_type_model(ResultTestDoc)
new_result_test_doc_with_id_model = create_base_doc_from_schema(
ResultTestDocCopy.schema(), 'ResultTestDoc', {}
)
print(
f'new_result_test_doc_with_id_model {new_result_test_doc_with_id_model.schema()}'
)
result_test_docs = DocList[ResultTestDoc]()
if transformation == 'proto':
custom_da = DocList[new_result_test_doc_with_id_model].from_protobuf(
result_test_docs.to_protobuf()
)
original_back = DocList[ResultTestDoc].from_protobuf(custom_da.to_protobuf())
elif transformation == 'json':
custom_da = DocList[new_result_test_doc_with_id_model].from_json(
result_test_docs.to_json()
)
original_back = DocList[ResultTestDoc].from_json(custom_da.to_json())
assert len(original_back) == 0
assert len(custom_da) == 0
def test_create_with_field_info():
class CustomDoc(BaseDoc):
"""Here I have the description of the class"""
a: str = Field(examples=['Example here'], another_extra='I am another extra')
CustomDocCopy = create_pure_python_type_model(CustomDoc)
new_custom_doc_model = create_base_doc_from_schema(
CustomDocCopy.schema(), 'CustomDoc'
)
assert new_custom_doc_model.schema().get('properties')['a']['examples'] == [
'Example here'
]
assert (
new_custom_doc_model.schema().get('properties')['a']['another_extra']
== 'I am another extra'
)
assert (
new_custom_doc_model.schema().get('description')
== 'Here I have the description of the class'
)
def test_dynamic_class_creation_multiple_doclist_nested():
from docarray import BaseDoc, DocList
class MyTextDoc(BaseDoc):
text: str
class QuoteFile(BaseDoc):
texts: DocList[MyTextDoc]
class SearchResult(BaseDoc):
results: DocList[QuoteFile] = None
models_created_by_name = {}
SearchResult_aux = create_pure_python_type_model(SearchResult)
m = create_base_doc_from_schema(
SearchResult_aux.schema(), 'SearchResult', models_created_by_name
)
print(f'm {m.schema()}')
QuoteFile_reconstructed_in_gateway_from_Search_results = models_created_by_name[
'QuoteFile'
]
textlist = DocList[models_created_by_name['MyTextDoc']](
[models_created_by_name['MyTextDoc'](id='11', text='hey')]
)
reconstructed_in_gateway_from_Search_results = (
QuoteFile_reconstructed_in_gateway_from_Search_results(id='0', texts=textlist)
)
assert reconstructed_in_gateway_from_Search_results.texts[0].text == 'hey'
def test_id_optional():
from docarray import BaseDoc
import json
class MyTextDoc(BaseDoc):
text: str
opt: Optional[str] = None
MyTextDoc_aux = create_pure_python_type_model(MyTextDoc)
td = create_base_doc_from_schema(MyTextDoc_aux.schema(), 'MyTextDoc')
print(f'{td.schema()}')
direct = MyTextDoc.from_json(json.dumps({"text": "text"}))
aux = MyTextDoc_aux.from_json(json.dumps({"text": "text"}))
indirect = td.from_json(json.dumps({"text": "text"}))
assert direct.text == 'text'
assert aux.text == 'text'
assert indirect.text == 'text'
direct = MyTextDoc(text='hey')
aux = MyTextDoc_aux(text='hey')
indirect = td(text='hey')
assert direct.text == 'hey'
assert aux.text == 'hey'
assert indirect.text == 'hey'