Skip to content

Commit 8a441de

Browse files
committed
turn original_version into Version
1 parent b82ecac commit 8a441de

File tree

5 files changed

+11
-12
lines changed

5 files changed

+11
-12
lines changed

bioimageio/spec/_internal/base_nodes.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from typing_extensions import Annotated, LiteralString, Self
4040

4141
from bioimageio.spec._internal.constants import IN_PACKAGE_MESSAGE
42-
from bioimageio.spec._internal.types import NotEmpty, RdfContent, YamlValue
42+
from bioimageio.spec._internal.types import NotEmpty, RdfContent, Version, YamlValue
4343
from bioimageio.spec._internal.types.field_validation import is_valid_yaml_mapping
4444
from bioimageio.spec._internal.utils import unindent
4545
from bioimageio.spec._internal.validation_context import InternalValidationContext, get_internal_validation_context
@@ -50,7 +50,7 @@
5050

5151
if sys.version_info < (3, 9):
5252

53-
class FrozenDictBase(collections.abc.Mapping, Generic[K, V]):
53+
class FrozenDictBase(collections.abc.Mapping, Generic[K, V]): # pyright: ignore[reportMissingTypeArgument]
5454
pass
5555

5656
else:
@@ -209,8 +209,7 @@ def _update_context(cls, context: InternalValidationContext, data: RdfContent) -
209209
# set original format if possible
210210
original_format = data.get("format_version")
211211
if "original_format" not in context and isinstance(original_format, str) and original_format.count(".") == 2:
212-
context["original_format"] = cast(Tuple[int, int, int], tuple(map(int, original_format.split("."))))
213-
assert len(context["original_format"]) == 3
212+
context["original_format"] = Version(original_format)
214213

215214
@classmethod
216215
def model_validate(
@@ -337,7 +336,7 @@ def keys(self) -> Set[K]: # type: ignore
337336
def __contains__(self, key: Any):
338337
return key in self.model_fields_set
339338

340-
def get(self, item: Any, default: D = None) -> Union[V, D]: # type: ignore
339+
def get(self, item: Any, default: D = None) -> Union[V, D]:
341340
return getattr(self, item, default)
342341

343342
@model_validator(mode="after")

bioimageio/spec/_internal/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ def iterate_annotated_union(typ: type) -> Iterator[Any]:
4646
def nest_dict(flat_dict: Dict[Tuple[K, ...], V]) -> NestedDict[K, V]:
4747
res: NestedDict[K, V] = {}
4848
for k, v in flat_dict.items():
49-
node = res
49+
node: Union[Dict[K, Union[NestedDict[K, V], V]], NestedDict[K, V]] = res
5050
for kk in k[:-1]:
51-
if not isinstance(node, dict):
51+
if not isinstance(node, dict): # pyright: ignore[reportUnnecessaryIsInstance]
5252
raise ValueError(f"nesting level collision for flat key {k} at {kk}")
5353
d: NestedDict[K, V] = {}
54-
node = node.setdefault(kk, d)
54+
node = node.setdefault(kk, d) # type: ignore
5555

56-
if not isinstance(node, dict):
56+
if not isinstance(node, dict): # pyright: ignore[reportUnnecessaryIsInstance]
5757
raise ValueError(f"nesting level collision for flat key {k}")
5858

5959
node[k[-1]] = v

bioimageio/spec/_internal/validation_context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing_extensions import NotRequired, TypedDict
66

77
from bioimageio.spec._internal.constants import ERROR, WARNING_LEVEL_CONTEXT_KEY
8+
from bioimageio.spec._internal.types._version import Version
89

910
WarningSeverity = Literal[20, 30, 35]
1011
WarningLevel = Literal[WarningSeverity, 50]
@@ -32,7 +33,7 @@ class InternalValidationContext(TypedDict):
3233
warning_level: WarningLevel
3334
"""raise warnings of severity s as validation errors if s >= `warning_level`"""
3435

35-
original_format: NotRequired[Tuple[int, int, int]]
36+
original_format: NotRequired[Version]
3637
"""original format version of the validation data (set dynamically during validation of resource descriptions)."""
3738

3839
collection_base_content: NotRequired[Dict[str, Any]]

bioimageio/spec/generic/v0_2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class _Person(Node, frozen=True):
4949
@classmethod
5050
def convert_name(cls, name: Any, info: FieldValidationInfo):
5151
ctxt = get_internal_validation_context(info.context)
52-
if "original_format" in ctxt and ctxt["original_format"] < (0, 2, 3) and isinstance(name, str):
52+
if "original_format" in ctxt and ctxt["original_format"] < Version("0.2.3") and isinstance(name, str):
5353
name = name.replace("/", "").replace("\\", "")
5454

5555
return name

bioimageio/spec/model/v0_4.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import warnings
43
from typing import Any, ClassVar, Dict, FrozenSet, List, Literal, NewType, Optional, Sequence, Tuple, Union
54

65
from annotated_types import Ge, Interval, MaxLen, MinLen, MultipleOf

0 commit comments

Comments
 (0)