66from datetime import datetime
77from datetime import time
88from types import MappingProxyType
9+ from typing import Any
10+ from typing import Callable
11+ from typing import Type
912
1013import pytest
1114
3841from tomlkit .toml_document import TOMLDocument
3942
4043
41- def json_serial (obj ) :
44+ def json_serial (obj : Any ) -> str :
4245 """JSON serializer for objects not serializable by default json code"""
4346 if isinstance (obj , (datetime , date , time )):
4447 return obj .isoformat ()
@@ -62,7 +65,7 @@ def json_serial(obj):
6265 "table_names" ,
6366 ],
6467)
65- def test_parse_can_parse_valid_toml_files (example , example_name ) :
68+ def test_parse_can_parse_valid_toml_files (example : Callable [[ str ], str ], example_name : str ) -> None :
6669 assert isinstance (parse (example (example_name )), TOMLDocument )
6770 assert isinstance (loads (example (example_name )), TOMLDocument )
6871
@@ -83,7 +86,7 @@ def test_parse_can_parse_valid_toml_files(example, example_name):
8386 "table_names" ,
8487 ],
8588)
86- def test_load_from_file_object (example_name ) :
89+ def test_load_from_file_object (example_name : str ) -> None :
8790 with open (
8891 os .path .join (os .path .dirname (__file__ ), "examples" , example_name + ".toml" ),
8992 encoding = "utf-8" ,
@@ -93,8 +96,8 @@ def test_load_from_file_object(example_name):
9396
9497@pytest .mark .parametrize ("example_name" , ["0.5.0" , "pyproject" , "table_names" ])
9598def test_parsed_document_are_properly_json_representable (
96- example , json_example , example_name
97- ):
99+ example : Callable [[ str ], str ], json_example : Callable [[ str ], str ], example_name : str
100+ ) -> None :
98101 doc = json .loads (json .dumps (parse (example (example_name )), default = json_serial ))
99102 json_doc = json .loads (json_example (example_name ))
100103
@@ -123,8 +126,8 @@ def test_parsed_document_are_properly_json_representable(
123126 ],
124127)
125128def test_parse_raises_errors_for_invalid_toml_files (
126- invalid_example , error , example_name
127- ):
129+ invalid_example : Callable [[ str ], str ], error : Type [ Exception ] , example_name : str
130+ ) -> None :
128131 with pytest .raises (error ):
129132 parse (invalid_example (example_name ))
130133
@@ -142,69 +145,69 @@ def test_parse_raises_errors_for_invalid_toml_files(
142145 "table_names" ,
143146 ],
144147)
145- def test_original_string_and_dumped_string_are_equal (example , example_name ) :
148+ def test_original_string_and_dumped_string_are_equal (example : Callable [[ str ], str ], example_name : str ) -> None :
146149 content = example (example_name )
147150 parsed = parse (content )
148151
149152 assert content == dumps (parsed )
150153
151154
152- def test_a_raw_dict_can_be_dumped ():
155+ def test_a_raw_dict_can_be_dumped () -> None :
153156 s = dumps ({"foo" : "bar" })
154157
155158 assert s == 'foo = "bar"\n '
156159
157160
158- def test_mapping_types_can_be_dumped ():
161+ def test_mapping_types_can_be_dumped () -> None :
159162 x = MappingProxyType ({"foo" : "bar" })
160163 assert dumps (x ) == 'foo = "bar"\n '
161164
162165
163- def test_dumps_weird_object ():
166+ def test_dumps_weird_object () -> None :
164167 with pytest .raises (TypeError ):
165- dumps (object ())
168+ dumps (object ()) # type: ignore[arg-type]
166169
167170
168- def test_dump_tuple_value_as_array ():
169- x = {"foo" : (1 , 2 )}
171+ def test_dump_tuple_value_as_array () -> None :
172+ x : dict [ str , Any ] = {"foo" : (1 , 2 )}
170173 assert dumps (x ) == "foo = [1, 2]\n "
171174
172175 x = {"foo" : ({"a" : 1 }, {"a" : 2 })}
173176 assert dumps (x ) == "[[foo]]\n a = 1\n \n [[foo]]\n a = 2\n "
174177
175178
176- def test_dump_to_file_object ():
179+ def test_dump_to_file_object () -> None :
177180 doc = {"foo" : "bar" }
178181 fp = io .StringIO ()
179182 dump (doc , fp )
180183 assert fp .getvalue () == 'foo = "bar"\n '
181184
182185
183- def test_dump_nested_dotted_table ():
184- a = tomlkit .parse ("a.b.c.d='e'" )["a" ]
186+ def test_dump_nested_dotted_table () -> None :
187+ a : Any = tomlkit .parse ("a.b.c.d='e'" )["a" ]
185188 assert a == {"b" : {"c" : {"d" : "e" }}}
186189 assert dumps (a ) == "b.c.d='e'"
187190
188191
189- def test_integer ():
192+ def test_integer () -> None :
190193 i = tomlkit .integer ("34" )
191194
192195 assert isinstance (i , Integer )
193196
194197
195- def test_float ():
198+ def test_float () -> None :
196199 i = tomlkit .float_ ("34.56" )
197200
198201 assert isinstance (i , Float )
199202
200203
201- def test_boolean ():
204+ def test_boolean () -> None :
202205 i = tomlkit .boolean ("true" )
203206
204207 assert isinstance (i , Bool )
205208
206209
207- def test_date ():
210+ def test_date () -> None :
208211 dt = tomlkit .date ("1979-05-13" )
209212
210213 assert isinstance (dt , Date )
@@ -213,7 +216,7 @@ def test_date():
213216 tomlkit .date ("12:34:56" )
214217
215218
216- def test_time ():
219+ def test_time () -> None :
217220 dt = tomlkit .time ("12:34:56" )
218221
219222 assert isinstance (dt , Time )
@@ -222,7 +225,7 @@ def test_time():
222225 tomlkit .time ("1979-05-13" )
223226
224227
225- def test_datetime ():
228+ def test_datetime () -> None :
226229 dt = tomlkit .datetime ("1979-05-13T12:34:56" )
227230
228231 assert isinstance (dt , DateTime )
@@ -231,7 +234,7 @@ def test_datetime():
231234 tomlkit .time ("1979-05-13" )
232235
233236
234- def test_array ():
237+ def test_array () -> None :
235238 a = tomlkit .array ()
236239
237240 assert isinstance (a , Array )
@@ -241,45 +244,45 @@ def test_array():
241244 assert isinstance (a , Array )
242245
243246
244- def test_table ():
247+ def test_table () -> None :
245248 t = tomlkit .table ()
246249
247250 assert isinstance (t , Table )
248251
249252
250- def test_inline_table ():
253+ def test_inline_table () -> None :
251254 t = tomlkit .inline_table ()
252255
253256 assert isinstance (t , InlineTable )
254257
255258
256- def test_aot ():
259+ def test_aot () -> None :
257260 t = tomlkit .aot ()
258261
259262 assert isinstance (t , AoT )
260263
261264
262- def test_key ():
265+ def test_key () -> None :
263266 k = tomlkit .key ("foo" )
264267
265268 assert isinstance (k , Key )
266269
267270
268- def test_key_value ():
271+ def test_key_value () -> None :
269272 k , i = tomlkit .key_value ("foo = 12" )
270273
271274 assert isinstance (k , Key )
272275 assert isinstance (i , Integer )
273276
274277
275- def test_string ():
278+ def test_string () -> None :
276279 s = tomlkit .string ('foo "' )
277280
278281 assert s .value == 'foo "'
279282 assert s .as_string () == '"foo \\ ""'
280283
281284
282- def test_item_dict_to_table ():
285+ def test_item_dict_to_table () -> None :
283286 t = tomlkit .item ({"foo" : {"bar" : "baz" }})
284287
285288 assert t .value == {"foo" : {"bar" : "baz" }}
@@ -291,25 +294,25 @@ def test_item_dict_to_table():
291294 )
292295
293296
294- def test_item_mixed_aray ():
297+ def test_item_mixed_aray () -> None :
295298 example = [{"a" : 3 }, "b" , 42 ]
296299 expected = '[{a = 3}, "b", 42]'
297300 t = tomlkit .item (example )
298301 assert t .as_string ().strip () == expected
299302 assert dumps ({"x" : {"y" : example }}).strip () == "[x]\n y = " + expected
300303
301304
302- def test_build_super_table ():
305+ def test_build_super_table () -> None :
303306 doc = tomlkit .document ()
304307 table = tomlkit .table (True )
305308 table .add ("bar" , {"x" : 1 })
306309 doc .add ("foo" , table )
307310 assert doc .as_string () == "[foo.bar]\n x = 1\n "
308311
309312
310- def test_add_dotted_key ():
313+ def test_add_dotted_key () -> None :
311314 doc = tomlkit .document ()
312- doc .add (tomlkit .key (["foo" , "bar" ]), 1 )
315+ doc .add (tomlkit .key (["foo" , "bar" ]), 1 ) # type: ignore[arg-type]
313316 assert doc .as_string () == "foo.bar = 1\n "
314317
315318 table = tomlkit .table ()
@@ -324,15 +327,15 @@ def test_add_dotted_key():
324327 ("false" , False ),
325328 ],
326329)
327- def test_value_parses_boolean (raw , expected ) :
330+ def test_value_parses_boolean (raw : str , expected : bool ) -> None :
328331 parsed = tomlkit .value (raw )
329- assert parsed == expected
332+ assert parsed == expected # type: ignore[comparison-overlap]
330333
331334
332335@pytest .mark .parametrize (
333336 "raw" , ["t" , "f" , "tru" , "fals" , "test" , "friend" , "truthy" , "falsify" ]
334337)
335- def test_value_rejects_values_looking_like_bool_at_start (raw ) :
338+ def test_value_rejects_values_looking_like_bool_at_start (raw : str ) -> None :
336339 """Reproduces https://github.com/sdispater/tomlkit/issues/165"""
337340 with pytest .raises (tomlkit .exceptions .ParseError ):
338341 tomlkit .value (raw )
@@ -347,7 +350,7 @@ def test_value_rejects_values_looking_like_bool_at_start(raw):
347350 "true_hip_hop" ,
348351 ],
349352)
350- def test_value_rejects_values_having_true_prefix (raw ) :
353+ def test_value_rejects_values_having_true_prefix (raw : str ) -> None :
351354 """Values that have ``true`` or ``false`` as prefix but then have additional chars are rejected."""
352355 with pytest .raises (tomlkit .exceptions .ParseError ):
353356 tomlkit .value (raw )
@@ -362,7 +365,7 @@ def test_value_rejects_values_having_true_prefix(raw):
362365 "false_prophet" ,
363366 ],
364367)
365- def test_value_rejects_values_having_false_prefix (raw ) :
368+ def test_value_rejects_values_having_false_prefix (raw : str ) -> None :
366369 """Values that have ``true`` or ``false`` as prefix but then have additional chars are rejected."""
367370 with pytest .raises (tomlkit .exceptions .ParseError ):
368371 tomlkit .value (raw )
@@ -384,18 +387,18 @@ def test_value_rejects_values_having_false_prefix(raw):
384387 "false{a=1}" ,
385388 ],
386389)
387- def test_value_rejects_values_with_appendage (raw ) :
390+ def test_value_rejects_values_with_appendage (raw : str ) -> None :
388391 """Values that appear valid at the beginning but leave chars unparsed are rejected."""
389392 with pytest .raises (tomlkit .exceptions .ParseError ):
390393 tomlkit .value (raw )
391394
392395
393- def test_create_super_table_with_table ():
396+ def test_create_super_table_with_table () -> None :
394397 data = {"foo" : {"bar" : {"a" : 1 }}}
395398 assert dumps (data ) == "[foo.bar]\n a = 1\n "
396399
397400
398- def test_create_super_table_with_aot ():
401+ def test_create_super_table_with_aot () -> None :
399402 data = {"foo" : {"bar" : [{"a" : 1 }]}}
400403 assert dumps (data ) == "[[foo.bar]]\n a = 1\n "
401404
@@ -442,7 +445,7 @@ def test_create_super_table_with_aot():
442445 ),
443446 ],
444447)
445- def test_create_string (kwargs , example , expected ) :
448+ def test_create_string (kwargs : dict [ str , Any ], example : str , expected : str ) -> None :
446449 value = tomlkit .string (example , ** kwargs )
447450 assert value .as_string () == expected
448451
@@ -460,12 +463,12 @@ def test_create_string(kwargs, example, expected):
460463 ({"multiline" : True , "literal" : True }, "My'''String" ),
461464 ],
462465)
463- def test_create_string_with_invalid_characters (kwargs , example ) :
466+ def test_create_string_with_invalid_characters (kwargs : dict [ str , Any ], example : str ) -> None :
464467 with pytest .raises (InvalidStringError ):
465468 tomlkit .string (example , ** kwargs )
466469
467470
468- def test_parse_empty_quoted_table_name ():
471+ def test_parse_empty_quoted_table_name () -> None :
469472 content = "['']\n x = 1\n "
470473 parsed = loads (content )
471474 assert parsed == {"" : {"x" : 1 }}
0 commit comments