@@ -114,7 +114,7 @@ def __init__(self, isolated: bool, load_only: Optional[Kind] = None) -> None:
114114 self ._parsers : Dict [Kind , List [Tuple [str , RawConfigParser ]]] = {
115115 variant : [] for variant in OVERRIDE_ORDER
116116 }
117- self ._config : Dict [Kind , Dict [str , Any ]] = {
117+ self ._config : Dict [Kind , Dict [str , Dict [ str , Any ] ]] = {
118118 variant : {} for variant in OVERRIDE_ORDER
119119 }
120120 self ._modified_parsers : List [Tuple [str , RawConfigParser ]] = []
@@ -145,7 +145,14 @@ def get_value(self, key: str) -> Any:
145145 orig_key = key
146146 key = _normalize_name (key )
147147 try :
148- return self ._dictionary [key ]
148+ clean_config = {}
149+ for k , value in self ._dictionary .items ():
150+ if isinstance (value , dict ):
151+ for k , v in value .items ():
152+ clean_config [k ] = v
153+ else :
154+ clean_config [k ] = value
155+ return clean_config [key ]
149156 except KeyError :
150157 # disassembling triggers a more useful error message than simply
151158 # "No such key" in the case that the key isn't in the form command.option
@@ -168,7 +175,11 @@ def set_value(self, key: str, value: Any) -> None:
168175 parser .add_section (section )
169176 parser .set (section , name , value )
170177
171- self ._config [self .load_only ][key ] = value
178+ exists = self ._config [self .load_only ].get (fname )
179+ if not exists :
180+ self ._config [self .load_only ][fname ] = {}
181+
182+ self ._config [self .load_only ][fname ][key ] = value
172183 self ._mark_as_modified (fname , parser )
173184
174185 def unset_value (self , key : str ) -> None :
@@ -178,11 +189,14 @@ def unset_value(self, key: str) -> None:
178189 self ._ensure_have_load_only ()
179190
180191 assert self .load_only
181- if key not in self ._config [self .load_only ]:
182- raise ConfigurationError (f"No such key - { orig_key } " )
183-
184192 fname , parser = self ._get_parser_to_modify ()
185193
194+ if (
195+ key not in self ._config [self .load_only ][fname ]
196+ and key not in self ._config [self .load_only ]
197+ ):
198+ raise ConfigurationError (f"No such key - { orig_key } " )
199+
186200 if parser is not None :
187201 section , name = _disassemble_key (key )
188202 if not (
@@ -197,8 +211,10 @@ def unset_value(self, key: str) -> None:
197211 if not parser .items (section ):
198212 parser .remove_section (section )
199213 self ._mark_as_modified (fname , parser )
200-
201- del self ._config [self .load_only ][key ]
214+ try :
215+ del self ._config [self .load_only ][fname ][key ]
216+ except KeyError :
217+ del self ._config [self .load_only ][key ]
202218
203219 def save (self ) -> None :
204220 """Save the current in-memory state."""
@@ -270,7 +286,10 @@ def _load_file(self, variant: Kind, fname: str) -> RawConfigParser:
270286
271287 for section in parser .sections ():
272288 items = parser .items (section )
273- self ._config [variant ].update (self ._normalized_keys (section , items ))
289+ exists = self ._config [variant ].get (fname )
290+ if not exists :
291+ self ._config [variant ][fname ] = {}
292+ self ._config [variant ][fname ].update (self ._normalized_keys (section , items ))
274293
275294 return parser
276295
0 commit comments