Skip to content

Commit

Permalink
fix: fix subclass with DocList (#6138)
Browse files Browse the repository at this point in the history
Signed-off-by: Joan Martinez <[email protected]>
  • Loading branch information
JoanFM authored Feb 15, 2024
1 parent ec8c614 commit 073511e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
2 changes: 1 addition & 1 deletion extra-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ aiofiles: standard,devel
aiohttp: standard,devel
scipy>=1.6.1: test
Pillow: test
pytest: test
pytest<8.0.0: test
pytest-timeout: test
pytest-mock: test
pytest-cov==3.0.0: test
Expand Down
4 changes: 2 additions & 2 deletions jina/serve/runtimes/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def _create_aux_model_doc_list_to_list(model):
try:
if issubclass(field, DocList):
t: Any = field.doc_type
fields[field_name] = (List[t], field_info)
t_aux = _create_aux_model_doc_list_to_list(t)
fields[field_name] = (List[t_aux], field_info)
else:
fields[field_name] = (field, field_info)
except TypeError:
Expand Down Expand Up @@ -272,7 +273,6 @@ def _create_pydantic_model_from_schema(
) -> type:
if not definitions:
definitions = schema.get('definitions', {})

cached_models = cached_models if cached_models is not None else {}
fields: Dict[str, Any] = {}
if model_name in cached_models:
Expand Down
Empty file.
Empty file.
30 changes: 30 additions & 0 deletions tests/integration/docarray_v2/issues/github_6137/test_issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from docarray import DocList, BaseDoc
from docarray.documents.text import TextDoc
from jina import Executor, requests, Flow


def test_issue():
class QuoteFile(BaseDoc):
quote_file_id: int = None
texts: DocList[TextDoc] = None

class SearchResult(BaseDoc):
results: DocList[QuoteFile] = None

class InitialExecutor(Executor):

@requests(on='/search')
async def search(self, docs: DocList[SearchResult], **kwargs) -> DocList[SearchResult]:
return docs

f = (
Flow(protocol='http')
.add(name='initial', uses=InitialExecutor)
)

with f:
resp = f.post(on='/search', inputs=DocList[SearchResult]([SearchResult(results=DocList[QuoteFile](
[QuoteFile(quote_file_id=999, texts=DocList[TextDoc]([TextDoc(text='hey here')]))]))]),
return_type=DocList[SearchResult])
assert resp[0].results[0].quote_file_id == 999
assert resp[0].results[0].texts[0].text == 'hey here'
43 changes: 35 additions & 8 deletions tests/unit/serve/runtimes/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ def test_split_key_executor_name(full_key, key, executor):
'param, parsed_param, executor_name',
[
(
{'key': 1, 'executor__key': 2, 'wrong_executor__key': 3},
{'key': 2},
'executor',
{'key': 1, 'executor__key': 2, 'wrong_executor__key': 3},
{'key': 2},
'executor',
),
({'executor__key': 2, 'wrong_executor__key': 3}, {'key': 2}, 'executor'),
(
{'a': 1, 'executor__key': 2, 'wrong_executor__key': 3},
{'key': 2, 'a': 1},
'executor',
{'a': 1, 'executor__key': 2, 'wrong_executor__key': 3},
{'key': 2, 'a': 1},
'executor',
),
({'key_1': 0, 'exec2__key_2': 1}, {'key_1': 0}, 'executor'),
],
Expand All @@ -69,8 +69,8 @@ def test_get_name_from_replicas(name_w_replicas, name):


def _custom_grpc_options(
call_recording_mock: Mock,
additional_options: Optional[Union[list, Dict[str, Any]]] = None,
call_recording_mock: Mock,
additional_options: Optional[Union[list, Dict[str, Any]]] = None,
) -> List[Tuple[str, Any]]:
call_recording_mock()
expected_grpc_option_keys = [
Expand Down Expand Up @@ -355,3 +355,30 @@ class ResultTestDoc(BaseDoc):

assert len(original_back) == 0
assert len(custom_da) == 0


@pytest.mark.skipif(not docarray_v2, reason='Test only working with docarray v2')
def test_dynamic_class_creation_multiple_doclist_nested():
from docarray import BaseDoc, DocList
from jina.serve.runtimes.helper import _create_aux_model_doc_list_to_list
from jina.serve.runtimes.helper import _create_pydantic_model_from_schema

class MyTextDoc(BaseDoc):
text: str

class QuoteFile(BaseDoc):
texts: DocList[MyTextDoc]

class SearchResult(BaseDoc):
results: DocList[QuoteFile] = None

textlist = DocList[MyTextDoc]([MyTextDoc(text='hey')])
models_created_by_name = {}
SearchResult_aux = _create_aux_model_doc_list_to_list(SearchResult)
_ = _create_pydantic_model_from_schema(SearchResult_aux.schema(), 'SearchResult',
models_created_by_name)
QuoteFile_reconstructed_in_gateway_from_Search_results = models_created_by_name['QuoteFile']

reconstructed_in_gateway_from_Search_results = QuoteFile_reconstructed_in_gateway_from_Search_results(
texts=textlist)
assert reconstructed_in_gateway_from_Search_results.texts[0].text == 'hey'

0 comments on commit 073511e

Please sign in to comment.