Coverage for bioimageio/spec/_internal/node.py: 100%
20 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-18 12:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-18 12:47 +0000
1from __future__ import annotations
3import collections.abc
4from typing import (
5 Any,
6 Mapping,
7 Optional,
8 Type,
9 Union,
10)
12import pydantic
13from typing_extensions import Self
15from .type_guards import is_kwargs
16from .validation_context import ValidationContext, get_validation_context
19def _node_title_generator(node: Type[Node]) -> str:
20 return (
21 f"{node.implemented_type} {node.implemented_format_version}" # pyright: ignore[reportAttributeAccessIssue]
22 if hasattr(node, "implemented_type")
23 and hasattr(node, "implemented_format_version")
24 else f"{node.__module__.replace('bioimageio.spec.', '')}.{node.__name__}"
25 )
28class Node(
29 pydantic.BaseModel,
30 allow_inf_nan=False,
31 extra="forbid",
32 frozen=False,
33 model_title_generator=_node_title_generator,
34 populate_by_name=True,
35 revalidate_instances="never",
36 use_attribute_docstrings=True,
37 validate_assignment=True,
38 validate_default=False,
39 validate_return=True, # TODO: check if False here would bring a speedup and can still be safe
40):
41 """""" # empty docstring to remove all pydantic docstrings from the pdoc spec docs
43 @classmethod
44 def model_validate(
45 cls,
46 obj: Union[Any, Mapping[str, Any]],
47 *,
48 strict: Optional[bool] = None,
49 from_attributes: Optional[bool] = None,
50 context: Union[ValidationContext, Mapping[str, Any], None] = None,
51 by_alias: bool | None = None,
52 by_name: bool | None = None,
53 ) -> Self:
54 """Validate a pydantic model instance.
56 Args:
57 obj: The object to validate.
58 strict: Whether to raise an exception on invalid fields.
59 from_attributes: Whether to extract data from object attributes.
60 context: Additional context to pass to the validator.
62 Raises:
63 ValidationError: If the object failed validation.
65 Returns:
66 The validated description instance.
67 """
68 __tracebackhide__ = True
70 if context is None:
71 context = get_validation_context()
72 elif isinstance(context, collections.abc.Mapping):
73 context = ValidationContext(**context)
75 assert not isinstance(obj, collections.abc.Mapping) or is_kwargs(obj), obj
77 with context:
78 # use validation context as context manager for equal behavior of __init__ and model_validate
79 return super().model_validate(
80 obj, strict=strict, from_attributes=from_attributes
81 )