@@ -167,9 +167,9 @@ def set_value(
167167 except KeyError :
168168 _set_value_in_existing (
169169 toml_document = toml_document ,
170- current_container = d ,
170+ shared_container = d ,
171+ shared_keys = shared_keys ,
171172 keys = keys ,
172- current_keys = shared_keys ,
173173 value = value ,
174174 )
175175 except ValidationError :
@@ -179,9 +179,9 @@ def set_value(
179179 else :
180180 _set_value_in_existing (
181181 toml_document = toml_document ,
182- current_container = d ,
182+ shared_keys = shared_keys ,
183+ shared_container = d ,
183184 keys = keys ,
184- current_keys = shared_keys ,
185185 value = value ,
186186 )
187187 else :
@@ -269,12 +269,14 @@ def extend_list(self, *, keys: Sequence[Key], values: list[Any]) -> None:
269269
270270 toml_document = copy .copy (self .get ())
271271
272+ shared_keys : list [str ] = []
273+ d = toml_document
272274 try :
273- d = toml_document
274275 for key in keys [:- 1 ]:
275276 TypeAdapter (dict ).validate_python (d )
276277 assert isinstance (d , dict )
277278 d = d [key ]
279+ shared_keys .append (key )
278280 p_parent = d
279281 TypeAdapter (dict ).validate_python (p_parent )
280282 assert isinstance (p_parent , dict )
@@ -284,7 +286,13 @@ def extend_list(self, *, keys: Sequence[Key], values: list[Any]) -> None:
284286 for key in reversed (keys ):
285287 contents = {key : contents }
286288 assert isinstance (contents , dict )
287- toml_document = mergedeep .merge (toml_document , contents )
289+ _set_value_in_existing (
290+ toml_document = toml_document ,
291+ shared_keys = shared_keys ,
292+ shared_container = d ,
293+ keys = keys ,
294+ value = values ,
295+ )
288296 assert isinstance (toml_document , TOMLDocument )
289297 except ValidationError :
290298 msg = (
@@ -349,11 +357,20 @@ def remove_from_list(self, *, keys: Sequence[Key], values: Collection[Any]) -> N
349357def _set_value_in_existing (
350358 * ,
351359 toml_document : TOMLDocument ,
352- current_container : TOMLDocument | Item | Container ,
360+ shared_keys : Sequence [Key ],
361+ shared_container : TOMLDocument | Item | Container ,
353362 keys : Sequence [Key ],
354- current_keys : Sequence [Key ],
355363 value : Any ,
356364) -> None :
365+ """Set a new value in an existing container.
366+
367+ Args:
368+ toml_document: The overall document.
369+ shared_keys: Keys to the deepest container that actually exists.
370+ shared_container: The shared container itself that needs new contents.
371+ keys: Keys to the new value from the root of the document.
372+ value: The value at the keys.
373+ """
357374 # The old configuration should be kept for all ID keys except the
358375 # final/deepest one which shouldn't exist anyway since we checked as much,
359376 # above. For example, if there is [tool.ruff] then we shouldn't overwrite it
@@ -369,12 +386,12 @@ def _set_value_in_existing(
369386 else :
370387 # Note that this alternative logic is just to avoid a bug:
371388 # https://github.com/usethis-python/usethis-python/issues/507
372- TypeAdapter (dict ).validate_python (current_container )
373- assert isinstance (current_container , dict )
389+ TypeAdapter (dict ).validate_python (shared_container )
390+ assert isinstance (shared_container , dict )
374391
375- unshared_keys = keys [len (current_keys ) :]
392+ unshared_keys = keys [len (shared_keys ) :]
376393
377- if len (current_keys ) == 1 :
394+ if len (shared_keys ) == 1 :
378395 # In this case, we need to "seed" the section to avoid another bug:
379396 # https://github.com/usethis-python/usethis-python/issues/558
380397
@@ -385,9 +402,9 @@ def _set_value_in_existing(
385402 for key in reversed (unshared_keys [1 :]):
386403 contents = {key : contents }
387404
388- current_container [keys [1 ]] = contents # type: ignore[reportAssignmentType]
405+ shared_container [keys [1 ]] = contents # type: ignore[reportAssignmentType]
389406 else :
390- current_container [_get_unified_key (unshared_keys )] = value
407+ shared_container [_get_unified_key (unshared_keys )] = value
391408
392409
393410def _validate_keys (keys : Sequence [Key ]) -> list [str ]:
0 commit comments