|
29 | 29 | StringConstraints, |
30 | 30 | TypeAdapter, |
31 | 31 | ValidationInfo, |
32 | | - model_validator, # type: ignore |
| 32 | + model_validator, |
33 | 33 | ) |
34 | 34 | from pydantic_core import PydanticUndefined, core_schema |
35 | 35 | from typing_extensions import Annotated, LiteralString, Self |
36 | 36 |
|
37 | 37 | from bioimageio.spec._internal.constants import IN_PACKAGE_MESSAGE |
38 | | -from bioimageio.spec._internal.types import NotEmpty, RdfContent, Version, YamlValue |
| 38 | +from bioimageio.spec._internal.types import RdfContent, Version |
39 | 39 | from bioimageio.spec._internal.utils import unindent |
40 | 40 | from bioimageio.spec._internal.validation_context import InternalValidationContext, get_internal_validation_context |
41 | 41 | from bioimageio.spec.summary import ValidationSummary |
@@ -137,9 +137,9 @@ def set_fields_explicitly(cls, data: Union[Any, Dict[Any, Any]]) -> Union[Any, D |
137 | 137 | class ResourceDescriptionBase(NodeWithExplicitlySetFields): |
138 | 138 | """base class for all resource descriptions""" |
139 | 139 |
|
140 | | - type: str |
141 | | - format_version: str |
142 | | - _internal_validation_context: InternalValidationContext |
| 140 | + _internal_validation_context: InternalValidationContext = PrivateAttr( |
| 141 | + default_factory=get_internal_validation_context |
| 142 | + ) |
143 | 143 | _validation_summaries: List[ValidationSummary] = PrivateAttr(default_factory=list) |
144 | 144 |
|
145 | 145 | fields_to_set_explicitly: ClassVar[FrozenSet[LiteralString]] = frozenset({"type", "format_version"}) |
@@ -174,7 +174,7 @@ def remember_internal_validation_context(self, info: ValidationInfo) -> Self: |
174 | 174 | @classmethod |
175 | 175 | def __pydantic_init_subclass__(cls, **kwargs: Any): |
176 | 176 | super().__pydantic_init_subclass__(**kwargs) |
177 | | - if cls.model_fields["format_version"].default is not PydanticUndefined: |
| 177 | + if "format_version" in cls.model_fields and cls.model_fields["format_version"].default is not PydanticUndefined: |
178 | 178 | cls.implemented_format_version = cls.model_fields["format_version"].default |
179 | 179 | if "." not in cls.implemented_format_version: |
180 | 180 | cls.implemented_format_version_tuple = (0, 0, 0) |
@@ -228,6 +228,8 @@ def model_validate( |
228 | 228 |
|
229 | 229 |
|
230 | 230 | class StringNode(collections.UserString, ABC): |
| 231 | + """deprecated! don't use for new spec fields!""" |
| 232 | + |
231 | 233 | _pattern: ClassVar[str] |
232 | 234 | _node_class: Type[Node] |
233 | 235 | _node: Optional[Node] = None |
@@ -266,7 +268,7 @@ def __getattr__(self, name: str): |
266 | 268 | @classmethod |
267 | 269 | def __get_pydantic_core_schema__(cls, source: Type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema: |
268 | 270 | assert issubclass(source, StringNode) |
269 | | - return core_schema.general_after_validator_function( |
| 271 | + return core_schema.with_info_after_validator_function( |
270 | 272 | cls._validate, |
271 | 273 | core_schema.str_schema(pattern=cls._pattern), |
272 | 274 | serialization=core_schema.plain_serializer_function_ser_schema( |
@@ -294,5 +296,15 @@ def _serialize(self) -> str: |
294 | 296 | return self.data |
295 | 297 |
|
296 | 298 |
|
297 | | -ConfigNode = Dict[NotEmpty[str], YamlValue] |
298 | | -Kwargs = Dict[NotEmpty[str], YamlValue] |
| 299 | +class KwargsNode(Node): |
| 300 | + def get(self, item: str, default: Any = None) -> Any: |
| 301 | + return self[item] if item in self else default |
| 302 | + |
| 303 | + def __getitem__(self, item: str) -> Any: |
| 304 | + if item in self.model_fields: |
| 305 | + return getattr(self, item) |
| 306 | + else: |
| 307 | + raise KeyError(item) |
| 308 | + |
| 309 | + def __contains__(self, item: str) -> int: |
| 310 | + return item in self.model_fields |
0 commit comments