Skip to content

Commit

Permalink
_update_context_and_data -> _update_context
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Sep 21, 2023
1 parent 3b5b80c commit 3186b03
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 34 deletions.
29 changes: 5 additions & 24 deletions bioimageio/spec/_internal/base_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,9 @@ def validation_summaries(self) -> List[ValidationSummary]:

@model_validator(mode="before")
@classmethod
def update_context_and_data(cls, data: Union[Any, Mapping[Any, Any]], info: ValidationInfo):
if isinstance(data, collections.abc.Mapping):
if isinstance(data, dict):
data_dict = data
else:
data_dict = dict(data)

cls._update_context_and_data(get_internal_validation_context(info.context), data_dict)
return data_dict
else:
return data
def update_context(cls, data: RdfContent, info: ValidationInfo):
cls._update_context(get_internal_validation_context(info.context), data)
return data

@model_validator(mode="after")
def remember_internal_validation_context(self, info: ValidationInfo) -> Self:
Expand All @@ -208,18 +200,7 @@ def __pydantic_init_subclass__(cls, **kwargs: Any):
assert len(cls.implemented_format_version_tuple) == 3

@classmethod
def _update_context_and_data(cls, context: InternalValidationContext, data: Dict[Any, Any]) -> None:
# set original format if possible
original_format = data.get("format_version")
if "original_format" not in context and isinstance(original_format, str) and original_format.count(".") == 2:
context["original_format"] = cast(Tuple[int, int, int], tuple(map(int, original_format.split("."))))
assert len(context["original_format"]) == 3

cls.convert_from_older_format(data, context)

@classmethod
def convert_from_older_format(cls, data: RdfContent, context: InternalValidationContext) -> None:
"""A node may `convert` it's raw data from an older format."""
def _update_context(cls, context: InternalValidationContext, data: RdfContent) -> None:
pass

@classmethod
Expand Down Expand Up @@ -251,7 +232,7 @@ def model_validate(
context = get_internal_validation_context(context)
if isinstance(obj, dict):
assert all(isinstance(k, str) for k in obj)
cls._update_context_and_data(context, obj)
cls._update_context(context, obj)

return super().model_validate(
obj, strict=strict, from_attributes=from_attributes, context=cast(Dict[str, Any], context)
Expand Down
8 changes: 2 additions & 6 deletions bioimageio/spec/_internal/validation_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class InternalValidationContext(TypedDict):
warning_level: WarningLevel
"""raise warnings of severity s as validation errors if s >= `warning_level`"""

original_format: NotRequired[Tuple[int, int, int]]
"""original format version of the validation data (set dynamically during validation of resource descriptions)."""

collection_base_content: NotRequired[Dict[str, Any]]
"""Collection base content (set dynamically during validation of collection resource descriptions)."""

Expand All @@ -55,8 +52,7 @@ def get_internal_validation_context(
file_name=file_name or given_context.get("file_name", "rdf.bioimageio.yaml"),
warning_level=warning_level or given_context.get(WARNING_LEVEL_CONTEXT_KEY, ERROR),
)
for k in {"original_format", "collection_base_content"}: # TypedDict.__optional_keys__ requires py>=3.9
if k in given_context:
ret[k] = given_context[k]
if "collection_base_content" in given_context:
ret["collection_base_content"] = given_context["collection_base_content"]

return ret
4 changes: 2 additions & 2 deletions bioimageio/spec/collection/v0_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def check_unique_ids_impl(value: NonEmpty[Tuple[CollectionEntryBase, ...]]):
seen[v.id] = i

@classmethod
def _update_context_and_data(cls, context: InternalValidationContext, data: Dict[Any, Any]) -> None:
super()._update_context_and_data(context, data)
def _update_context(cls, context: InternalValidationContext, data: RdfContent) -> None:
super()._update_context(context, data)
collection_base_content = {k: v for k, v in data.items() if k != "collection"}
assert "collection_base_content" not in context or context["collection_base_content"] == collection_base_content
context["collection_base_content"] = collection_base_content
Expand Down
4 changes: 2 additions & 2 deletions bioimageio/spec/collection/v0_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Collection(GenericBase, extra="allow", frozen=True, title="bioimage.io col
type: Literal["collection"] = "collection"

@classmethod
def _update_context_and_data(cls, context: InternalValidationContext, data: Dict[Any, Any]) -> None:
super()._update_context_and_data(context, data)
def _update_context(cls, context: InternalValidationContext, data: RdfContent) -> None:
super()._update_context(context, data)
collection_base_content = {k: v for k, v in data.items() if k != "collection"}
assert "collection_base_content" not in context or context["collection_base_content"] == collection_base_content
context["collection_base_content"] = collection_base_content
Expand Down

0 comments on commit 3186b03

Please sign in to comment.