Skip to content

Commit

Permalink
fix: skip doc attributes in __annotations__ but not in __fields__ (#6035
Browse files Browse the repository at this point in the history
)

Co-authored-by: Joan Fontanals <[email protected]>
  • Loading branch information
NarekA and JoanFM authored Sep 6, 2023
1 parent f77e876 commit 7529214
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions jina/serve/runtimes/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def _parse_specific_params(parameters: Dict, executor_name: str):
def _create_aux_model_doc_list_to_list(model):
fields: Dict[str, Any] = {}
for field_name, field in model.__annotations__.items():
if field_name not in model.__fields__:
continue
field_info = model.__fields__[field_name].field_info
try:
if issubclass(field, DocList):
Expand Down
6 changes: 5 additions & 1 deletion tests/integration/docarray_v2/test_streaming.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import time
from typing import AsyncGenerator, Generator, Optional
from typing import AsyncGenerator, Generator, Optional, ClassVar

import pytest
from docarray import BaseDoc, DocList
Expand All @@ -10,6 +10,9 @@


class MyDocument(BaseDoc):
# Not exposed to client
input_type_name: ClassVar[str] = 'MyDocumentType'

text: str
number: Optional[int]

Expand Down Expand Up @@ -68,6 +71,7 @@ async def test_streaming_deployment(protocol, include_gateway):
):
assert doc.text == f'hello world {i}'
i += 1
assert doc.input_type_name == 'MyDocumentType'


@pytest.mark.asyncio
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/serve/runtimes/test_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union, ClassVar
from unittest.mock import Mock

import pytest
Expand Down Expand Up @@ -103,9 +103,11 @@ 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]
Expand All @@ -120,6 +122,7 @@ class CustomDoc(BaseDoc):
lu: List[Union[str, int]] = [0, 1, 2]
tags: Optional[Dict[str, Any]] = None
nested: Nested1Doc
classvar: ClassVar[str] = 'classvar'

CustomDocCopy = _create_aux_model_doc_list_to_list(CustomDoc)
new_custom_doc_model = _create_pydantic_model_from_schema(
Expand Down Expand Up @@ -189,6 +192,9 @@ class CustomDoc(BaseDoc):
assert original_back[0].single_text.text == 'single hey ha'
assert original_back[0].single_text.embedding.shape == (2,)
assert original_back[0].nested.nested.value == 'hello world'
assert original_back[0].classvar == 'classvar'
assert original_back[0].nested.classvar == 'classvar1'
assert original_back[0].nested.nested.classvar == 'classvar2'

class TextDocWithId(BaseDoc):
ia: str
Expand Down Expand Up @@ -270,6 +276,7 @@ def test_create_empty_doc_list_from_schema(transformation):
class CustomDoc(BaseDoc):
tensor: Optional[AnyTensor]
url: ImageUrl
class_var: ClassVar[str] = "class_var_val"
lll: List[List[List[int]]] = [[[5]]]
fff: List[List[List[float]]] = [[[5.2]]]
single_text: TextDoc
Expand Down

0 comments on commit 7529214

Please sign in to comment.