Skip to content
Snippets Groups Projects
Commit 8ab2f4cb authored by Maurice Herné's avatar Maurice Herné
Browse files

Better way to do the attachments fix

parent 7e3ad3e2
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ from typing import (
Annotated,
Union,
Literal,
Optional,
Any,
)
from uuid import uuid4
......@@ -17,6 +18,7 @@ from pydantic import (
field_validator,
ValidationInfo,
ConfigDict,
model_serializer,
)
from ..configuration import (
......@@ -78,13 +80,35 @@ class BaseNotebookCellModel(BaseModel):
raise PydanticUseDefault()
return value
@model_serializer
def drop_optional_none_fields(self):
"""
Drops optional fields that are None.
We should look for a more dynamic solution to this.
Currently those fields need to be hardcoded because there is no way to specifically
detect fields that are annotated as Optional because the matching only returns true
if you get it exactly (Optional != Optional[dict]).
:return: The model with the optional fields that are None removed.
"""
optional_fields = [
"attachments",
]
drop = {key for key, value in self.__dict__.items() if value is None and key in optional_fields}
return {
key: value
for key, value in self.__dict__.items()
if key not in drop
}
class NotebookMarkdownCellModel(BaseNotebookCellModel):
"""
"Notebook markdown cell."
"""
cell_type: Literal["markdown"]
attachments: dict = Field(default={}, description="Media attachments (e.g. inline images), stored as mimebundle keyed by filename.")
attachments: Optional[dict] = Field(default=None, description="Media attachments (e.g. inline images), stored as mimebundle keyed by filename.")
class NotebookCodeCellModel(BaseNotebookCellModel):
......@@ -101,7 +125,7 @@ class NotebookRawCellModel(BaseNotebookCellModel):
"Notebook raw nbconvert cell."
"""
cell_type: Literal["raw"]
attachments: dict = Field(default={}, description="Media attachments (e.g. inline images), stored as mimebundle keyed by filename.")
attachments: Optional[dict] = Field(default=None, description="Media attachments (e.g. inline images), stored as mimebundle keyed by filename.")
NotebookCellAnnotation = Annotated[
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment