From c40ec3352fb128fb477fee5dafbdf5208c0e3f8f Mon Sep 17 00:00:00 2001 From: fynnbe Date: Mon, 28 Oct 2024 16:06:06 +0100 Subject: [PATCH 1/2] do not perform_io_checks when reevaluating on field assignment --- bioimageio/spec/model/v0_4.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/bioimageio/spec/model/v0_4.py b/bioimageio/spec/model/v0_4.py index 03bf0b3a3..674794a86 100644 --- a/bioimageio/spec/model/v0_4.py +++ b/bioimageio/spec/model/v0_4.py @@ -57,6 +57,7 @@ from .._internal.types import LicenseId as LicenseId from .._internal.types import NotEmpty as NotEmpty from .._internal.url import HttpUrl as HttpUrl +from .._internal.validation_context import validation_context_var from .._internal.validator_annotations import AfterValidator, RestrictCharacters from .._internal.version_type import Version as Version from .._internal.warning_levels import ALERT, INFO @@ -875,7 +876,7 @@ class LinkedModel(Node): def package_weights( - value: Node, + value: Node, # Union[v0_4.WeightsDescr, v0_5.WeightsDescr] handler: SerializerFunctionWrapHandler, info: SerializationInfo, ): @@ -891,14 +892,11 @@ def package_weights( + f" ({ctxt.weights_priority_order}) is present in the given model." ) - # remove links to parent entry (otherwise we cannot remove the parent) - for _, w in value: - if w is not None: - w.parent = None - - for field_name in value.model_fields: - if field_name != wf: - setattr(value, field_name, None) + # create WeightsDescr with single weight format entry + with validation_context_var.get().replace(perform_io_checks=False): + new_w = w.model_copy() + new_w.parent = None + value = value.__class__(**{wf: new_w}) return handler( value, info # pyright: ignore[reportArgumentType] # taken from pydantic docs From ef9d8753293b4bbd3a6f85b9f21f0ee863c00b31 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Mon, 28 Oct 2024 16:43:34 +0100 Subject: [PATCH 2/2] use model_construct instead of copy to avoid revalidation on assignment --- bioimageio/spec/model/v0_4.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/bioimageio/spec/model/v0_4.py b/bioimageio/spec/model/v0_4.py index 674794a86..34bc5760f 100644 --- a/bioimageio/spec/model/v0_4.py +++ b/bioimageio/spec/model/v0_4.py @@ -57,7 +57,6 @@ from .._internal.types import LicenseId as LicenseId from .._internal.types import NotEmpty as NotEmpty from .._internal.url import HttpUrl as HttpUrl -from .._internal.validation_context import validation_context_var from .._internal.validator_annotations import AfterValidator, RestrictCharacters from .._internal.version_type import Version as Version from .._internal.warning_levels import ALERT, INFO @@ -892,11 +891,10 @@ def package_weights( + f" ({ctxt.weights_priority_order}) is present in the given model." ) - # create WeightsDescr with single weight format entry - with validation_context_var.get().replace(perform_io_checks=False): - new_w = w.model_copy() - new_w.parent = None - value = value.__class__(**{wf: new_w}) + assert isinstance(w, Node), type(w) + # construct WeightsDescr with new single weight format entry + new_w = w.model_construct(**{k: v for k, v in w if k != "parent"}) + value = value.model_construct(None, **{wf: new_w}) return handler( value, info # pyright: ignore[reportArgumentType] # taken from pydantic docs