Skip to content

Commit 189ff63

Browse files
docs: explain how to set document config (docarray#1773)
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
1 parent 3dc525f commit 189ff63

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

docs/user_guide/representing/first_step.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ This representation can be used to [send](../sending/first_step.md) or [store](.
117117

118118
[BaseDoc][docarray.base_doc.doc.BaseDoc] can be nested to represent any kind of data hierarchy.
119119

120+
## Setting a Pydantic `Config` class
121+
122+
Documents support setting a `Config` [like any other Pydantic `BaseModel`](https://docs.pydantic.dev/latest/usage/model_config/).
123+
124+
However, if you set a config, you should inherit from the `BaseDoc` config class:
125+
126+
```python
127+
from docarray import BaseDoc
128+
129+
130+
class MyDoc(BaseDoc):
131+
class Config(BaseDoc.Config):
132+
arbitrary_types_allowed = True # just an example setting
133+
```
134+
120135
See also:
121136

122137
* The [next part](./array.md) of the representing section

tests/units/document/test_any_document.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
from typing import Dict, List
2+
13
import numpy as np
24
import pytest
3-
from typing import Dict, List
5+
from orjson import orjson
46

57
from docarray import DocList
68
from docarray.base_doc import AnyDoc, BaseDoc
9+
from docarray.base_doc.io.json import orjson_dumps_and_decode
710
from docarray.typing import NdArray
11+
from docarray.typing.tensor.abstract_tensor import AbstractTensor
812

913

1014
def test_any_doc():
@@ -36,7 +40,7 @@ class InnerDoc(BaseDoc):
3640
class DocTest(BaseDoc):
3741
text: str
3842
tags: Dict[str, int]
39-
l: List[int]
43+
l_: List[int]
4044
d: InnerDoc
4145
ld: DocList[InnerDoc]
4246

@@ -46,14 +50,14 @@ class DocTest(BaseDoc):
4650
DocTest(
4751
text='type1',
4852
tags={'type': 1},
49-
l=[1, 2],
53+
l_=[1, 2],
5054
d=inner_doc,
5155
ld=DocList[InnerDoc]([inner_doc]),
5256
),
5357
DocTest(
5458
text='type2',
5559
tags={'type': 2},
56-
l=[1, 2],
60+
l_=[1, 2],
5761
d=inner_doc,
5862
ld=DocList[InnerDoc]([inner_doc]),
5963
),
@@ -71,7 +75,7 @@ class DocTest(BaseDoc):
7175
for i, d in enumerate(aux):
7276
assert d.tags['type'] == i + 1
7377
assert d.text == f'type{i + 1}'
74-
assert d.l == [1, 2]
78+
assert d.l_ == [1, 2]
7579
if protocol == 'proto':
7680
assert isinstance(d.d, AnyDoc)
7781
assert d.d.text == 'I am inner' # inner Document is a Dict
@@ -89,3 +93,20 @@ class DocTest(BaseDoc):
8993
assert isinstance(d.ld[0], dict)
9094
assert d.ld[0]['text'] == 'I am inner'
9195
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

Comments
 (0)