This repository was archived by the owner on Sep 9, 2026. It is now read-only.
Repository navigation
fix: wip add default value to TextDoc #1798
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import Any, Dict | ||
|
|
||
| from docarray.base_doc.doc import BaseDoc | ||
| from docarray.utils._internal.pydantic import is_pydantic_v2 | ||
|
|
||
| if is_pydantic_v2: | ||
| from pydantic_core import core_schema | ||
|
|
||
|
|
||
| class PredefinedDoc(BaseDoc, ABC): | ||
| """ | ||
| Custom class for handling predefined documents and can override their init and validation input. | ||
| """ | ||
|
|
||
| @classmethod | ||
| @abstractmethod | ||
| def _docarray_custom_val(cls, value: Any) -> Dict[str, Any]: | ||
| ... | ||
|
|
||
| if is_pydantic_v2: | ||
|
|
||
| @classmethod | ||
| def __get_pydantic_core_schema__( | ||
| cls, source, handler | ||
| ) -> core_schema.CoreSchema: | ||
| if '__pydantic_core_schema__' in cls.__dict__: | ||
| if not cls.__pydantic_generic_metadata__['origin']: | ||
| schema = cls.__pydantic_core_schema__ | ||
|
|
||
| schema = handler(source) | ||
| return core_schema.general_wrap_validator_function( | ||
| function=cls._docarray_default_value_validation, schema=schema | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _docarray_default_value_validation(cls, value, model_validator, _): | ||
| # print(value) | ||
| return model_validator(cls._docarray_custom_val(value)) | ||
|
|
||
| else: | ||
|
|
||
| @classmethod | ||
| def validate(cls, value: Any): | ||
| return super().validate(cls._docarray_custom_val(value)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from pydantic import parse_obj_as | ||
|
|
||
| from docarray import BaseDoc | ||
| from docarray.documents import TextDoc | ||
|
|
||
|
|
||
| def test_nested_defaut_value(): | ||
| class MyDocument(BaseDoc): | ||
| caption: TextDoc | ||
|
|
||
| doc = MyDocument(caption='A tiger in the jungle') | ||
|
|
||
| assert doc.caption.text == 'A tiger in the jungle' | ||
|
|
||
|
|
||
| def test_parse_default_val(): | ||
|
|
||
| doc = parse_obj_as(TextDoc, 'A tiger in the jungle') | ||
| assert doc.text == 'A tiger in the jungle' | ||
|
|
||
|
|
||
| def test_parse_default_val_init(): | ||
|
|
||
| doc = TextDoc('A tiger in the jungle') | ||
| assert doc.text == 'A tiger in the jungle' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't this make all the previous code useless?