@@ -305,27 +305,28 @@ def get_value_by_path_id(cls, data: DataStructure, path_id: str, get_key: bool =
305305 - `get_key` -⠀if true and the final item is in a dict, it returns the key instead of the value"""
306306 parent : Optional [DataStructure ] = None
307307 path = cls ._sep_path_id (path_id )
308+ current_data : Any = data
308309
309310 for i , path_idx in enumerate (path ):
310- if isinstance (data , dict ):
311- keys = list (data .keys ())
311+ if isinstance (current_data , dict ):
312+ keys = list (current_data .keys ())
312313 if i == len (path ) - 1 and get_key :
313314 return keys [path_idx ]
314- parent = data
315- data = data [keys [path_idx ]]
315+ parent = current_data
316+ current_data = current_data [keys [path_idx ]]
316317
317- elif isinstance (data , IndexIterableTypes ):
318+ elif isinstance (current_data , IndexIterableTypes ):
318319 if i == len (path ) - 1 and get_key :
319320 if parent is None or not isinstance (parent , dict ):
320321 raise ValueError (f"Cannot get key from a non-dict parent at path '{ path [:i + 1 ]} '" )
321- return next (key for key , value in parent .items () if value is data )
322- parent = data
323- data = list (data )[path_idx ] # CONVERT TO LIST FOR INDEXING
322+ return next (key for key , value in parent .items () if value is current_data )
323+ parent = current_data
324+ current_data = list (current_data )[path_idx ] # CONVERT TO LIST FOR INDEXING
324325
325326 else :
326- raise TypeError (f"Unsupported type '{ type (data )} ' at path '{ path [:i + 1 ]} '" )
327+ raise TypeError (f"Unsupported type '{ type (current_data )} ' at path '{ path [:i + 1 ]} '" )
327328
328- return data
329+ return current_data
329330
330331 @classmethod
331332 def set_value_by_path_id (cls , data : DataStructure , update_values : dict [str , Any ]) -> DataStructure :
@@ -468,21 +469,33 @@ def _compare_nested(
468469 ) -> bool :
469470 if any (current_path == path [:len (current_path )] for path in ignore_paths ):
470471 return True
472+
471473 if type (data1 ) is not type (data2 ):
472474 return False
475+
473476 if isinstance (data1 , dict ) and isinstance (data2 , dict ):
474477 if set (data1 .keys ()) != set (data2 .keys ()):
475478 return False
476- return all (cls ._compare_nested (data1 [key ], data2 [key ], ignore_paths , current_path + [key ]) for key in data1 )
477- if isinstance (data1 , (list , tuple )):
479+ return all (cls ._compare_nested ( \
480+ data1 = data1 [key ],
481+ data2 = data2 [key ],
482+ ignore_paths = ignore_paths ,
483+ current_path = current_path + [key ],
484+ ) for key in data1 )
485+
486+ elif isinstance (data1 , (list , tuple )):
478487 if len (data1 ) != len (data2 ):
479488 return False
480- return all (
481- cls ._compare_nested (item1 , item2 , ignore_paths , current_path + [str (i )])
482- for i , (item1 , item2 ) in enumerate (zip (data1 , data2 ))
483- )
484- if isinstance (data1 , (set , frozenset )):
489+ return all (cls ._compare_nested ( \
490+ data1 = item1 ,
491+ data2 = item2 ,
492+ ignore_paths = ignore_paths ,
493+ current_path = current_path + [str (i )],
494+ ) for i , (item1 , item2 ) in enumerate (zip (data1 , data2 )))
495+
496+ elif isinstance (data1 , (set , frozenset )):
485497 return data1 == data2
498+
486499 return data1 == data2
487500
488501 @staticmethod
@@ -500,38 +513,39 @@ def _sep_path_id(path_id: str) -> list[int]:
500513 raise ValueError (f"Path ID '{ path_id } ' is an invalid format." )
501514
502515 @staticmethod
503- def _get_path_id (path : str , path_sep : str , data_obj : Any , ignore_not_found : bool ) -> Optional [str ]:
516+ def _get_path_id (path : str , path_sep : str , data_obj : DataStructure , ignore_not_found : bool ) -> Optional [str ]:
504517 """Internal method to process a single data-path and generate its path ID."""
505518 keys = path .split (path_sep )
506519 path_ids , max_id_length = [], 0
520+ current_data : Any = data_obj
507521
508522 for key in keys :
509- if isinstance (data_obj , dict ):
523+ if isinstance (current_data , dict ):
510524 if key .isdigit ():
511525 if ignore_not_found :
512526 return None
513527 raise TypeError (f"Key '{ key } ' is invalid for a dict type." )
514528
515529 try :
516- idx = list (data_obj .keys ()).index (key )
517- data_obj = data_obj [key ]
530+ idx = list (current_data .keys ()).index (key )
531+ current_data = current_data [key ]
518532 except (ValueError , KeyError ):
519533 if ignore_not_found :
520534 return None
521535 raise KeyError (f"Key '{ key } ' not found in dict." )
522536
523- elif isinstance (data_obj , IndexIterableTypes ):
537+ elif isinstance (current_data , IndexIterableTypes ):
524538 try :
525539 idx = int (key )
526- data_obj = list (data_obj )[idx ] # CONVERT TO LIST FOR INDEXING
540+ current_data = list (current_data )[idx ] # CONVERT TO LIST FOR INDEXING
527541 except ValueError :
528542 try :
529- idx = list (data_obj ).index (key )
530- data_obj = list (data_obj )[idx ]
543+ idx = list (current_data ).index (key )
544+ current_data = list (current_data )[idx ]
531545 except ValueError :
532546 if ignore_not_found :
533547 return None
534- raise ValueError (f"Value '{ key } ' not found in '{ type (data_obj ).__name__ } '" )
548+ raise ValueError (f"Value '{ key } ' not found in '{ type (current_data ).__name__ } '" )
535549
536550 else :
537551 break
@@ -544,27 +558,31 @@ def _get_path_id(path: str, path_sep: str, data_obj: Any, ignore_not_found: bool
544558 return f"{ max_id_length } >{ '' .join (id .zfill (max_id_length ) for id in path_ids )} "
545559
546560 @classmethod
547- def _set_nested_val (cls , data : Any , id_path : list [int ], value : Any ) -> Any :
561+ def _set_nested_val (cls , data : DataStructure , id_path : list [int ], value : Any ) -> Any :
548562 """Internal method to set a value in a nested data structure based on the provided ID path."""
563+ current_data : Any = data
564+
549565 if len (id_path ) == 1 :
550- if isinstance (data , dict ):
551- keys , data = list (data .keys ()), dict (data )
552- data [keys [id_path [0 ]]] = value
553- elif isinstance (data , IndexIterableTypes ):
554- was_t , data = type (data ), list (data )
555- data [id_path [0 ]] = value
556- data = was_t (data )
566+ if isinstance (current_data , dict ):
567+ keys , data_dict = list (current_data .keys ()), dict (current_data )
568+ data_dict [keys [id_path [0 ]]] = value
569+ return data_dict
570+ elif isinstance (current_data , IndexIterableTypes ):
571+ was_t , data_list = type (current_data ), list (current_data )
572+ data_list [id_path [0 ]] = value
573+ return was_t (data_list )
557574
558575 else :
559- if isinstance (data , dict ):
560- keys , data = list (data .keys ()), dict (data )
561- data [keys [id_path [0 ]]] = cls ._set_nested_val (data [keys [id_path [0 ]]], id_path [1 :], value )
562- elif isinstance (data , IndexIterableTypes ):
563- was_t , data = type (data ), list (data )
564- data [id_path [0 ]] = cls ._set_nested_val (data [id_path [0 ]], id_path [1 :], value )
565- data = was_t (data )
566-
567- return data
576+ if isinstance (current_data , dict ):
577+ keys , data_dict = list (current_data .keys ()), dict (current_data )
578+ data_dict [keys [id_path [0 ]]] = cls ._set_nested_val (data_dict [keys [id_path [0 ]]], id_path [1 :], value )
579+ return data_dict
580+ elif isinstance (current_data , IndexIterableTypes ):
581+ was_t , data_list = type (current_data ), list (current_data )
582+ data_list [id_path [0 ]] = cls ._set_nested_val (data_list [id_path [0 ]], id_path [1 :], value )
583+ return was_t (data_list )
584+
585+ return current_data
568586
569587
570588class _DataRemoveCommentsHelper :
0 commit comments