1
+ from typing import Dict , List
2
+
1
3
import numpy as np
2
4
import pytest
3
- from typing import Dict , List
5
+ from orjson import orjson
4
6
5
7
from docarray import DocList
6
8
from docarray .base_doc import AnyDoc , BaseDoc
9
+ from docarray .base_doc .io .json import orjson_dumps_and_decode
7
10
from docarray .typing import NdArray
11
+ from docarray .typing .tensor .abstract_tensor import AbstractTensor
8
12
9
13
10
14
def test_any_doc ():
@@ -36,7 +40,7 @@ class InnerDoc(BaseDoc):
36
40
class DocTest (BaseDoc ):
37
41
text : str
38
42
tags : Dict [str , int ]
39
- l : List [int ]
43
+ l_ : List [int ]
40
44
d : InnerDoc
41
45
ld : DocList [InnerDoc ]
42
46
@@ -46,14 +50,14 @@ class DocTest(BaseDoc):
46
50
DocTest (
47
51
text = 'type1' ,
48
52
tags = {'type' : 1 },
49
- l = [1 , 2 ],
53
+ l_ = [1 , 2 ],
50
54
d = inner_doc ,
51
55
ld = DocList [InnerDoc ]([inner_doc ]),
52
56
),
53
57
DocTest (
54
58
text = 'type2' ,
55
59
tags = {'type' : 2 },
56
- l = [1 , 2 ],
60
+ l_ = [1 , 2 ],
57
61
d = inner_doc ,
58
62
ld = DocList [InnerDoc ]([inner_doc ]),
59
63
),
@@ -71,7 +75,7 @@ class DocTest(BaseDoc):
71
75
for i , d in enumerate (aux ):
72
76
assert d .tags ['type' ] == i + 1
73
77
assert d .text == f'type{ i + 1 } '
74
- assert d .l == [1 , 2 ]
78
+ assert d .l_ == [1 , 2 ]
75
79
if protocol == 'proto' :
76
80
assert isinstance (d .d , AnyDoc )
77
81
assert d .d .text == 'I am inner' # inner Document is a Dict
@@ -89,3 +93,20 @@ class DocTest(BaseDoc):
89
93
assert isinstance (d .ld [0 ], dict )
90
94
assert d .ld [0 ]['text' ] == 'I am inner'
91
95
assert d .ld [0 ]['t' ] == {'a' : 'b' }
96
+
97
+
98
+ def test_subclass_config ():
99
+ class MyDoc (BaseDoc ):
100
+ x : str
101
+
102
+ class Config (BaseDoc .Config ):
103
+ arbitrary_types_allowed = True # just an example setting
104
+
105
+ assert MyDoc .Config .json_loads == orjson .loads
106
+ assert MyDoc .Config .json_dumps == orjson_dumps_and_decode
107
+ assert (
108
+ MyDoc .Config .json_encoders [AbstractTensor ](3 ) == 3
109
+ ) # dirty check that it is identity
110
+ assert MyDoc .Config .validate_assignment
111
+ assert not MyDoc .Config ._load_extra_fields_from_protobuf
112
+ assert MyDoc .Config .arbitrary_types_allowed
0 commit comments