Skip to content

Commit

Permalink
Merge pull request #113 from bioimage-io/improve_collection_draft
Browse files Browse the repository at this point in the history
include all defaults for collection_draft.json
  • Loading branch information
FynnBe authored Nov 13, 2024
2 parents 1ed0b49 + a1f79d2 commit bff9dfa
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 34 deletions.
28 changes: 0 additions & 28 deletions bioimageio_collection_backoffice/common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from typing import Any, ClassVar, Dict, FrozenSet, Union

import pydantic
from pydantic_core import PydanticUndefined
from typing_extensions import LiteralString


class Node(
Expand All @@ -15,27 +11,3 @@ class Node(
validate_default=False,
):
"""""" # avoid inheriting docstring from `pydantic.BaseModel`

fields_to_set_explicitly: ClassVar[FrozenSet[LiteralString]] = frozenset()
"""set set these fields explicitly with their default value if they are not set,
such that they are always included even when dumping with 'exlude_unset'"""

@pydantic.model_validator(mode="before")
@classmethod
def set_fields_explicitly(
cls, data: Union[Any, Dict[str, Any]]
) -> Union[Any, Dict[str, Any]]:
if isinstance(data, dict):
for name in cls.fields_to_set_explicitly:
if (
name not in data
and name in cls.model_fields
and (
(field_info := cls.model_fields[name]).default
is not PydanticUndefined
or field_info.default_factory is not None
)
):
data[name] = field_info.get_default(call_default_factory=True)

return data # pyright: ignore[reportUnknownVariableType]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from datetime import datetime
from typing import (
ClassVar,
FrozenSet,
List,
Literal,
Optional,
Expand All @@ -12,16 +11,13 @@
)

import pydantic
from typing_extensions import Annotated, LiteralString
from typing_extensions import Annotated

from .._settings import settings
from ..common import Node


class _StatusBase(Node, frozen=True):
fields_to_set_explicitly: ClassVar[FrozenSet[LiteralString]] = frozenset(
{"name", "step", "num_steps", "description"}
)
timestamp: datetime = pydantic.Field(default_factory=datetime.now)
run_url: Optional[str] = settings.run_url

Expand Down
4 changes: 3 additions & 1 deletion bioimageio_collection_backoffice/remote_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,9 @@ def generate_collection_json(
if collection_entries or not list(self.client.ls(collection_output_file_name)):
self.client.put_json(
collection_output_file_name,
collection.model_dump(mode="json", exclude_defaults=True),
collection.model_dump(
mode="json", exclude_defaults=mode == "published"
),
)
else:
logger.error(
Expand Down

0 comments on commit bff9dfa

Please sign in to comment.