Skip to content

Commit 7c1e18e

Browse files
author
Joan Fontanals
authored
fix: fix create pure python class iteratively (#1867)
Signed-off-by: Joan Martinez <[email protected]>
1 parent aa15b9e commit 7c1e18e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

docarray/utils/create_dynamic_doc_class.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class MyDoc(BaseDoc):
6767
try:
6868
if safe_issubclass(field, DocList):
6969
t: Any = field.doc_type
70-
fields[field_name] = (List[t], field_info)
70+
t_aux = create_pure_python_type_model(t)
71+
fields[field_name] = (List[t_aux], field_info)
7172
else:
7273
fields[field_name] = (field, field_info)
7374
except TypeError:

tests/units/util/test_create_dynamic_code_class.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,33 @@ class CustomDoc(BaseDoc):
285285
new_custom_doc_model.schema().get('description')
286286
== 'Here I have the description of the class'
287287
)
288+
289+
290+
def test_dynamic_class_creation_multiple_doclist_nested():
291+
from docarray import BaseDoc, DocList
292+
293+
class MyTextDoc(BaseDoc):
294+
text: str
295+
296+
class QuoteFile(BaseDoc):
297+
texts: DocList[MyTextDoc]
298+
299+
class SearchResult(BaseDoc):
300+
results: DocList[QuoteFile] = None
301+
302+
models_created_by_name = {}
303+
SearchResult_aux = create_pure_python_type_model(SearchResult)
304+
_ = create_base_doc_from_schema(
305+
SearchResult_aux.schema(), 'SearchResult', models_created_by_name
306+
)
307+
QuoteFile_reconstructed_in_gateway_from_Search_results = models_created_by_name[
308+
'QuoteFile'
309+
]
310+
textlist = DocList[models_created_by_name['MyTextDoc']](
311+
[models_created_by_name['MyTextDoc'](id='11', text='hey')]
312+
)
313+
314+
reconstructed_in_gateway_from_Search_results = (
315+
QuoteFile_reconstructed_in_gateway_from_Search_results(id='0', texts=textlist)
316+
)
317+
assert reconstructed_in_gateway_from_Search_results.texts[0].text == 'hey'

0 commit comments

Comments
 (0)