Skip to content

Commit 50722a8

Browse files
authored
fix: set doc id first (#175)
1 parent 2196082 commit 50722a8

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

docarray/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def __init__(
4646
kwargs.pop(k)
4747

4848
self._data = self._data_class(self)
49+
50+
# ``id`` need to be set at the first place, since some other attributes would depends on it.
51+
if 'id' in kwargs:
52+
setattr(self._data, 'id', kwargs.pop('id'))
53+
4954
for k, v in kwargs.items():
5055
setattr(self._data, k, v)
5156

tests/unit/document/test_docdata.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ def test_doc_hash_identical():
2323
assert d1 == d2
2424

2525

26+
def test_doc_id_setter():
27+
d = Document(
28+
chunks=[Document(id='c0')],
29+
id='d0',
30+
)
31+
assert d.id == d.chunks[0].parent_id == 'd0'
32+
33+
2634
def test_doc_hash_complicate_content():
2735
d1 = Document(text='hello', embedding=np.array([1, 2, 3]), id=1)
2836
d2 = Document(text='hello', embedding=np.array([1, 2, 3]), id=1)

0 commit comments

Comments
 (0)