Skip to content

Commit 7529214

Browse files
NarekAJoan Fontanals
andauthored
fix: skip doc attributes in __annotations__ but not in __fields__ (#6035)
Co-authored-by: Joan Fontanals <[email protected]>
1 parent f77e876 commit 7529214

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

jina/serve/runtimes/helper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def _parse_specific_params(parameters: Dict, executor_name: str):
102102
def _create_aux_model_doc_list_to_list(model):
103103
fields: Dict[str, Any] = {}
104104
for field_name, field in model.__annotations__.items():
105+
if field_name not in model.__fields__:
106+
continue
105107
field_info = model.__fields__[field_name].field_info
106108
try:
107109
if issubclass(field, DocList):

tests/integration/docarray_v2/test_streaming.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import time
3-
from typing import AsyncGenerator, Generator, Optional
3+
from typing import AsyncGenerator, Generator, Optional, ClassVar
44

55
import pytest
66
from docarray import BaseDoc, DocList
@@ -10,6 +10,9 @@
1010

1111

1212
class MyDocument(BaseDoc):
13+
# Not exposed to client
14+
input_type_name: ClassVar[str] = 'MyDocumentType'
15+
1316
text: str
1417
number: Optional[int]
1518

@@ -68,6 +71,7 @@ async def test_streaming_deployment(protocol, include_gateway):
6871
):
6972
assert doc.text == f'hello world {i}'
7073
i += 1
74+
assert doc.input_type_name == 'MyDocumentType'
7175

7276

7377
@pytest.mark.asyncio

tests/unit/serve/runtimes/test_helper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List, Optional, Tuple, Union
1+
from typing import Any, Dict, List, Optional, Tuple, Union, ClassVar
22
from unittest.mock import Mock
33

44
import pytest
@@ -103,9 +103,11 @@ def test_create_pydantic_model_from_schema(transformation):
103103

104104
class Nested2Doc(BaseDoc):
105105
value: str
106+
classvar: ClassVar[str] = 'classvar2'
106107

107108
class Nested1Doc(BaseDoc):
108109
nested: Nested2Doc
110+
classvar: ClassVar[str] = 'classvar1'
109111

110112
class CustomDoc(BaseDoc):
111113
tensor: Optional[AnyTensor]
@@ -120,6 +122,7 @@ class CustomDoc(BaseDoc):
120122
lu: List[Union[str, int]] = [0, 1, 2]
121123
tags: Optional[Dict[str, Any]] = None
122124
nested: Nested1Doc
125+
classvar: ClassVar[str] = 'classvar'
123126

124127
CustomDocCopy = _create_aux_model_doc_list_to_list(CustomDoc)
125128
new_custom_doc_model = _create_pydantic_model_from_schema(
@@ -189,6 +192,9 @@ class CustomDoc(BaseDoc):
189192
assert original_back[0].single_text.text == 'single hey ha'
190193
assert original_back[0].single_text.embedding.shape == (2,)
191194
assert original_back[0].nested.nested.value == 'hello world'
195+
assert original_back[0].classvar == 'classvar'
196+
assert original_back[0].nested.classvar == 'classvar1'
197+
assert original_back[0].nested.nested.classvar == 'classvar2'
192198

193199
class TextDocWithId(BaseDoc):
194200
ia: str
@@ -270,6 +276,7 @@ def test_create_empty_doc_list_from_schema(transformation):
270276
class CustomDoc(BaseDoc):
271277
tensor: Optional[AnyTensor]
272278
url: ImageUrl
279+
class_var: ClassVar[str] = "class_var_val"
273280
lll: List[List[List[int]]] = [[[5]]]
274281
fff: List[List[List[float]]] = [[[5.2]]]
275282
single_text: TextDoc

0 commit comments

Comments
 (0)