44import collections .abc
55import inspect
66import os
7- import sys
87from abc import ABC
98from typing import (
109 Any ,
1110 ClassVar ,
1211 Dict ,
1312 FrozenSet ,
14- Generic ,
15- Iterator ,
1613 List ,
1714 Optional ,
18- Set ,
1915 Tuple ,
2016 Type ,
21- TypeVar ,
2217 Union ,
2318 cast ,
2419 get_type_hints ,
3025 DirectoryPath ,
3126 Field ,
3227 GetCoreSchemaHandler ,
28+ PrivateAttr ,
3329 StringConstraints ,
3430 TypeAdapter ,
3531 ValidationInfo ,
4036
4137from bioimageio .spec ._internal .constants import IN_PACKAGE_MESSAGE
4238from bioimageio .spec ._internal .types import NotEmpty , RdfContent , Version , YamlValue
43- from bioimageio .spec ._internal .types .field_validation import is_valid_yaml_mapping
4439from bioimageio .spec ._internal .utils import unindent
4540from bioimageio .spec ._internal .validation_context import InternalValidationContext , get_internal_validation_context
4641from bioimageio .spec .summary import ValidationSummary
4742
48- K = TypeVar ("K" , bound = str )
49- V = TypeVar ("V" )
50-
51- if sys .version_info < (3 , 9 ):
52-
53- class FrozenDictBase (collections .abc .Mapping , Generic [K , V ]): # pyright: ignore[reportMissingTypeArgument]
54- pass
55-
56- else :
57- FrozenDictBase = collections .abc .Mapping [K , V ]
58-
5943
6044class Node (
6145 pydantic .BaseModel ,
6246 extra = "forbid" ,
63- frozen = True ,
47+ frozen = False ,
6448 populate_by_name = True ,
6549 revalidate_instances = "always" ,
6650 validate_assignment = True ,
@@ -134,7 +118,7 @@ def _set_undefined_field_descriptions_from_field_name(cls):
134118 info .description = name
135119
136120
137- class NodeWithExplicitlySetFields (Node , frozen = True ):
121+ class NodeWithExplicitlySetFields (Node ):
138122 fields_to_set_explicitly : ClassVar [FrozenSet [LiteralString ]] = frozenset ()
139123 """set set these fields explicitly with their default value if they are not set,
140124 such that they are always included even when dumping with 'exlude_unset'"""
@@ -150,13 +134,13 @@ def set_fields_explicitly(cls, data: Union[Any, Dict[Any, Any]]) -> Union[Any, D
150134 return data
151135
152136
153- class ResourceDescriptionBase (NodeWithExplicitlySetFields , frozen = True ):
137+ class ResourceDescriptionBase (NodeWithExplicitlySetFields ):
154138 """base class for all resource descriptions"""
155139
156- __slots__ = ("_internal_validation_context" , "_validation_summaries" )
157-
158140 type : str
159141 format_version : str
142+ _internal_validation_context : InternalValidationContext
143+ _validation_summaries : List [ValidationSummary ] = PrivateAttr (default_factory = list )
160144
161145 fields_to_set_explicitly : ClassVar [FrozenSet [LiteralString ]] = frozenset ({"type" , "format_version" })
162146 implemented_format_version : ClassVar [str ]
@@ -184,11 +168,7 @@ def update_context_and_data(cls, data: RdfContent, info: ValidationInfo):
184168
185169 @model_validator (mode = "after" )
186170 def remember_internal_validation_context (self , info : ValidationInfo ) -> Self :
187- self .model_config ["frozen" ] = False
188- self ._validation_summaries : List [ValidationSummary ] = [] # type: ignore[frozen]
189- self ._internal_validation_context : InternalValidationContext
190- self ._internal_validation_context = get_internal_validation_context (info .context ) # type: ignore[frozen]
191- self .model_config ["frozen" ] = True
171+ self ._internal_validation_context = get_internal_validation_context (info .context )
192172 return self
193173
194174 @classmethod
@@ -314,42 +294,5 @@ def _serialize(self) -> str:
314294 return self .data
315295
316296
317- D = TypeVar ("D" )
318-
319-
320- class FrozenDictNode (Node , FrozenDictBase [K , V ], frozen = True ):
321- def __getitem__ (self , item : K ) -> V :
322- try :
323- return getattr (self , item )
324- except AttributeError :
325- raise KeyError (item ) from None
326-
327- def __iter__ (self ) -> Iterator [K ]: # type: ignore iterate over keys like a dict, not (key, value) tuples
328- yield from self .model_fields_set # type: ignore
329-
330- def __len__ (self ) -> int :
331- return len (self .model_fields_set )
332-
333- def keys (self ) -> Set [K ]: # type: ignore
334- return set (self .model_fields_set ) # type: ignore
335-
336- def __contains__ (self , key : Any ):
337- return key in self .model_fields_set
338-
339- def get (self , item : Any , default : D = None ) -> Union [V , D ]:
340- return getattr (self , item , default )
341-
342- @model_validator (mode = "after" )
343- def validate_raw_mapping (self ) -> Self :
344- if not is_valid_yaml_mapping (self ):
345- raise AssertionError (f"{ self } contains values unrepresentable in YAML" )
346-
347- return self
348-
349-
350- class ConfigNode (FrozenDictNode [NotEmpty [str ], YamlValue ], frozen = True ):
351- model_config = {** Node .model_config , "extra" : "allow" }
352-
353-
354- class Kwargs (FrozenDictNode [NotEmpty [str ], YamlValue ], frozen = True ):
355- model_config = {** Node .model_config , "extra" : "allow" }
297+ ConfigNode = Dict [NotEmpty [str ], YamlValue ]
298+ Kwargs = Dict [NotEmpty [str ], YamlValue ]
0 commit comments