Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f9734ce

Browse filesBrowse files
author
Joan Fontanals Martinez
committed
fix: add description and title to dynamic class
Signed-off-by: Joan Fontanals Martinez <joan.martinez@jina.ai>
1 parent 896c20b commit f9734ce
Copy full SHA for f9734ce

2 files changed

+14Lines changed: 14 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docarray/utils/create_dynamic_doc_class.py‎

Copy file name to clipboardExpand all lines: docarray/utils/create_dynamic_doc_class.py
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from docarray.utils._internal._typing import safe_issubclass
77

88

9+
RESERVED_KEYS = ['type', 'anyOf', '$ref', 'additionalProperties', 'allOf', 'items', 'definitions', 'properties', 'default']
10+
911
def create_pure_python_type_model(model: Any) -> BaseDoc:
1012
"""
1113
Take a Pydantic model and cast DocList fields into List fields.
@@ -239,5 +241,11 @@ class MyDoc(BaseDoc):
239241
)
240242

241243
model = create_model(base_doc_name, __base__=BaseDoc, **fields)
244+
model.__config__.title = schema.get('title', model.__config__.title)
245+
246+
for k in RESERVED_KEYS:
247+
if k in schema:
248+
schema.pop(k)
249+
model.__config__.schema_extra = schema
242250
cached_models[base_doc_name] = model
243251
return model
Collapse file

‎tests/units/util/test_create_dynamic_code_class.py‎

Copy file name to clipboardExpand all lines: tests/units/util/test_create_dynamic_code_class.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ class ResultTestDoc(BaseDoc):
243243

244244
def test_create_with_field_info():
245245
class CustomDoc(BaseDoc):
246+
"""Here I have the description of the class"""
247+
246248
a: str = Field(examples=['Example here'], another_extra='I am another extra')
247249

248250
CustomDocCopy = create_pure_python_type_model(CustomDoc)
@@ -256,3 +258,7 @@ class CustomDoc(BaseDoc):
256258
new_custom_doc_model.schema().get('properties')['a']['another_extra']
257259
== 'I am another extra'
258260
)
261+
assert (
262+
new_custom_doc_model.schema().get('description')
263+
== 'Here I have the description of the class'
264+
)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.