@@ -53,13 +53,6 @@ def __init__(self, target: dict, parent_optsdict: "OptsDict", key: str):
5353object .__setattr__ (self ,"_parent" ,parent_optsdict )
5454object .__setattr__ (self ,"_key" ,key )
5555object .__setattr__ (self ,"_copied" ,False )
56- import sys
57-
58- print (
59- f"[DICTPROXY] Created DictProxy for key={ key } , target type={ type (target ).__name__ } , target len={ len (target )} , target id={ id (target )} " ,
60- file = sys .stderr ,
61- flush = True ,
62- )
6356
6457def _ensure_copied (self ):
6558"""Copy target to parent's _local on first mutation."""
@@ -109,16 +102,7 @@ def __iter__(self):
109102return iter (object .__getattribute__ (self ,"_target" ))
110103
111104def __len__ (self ):
112- target = object .__getattribute__ (self ,"_target" )
113- key = object .__getattribute__ (self ,"_key" )
114- import sys
115-
116- print (
117- f"[DICTPROXY] __len__ called for key={ key } , target id={ id (target )} , target len={ len (target )} " ,
118- file = sys .stderr ,
119- flush = True ,
120- )
121- return len (target )
105+ return len (object .__getattribute__ (self ,"_target" ))
122106
123107def __repr__ (self ):
124108return repr (object .__getattribute__ (self ,"_target" ))
@@ -527,42 +511,22 @@ def __getitem__(self, key: str) -> Any:
527511if found :
528512# Wrap mutable values in proxies to catch mutations
529513if isinstance (value ,dict )and not isinstance (value ,OptsDict ):
530- import sys
531-
532- print (
533- f"[OPTSDICT] __getitem__({ key } ): Creating DictProxy from parent chain, value type={ type (value ).__name__ } , len={ len (value )if isinstance (value ,dict )else 'N/A' } , id={ id (value )} " ,
534- file = sys .stderr ,
535- flush = True ,
536- )
537514return DictProxy (value ,self ,key )
538515elif isinstance (value ,list ):
539516return ListProxy (value ,self ,key )
540517# Immutable values can be returned directly
541518return value
542519
543520# Check base (root level only)
544- if self ._parent is None :
545- import sys
546-
547- print (
548- f"[OPTSDICT] __getitem__({ key } ): Checking base, key in base={ key in self ._base } , base id={ id (self ._base )} , base keys={ list (self ._base .keys ())[:10 ]} " ,
549- file = sys .stderr ,
550- flush = True ,
551- )
552- if key in self ._base :
553- value = self ._base [key ]
554- # Even root instances need proxies to track when values are mutated
555- # This allows us to know when a key has been accessed/modified
556- if isinstance (value ,dict )and not isinstance (value ,OptsDict ):
557- print (
558- f"[OPTSDICT] __getitem__({ key } ): Creating DictProxy from base, value type={ type (value ).__name__ } , len={ len (value )if isinstance (value ,dict )else 'N/A' } , id={ id (value )} " ,
559- file = sys .stderr ,
560- flush = True ,
561- )
562- return DictProxy (value ,self ,key )
563- elif isinstance (value ,list ):
564- return ListProxy (value ,self ,key )
565- return value
521+ if self ._parent is None and key in self ._base :
522+ value = self ._base [key ]
523+ # Even root instances need proxies to track when values are mutated
524+ # This allows us to know when a key has been accessed/modified
525+ if isinstance (value ,dict )and not isinstance (value ,OptsDict ):
526+ return DictProxy (value ,self ,key )
527+ elif isinstance (value ,list ):
528+ return ListProxy (value ,self ,key )
529+ return value
566530
567531raise KeyError (key )
568532
@@ -891,50 +855,27 @@ def safe_opts_copy(opts: Any, name: str | None = None) -> OptsDict:
891855 from salt.utils.optsdict import safe_opts_copy
892856 opts = safe_opts_copy(opts, name="loader:states")
893857 """
894- global _dict_to_root
895-
896858if isinstance (opts ,OptsDict ):
897859# Find the root of this OptsDict tree
898860root = opts
899861while root ._parent is not None :
900862root = root ._parent
901863
902- import sys
903-
904- print (
905- f"[OPTSDICT] safe_opts_copy called with OptsDict (name={ opts ._name } ), found root={ root ._name } , creating child of root with name={ name } " ,
906- file = sys .stderr ,
907- flush = True ,
908- )
909-
910864# Always create children from the root to ensure all children are siblings
911865# This allows them to see each other's mutations through the root's _local
912866return OptsDict .from_parent (root ,name = name )
913- else :
914- # Converting from regular dict
915- # Check if we've already created a root for this specific dict object
916- opts_id = id (opts )
917- if opts_id in _dict_to_root :
918- # Reuse existing root - this ensures all OptsDict instances
919- # from the same source dict share the same base and see mutations
920- root = _dict_to_root [opts_id ]
921- import sys
922867
923- print (
924- f"[OPTSDICT] Reusing root for dict id={ opts_id } , has grains={ 'grains' in opts } , name={ name } " ,
925- file = sys .stderr ,
926- flush = True ,
927- )
928- return OptsDict .from_parent (root ,name = name )
929- else :
930- # First time seeing this dict - create a new root that wraps it
931- root = OptsDict .from_dict (opts ,name = name or f"root@{ opts_id } " )
932- _dict_to_root [opts_id ]= root
933- import sys
868+ # Converting from regular dict
869+ # Check if we've already created a root for this specific dict object
870+ global _dict_to_root # pylint: disable=global-variable-not-assigned
871+ opts_id = id (opts )
872+ if opts_id in _dict_to_root :
873+ # Reuse existing root - this ensures all OptsDict instances
874+ # from the same source dict share the same base and see mutations
875+ root = _dict_to_root [opts_id ]
876+ return OptsDict .from_parent (root ,name = name )
934877
935- print (
936- f"[OPTSDICT] Created NEW root for dict id={ opts_id } , has grains={ 'grains' in opts } , name={ name } " ,
937- file = sys .stderr ,
938- flush = True ,
939- )
940- return root
878+ # First time seeing this dict - create a new root that wraps it
879+ root = OptsDict .from_dict (opts ,name = name or f"root@{ opts_id } " )
880+ _dict_to_root [opts_id ]= root
881+ return root