Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docarray/utils/create_dynamic_doc_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
from docarray.utils._internal._typing import safe_issubclass


RESERVED_KEYS = [
'type',
'anyOf',
'$ref',
'additionalProperties',
'allOf',
'items',
'definitions',
'properties',
'default',
]


def create_pure_python_type_model(model: Any) -> BaseDoc:
"""
Take a Pydantic model and cast DocList fields into List fields.
Expand Down Expand Up @@ -239,5 +252,11 @@ class MyDoc(BaseDoc):
)

model = create_model(base_doc_name, __base__=BaseDoc, **fields)
model.__config__.title = schema.get('title', model.__config__.title)

for k in RESERVED_KEYS:
if k in schema:
schema.pop(k)
model.__config__.schema_extra = schema
cached_models[base_doc_name] = model
return model
6 changes: 6 additions & 0 deletions tests/units/util/test_create_dynamic_code_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class ResultTestDoc(BaseDoc):

def test_create_with_field_info():
class CustomDoc(BaseDoc):
"""Here I have the description of the class"""

a: str = Field(examples=['Example here'], another_extra='I am another extra')

CustomDocCopy = create_pure_python_type_model(CustomDoc)
Expand All @@ -256,3 +258,7 @@ class CustomDoc(BaseDoc):
new_custom_doc_model.schema().get('properties')['a']['another_extra']
== 'I am another extra'
)
assert (
new_custom_doc_model.schema().get('description')
== 'Here I have the description of the class'
)