Skip to content

Commit

Permalink
fix: fix issue with float (#6155)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanFM authored Mar 18, 2024
1 parent 64a0115 commit ade9084
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion jina/serve/runtimes/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ def _get_field_from_type(
for rec in range(num_recursions):
ret = List[ret]
elif field_type == 'number':
if num_recursions <= 1:
if num_recursions == 0:
ret = float
elif num_recursions == 1:
# This is a hack because AnyTensor is more generic than a simple List and it comes as simple List
if is_tensor:
ret = AnyTensor
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/serve/runtimes/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class Nested1Doc(BaseDoc):
class CustomDoc(BaseDoc):
tensor: Optional[AnyTensor]
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
Expand All @@ -132,6 +134,8 @@ class CustomDoc(BaseDoc):
original_custom_docs = DocList[CustomDoc](
[
CustomDoc(
num=3.5,
num_num=[4.5, 5.5],
url='photo.jpg',
lll=[[[40]]],
fff=[[[40.2]]],
Expand Down Expand Up @@ -163,6 +167,8 @@ class CustomDoc(BaseDoc):

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]]]
assert custom_partial_da[0].lu == ['3', '4'] # Union validates back to string
assert custom_partial_da[0].fff == [[[40.2]]]
Expand All @@ -178,6 +184,8 @@ class CustomDoc(BaseDoc):
assert custom_partial_da[0].nested.nested.value == 'hello world'

assert len(original_back) == 1
assert original_back[0].num == 3.5
assert original_back[0].num_num == [4.5, 5.5]
assert original_back[0].url == 'photo.jpg'
assert original_back[0].lll == [[[40]]]
assert original_back[0].lu == ['3', '4'] # Union validates back to string
Expand Down Expand Up @@ -276,6 +284,7 @@ def test_create_empty_doc_list_from_schema(transformation):
class CustomDoc(BaseDoc):
tensor: Optional[AnyTensor]
url: ImageUrl
num: float = 0.5,
class_var: ClassVar[str] = "class_var_val"
lll: List[List[List[int]]] = [[[5]]]
fff: List[List[List[float]]] = [[[5.2]]]
Expand Down

0 comments on commit ade9084

Please sign in to comment.