@@ -147,7 +147,11 @@ def __getitem__(self, item: Sequence[Key]) -> object:
147147 raise KeyError (msg )
148148
149149 def set_value (
150- self , * , keys : Sequence [Key ], value : Any , exists_ok : bool = False
150+ self ,
151+ * ,
152+ keys : Sequence [Key ],
153+ value : object ,
154+ exists_ok : bool = False ,
151155 ) -> None :
152156 """Set a value in the INI file.
153157
@@ -156,19 +160,24 @@ def set_value(
156160 root = self .get ()
157161
158162 if len (keys ) == 0 :
163+ value = TypeAdapter (dict [str , dict [str , str | list [str ]]]).validate_python (
164+ value
165+ )
159166 self ._set_value_in_root (root = root , value = value , exists_ok = exists_ok )
160167 elif len (keys ) == 1 :
161168 (section_key ,) = keys
169+ value = TypeAdapter (dict [str , str | list [str ]]).validate_python (value )
162170 self ._set_value_in_section (
163171 root = root , section_key = keys [0 ], value = value , exists_ok = exists_ok
164172 )
165173 elif len (keys ) == 2 :
166174 (section_key , option_key ) = keys
175+ cast_value = TypeAdapter (str | list [str ]).validate_python (value )
167176 self ._set_value_in_option (
168177 root = root ,
169178 section_key = section_key ,
170179 option_key = option_key ,
171- value = value ,
180+ value = cast_value ,
172181 exists_ok = exists_ok ,
173182 )
174183 else :
@@ -271,7 +280,7 @@ def _set_value_in_option(
271280 root : INIDocument ,
272281 section_key : Key ,
273282 option_key : Key ,
274- value : str ,
283+ value : str | list [ str ] ,
275284 exists_ok : bool ,
276285 ) -> None :
277286 if not isinstance (section_key , str ) or not isinstance (option_key , str ):
@@ -408,7 +417,7 @@ def _delete_strkeys(self, strkeys: Sequence[str]) -> None:
408417
409418 self .commit (root )
410419
411- def extend_list (self , * , keys : Sequence [Key ], values : list [ str ]) -> None :
420+ def extend_list (self , * , keys : Sequence [Key ], values : Sequence [ object ]) -> None :
412421 """Extend a list in the INI file.
413422
414423 An empty list of keys corresponds to the root of the document.
@@ -429,6 +438,7 @@ def extend_list(self, *, keys: Sequence[Key], values: list[str]) -> None:
429438 raise InvalidINITypeError (msg )
430439 elif len (keys ) == 2 :
431440 section_key , option_key = keys
441+ values = TypeAdapter (list [str ]).validate_python (values )
432442 self ._extend_list_in_option (
433443 root = root , section_key = section_key , option_key = option_key , values = values
434444 )
@@ -443,7 +453,7 @@ def extend_list(self, *, keys: Sequence[Key], values: list[str]) -> None:
443453
444454 @staticmethod
445455 def _extend_list_in_option (
446- * , root : INIDocument , section_key : Key , option_key : Key , values : list [str ]
456+ * , root : INIDocument , section_key : Key , option_key : Key , values : Sequence [str ]
447457 ) -> None :
448458 for value in values :
449459 INIFileManager ._validated_append (
@@ -452,7 +462,7 @@ def _extend_list_in_option(
452462
453463 @staticmethod
454464 def _remove_from_list_in_option (
455- * , root : INIDocument , section_key : Key , option_key : Key , values : list [str ]
465+ * , root : INIDocument , section_key : Key , option_key : Key , values : Sequence [str ]
456466 ) -> None :
457467 if section_key not in root :
458468 return
@@ -485,7 +495,9 @@ def _remove_from_list_in_option(
485495 elif len (new_values ) > 1 :
486496 root [section_key ][option_key ].set_values (new_values )
487497
488- def remove_from_list (self , * , keys : Sequence [Key ], values : list [str ]) -> None :
498+ def remove_from_list (
499+ self , * , keys : Sequence [Key ], values : Sequence [object ]
500+ ) -> None :
489501 """Remove values from a list in the INI file.
490502
491503 An empty list of keys corresponds to the root of the document.
@@ -506,6 +518,7 @@ def remove_from_list(self, *, keys: Sequence[Key], values: list[str]) -> None:
506518 raise InvalidINITypeError (msg )
507519 elif len (keys ) == 2 :
508520 section_key , option_key = keys
521+ values = TypeAdapter (list [str ]).validate_python (values )
509522 self ._remove_from_list_in_option (
510523 root = root , section_key = section_key , option_key = option_key , values = values
511524 )
0 commit comments