Skip to content

Commit 033d47a

Browse files
committed
Move code blocks back to easier reviewing. Consider reverting this change after review.
1 parent ba3af17 commit 033d47a

File tree

1 file changed

+64
-64
lines changed

1 file changed

+64
-64
lines changed

src/easyscience/variable/parameter.py

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -548,70 +548,6 @@ def as_dict(self, skip: Optional[List[str]] = None) -> Dict[str, Any]:
548548

549549
return raw_dict
550550

551-
@classmethod
552-
def from_dict(cls, obj_dict: dict) -> 'Parameter':
553-
"""
554-
Custom deserialization to handle parameter dependencies.
555-
Override the parent method to handle dependency information.
556-
"""
557-
# Extract dependency information before creating the parameter
558-
raw_dict = obj_dict.copy() # Don't modify the original dict
559-
dependency_string = raw_dict.pop('_dependency_string', None)
560-
dependency_map_dependency_ids = raw_dict.pop('_dependency_map_dependency_ids', None)
561-
is_independent = raw_dict.pop('_independent', True)
562-
# Note: Keep _dependency_id in the dict so it gets passed to __init__
563-
564-
# Create the parameter using the base class method (dependency_id is now handled in __init__)
565-
param = super().from_dict(raw_dict)
566-
567-
# Store dependency information for later resolution
568-
if not is_independent:
569-
param._pending_dependency_string = dependency_string
570-
param._pending_dependency_map_dependency_ids = dependency_map_dependency_ids
571-
# Keep parameter as independent initially - will be made dependent after all objects are loaded
572-
param._independent = True
573-
574-
return param
575-
576-
def resolve_pending_dependencies(self) -> None:
577-
"""Resolve pending dependencies after deserialization.
578-
579-
This method should be called after all parameters have been deserialized
580-
to establish dependency relationships using dependency_ids.
581-
"""
582-
if hasattr(self, '_pending_dependency_string'):
583-
dependency_string = self._pending_dependency_string
584-
dependency_map = {}
585-
586-
if hasattr(self, '_pending_dependency_map_dependency_ids'):
587-
dependency_map_dependency_ids = self._pending_dependency_map_dependency_ids
588-
589-
# Build dependency_map by looking up objects by dependency_id
590-
for key, dependency_id in dependency_map_dependency_ids.items():
591-
dep_obj = self._find_parameter_by_dependency_id(dependency_id)
592-
if dep_obj is not None:
593-
dependency_map[key] = dep_obj
594-
else:
595-
raise ValueError(f"Cannot find parameter with dependency_id '{dependency_id}'")
596-
597-
# Establish the dependency relationship
598-
try:
599-
self.make_dependent_on(dependency_expression=dependency_string, dependency_map=dependency_map)
600-
except Exception as e:
601-
raise ValueError(f"Error establishing dependency '{dependency_string}': {e}")
602-
603-
# Clean up temporary attributes
604-
delattr(self, '_pending_dependency_string')
605-
delattr(self, '_pending_dependency_map_dependency_ids')
606-
607-
def _find_parameter_by_dependency_id(self, dependency_id: str) -> Optional['DescriptorNumber']:
608-
"""Find a parameter by its dependency_id from all parameters in the global map."""
609-
for obj in self._global_object.map._store.values():
610-
if isinstance(obj, DescriptorNumber) and hasattr(obj, '_dependency_id') and obj._dependency_id == dependency_id:
611-
return obj
612-
return None
613-
614-
615551
def _revert_dependency(self, skip_detach=False) -> None:
616552
"""
617553
Revert the dependency to the old dependency. This is used when an error is raised during setting the dependency.
@@ -654,6 +590,31 @@ def _process_dependency_unique_names(self, dependency_expression: str):
654590
raise ValueError(f'The object with unique_name {stripped_name} is not a Parameter or DescriptorNumber. Please check your dependency expression.') # noqa: E501
655591
self._clean_dependency_string = clean_dependency_string
656592

593+
@classmethod
594+
def from_dict(cls, obj_dict: dict) -> 'Parameter':
595+
"""
596+
Custom deserialization to handle parameter dependencies.
597+
Override the parent method to handle dependency information.
598+
"""
599+
# Extract dependency information before creating the parameter
600+
raw_dict = obj_dict.copy() # Don't modify the original dict
601+
dependency_string = raw_dict.pop('_dependency_string', None)
602+
dependency_map_dependency_ids = raw_dict.pop('_dependency_map_dependency_ids', None)
603+
is_independent = raw_dict.pop('_independent', True)
604+
# Note: Keep _dependency_id in the dict so it gets passed to __init__
605+
606+
# Create the parameter using the base class method (dependency_id is now handled in __init__)
607+
param = super().from_dict(raw_dict)
608+
609+
# Store dependency information for later resolution
610+
if not is_independent:
611+
param._pending_dependency_string = dependency_string
612+
param._pending_dependency_map_dependency_ids = dependency_map_dependency_ids
613+
# Keep parameter as independent initially - will be made dependent after all objects are loaded
614+
param._independent = True
615+
616+
return param
617+
657618
def __copy__(self) -> Parameter:
658619
new_obj = super().__copy__()
659620
new_obj._callback = property()
@@ -987,3 +948,42 @@ def __abs__(self) -> Parameter:
987948
parameter = Parameter.from_scipp(name=self.name, full_value=new_full_value, min=min_value, max=max_value)
988949
parameter.name = parameter.unique_name
989950
return parameter
951+
952+
def resolve_pending_dependencies(self) -> None:
953+
"""Resolve pending dependencies after deserialization.
954+
955+
This method should be called after all parameters have been deserialized
956+
to establish dependency relationships using dependency_ids.
957+
"""
958+
if hasattr(self, '_pending_dependency_string'):
959+
dependency_string = self._pending_dependency_string
960+
dependency_map = {}
961+
962+
if hasattr(self, '_pending_dependency_map_dependency_ids'):
963+
dependency_map_dependency_ids = self._pending_dependency_map_dependency_ids
964+
965+
# Build dependency_map by looking up objects by dependency_id
966+
for key, dependency_id in dependency_map_dependency_ids.items():
967+
dep_obj = self._find_parameter_by_dependency_id(dependency_id)
968+
if dep_obj is not None:
969+
dependency_map[key] = dep_obj
970+
else:
971+
raise ValueError(f"Cannot find parameter with dependency_id '{dependency_id}'")
972+
973+
# Establish the dependency relationship
974+
try:
975+
self.make_dependent_on(dependency_expression=dependency_string, dependency_map=dependency_map)
976+
except Exception as e:
977+
raise ValueError(f"Error establishing dependency '{dependency_string}': {e}")
978+
979+
# Clean up temporary attributes
980+
delattr(self, '_pending_dependency_string')
981+
delattr(self, '_pending_dependency_map_dependency_ids')
982+
983+
def _find_parameter_by_dependency_id(self, dependency_id: str) -> Optional['DescriptorNumber']:
984+
"""Find a parameter by its dependency_id from all parameters in the global map."""
985+
for obj in self._global_object.map._store.values():
986+
if isinstance(obj, DescriptorNumber) and hasattr(obj, '_dependency_id') and obj._dependency_id == dependency_id:
987+
return obj
988+
return None
989+

0 commit comments

Comments
 (0)