44
55from __future__ import annotations
66
7- from collections .abc import Iterable
8- import string
97import sys
108from types import MappingProxyType
11- from typing import IO , Any , Final , NamedTuple
12- import warnings
139
1410from ._re import (
1511 RE_DATETIME ,
1915 match_to_localtime ,
2016 match_to_number ,
2117)
22- from ._types import Key , ParseFloat , Pos
18+
19+ TYPE_CHECKING = False
20+ if TYPE_CHECKING :
21+ from collections .abc import Iterable
22+ from typing import IO , Any , Final
23+
24+ from ._types import Key , ParseFloat , Pos
2325
2426# Inline tables/arrays are implemented using recursion. Pathologically
2527# nested documents cause pure Python to raise RecursionError (which is OK),
4648
4749TOML_WS : Final = frozenset (" \t " )
4850TOML_WS_AND_NEWLINE : Final = TOML_WS | frozenset ("\n " )
49- BARE_KEY_CHARS : Final = frozenset (string .ascii_letters + string .digits + "-_" )
51+ BARE_KEY_CHARS : Final = frozenset (
52+ "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_"
53+ )
5054KEY_INITIAL_CHARS : Final = BARE_KEY_CHARS | frozenset ("\" '" )
51- HEXDIGIT_CHARS : Final = frozenset (string . hexdigits )
55+ HEXDIGIT_CHARS : Final = frozenset ("abcdef" "ABCDEF" "0123456789" )
5256
5357BASIC_STR_ESCAPE_REPLACEMENTS : Final = MappingProxyType (
5458 {
5559 "\\ b" : "\u0008 " , # backspace
5660 "\\ t" : "\u0009 " , # tab
57- "\\ n" : "\u000A " , # linefeed
58- "\\ f" : "\u000C " , # form feed
59- "\\ r" : "\u000D " , # carriage return
61+ "\\ n" : "\u000a " , # linefeed
62+ "\\ f" : "\u000c " , # form feed
63+ "\\ r" : "\u000d " , # carriage return
6064 '\\ "' : "\u0022 " , # quote
61- "\\ \\ " : "\u005C " , # backslash
65+ "\\ \\ " : "\u005c " , # backslash
6266 }
6367)
6468
@@ -92,6 +96,8 @@ def __init__(
9296 or not isinstance (doc , str )
9397 or not isinstance (pos , int )
9498 ):
99+ import warnings
100+
95101 warnings .warn (
96102 "Free-form arguments for TOMLDecodeError are deprecated. "
97103 "Please set 'msg' (str), 'doc' (str) and 'pos' (int) arguments only." ,
@@ -151,7 +157,7 @@ def loads(__s: str, *, parse_float: ParseFloat = float) -> dict[str, Any]: # no
151157 f"Expected str object, not '{ type (__s ).__qualname__ } '"
152158 ) from None
153159 pos = 0
154- out = Output (NestedDict (), Flags () )
160+ out = Output ()
155161 header : Key = ()
156162 parse_float = make_safe_parse_float (parse_float )
157163
@@ -220,7 +226,7 @@ class Flags:
220226 EXPLICIT_NEST : Final = 1
221227
222228 def __init__ (self ) -> None :
223- self ._flags : dict [str , dict ] = {}
229+ self ._flags : dict [str , dict [ Any , Any ] ] = {}
224230 self ._pending_flags : set [tuple [Key , int ]] = set ()
225231
226232 def add_pending (self , key : Key , flag : int ) -> None :
@@ -278,7 +284,7 @@ def get_or_create_nest(
278284 key : Key ,
279285 * ,
280286 access_lists : bool = True ,
281- ) -> dict :
287+ ) -> dict [ str , Any ] :
282288 cont : Any = self .dict
283289 for k in key :
284290 if k not in cont :
@@ -288,7 +294,7 @@ def get_or_create_nest(
288294 cont = cont [- 1 ]
289295 if not isinstance (cont , dict ):
290296 raise KeyError ("There is no nest behind this key" )
291- return cont
297+ return cont # type: ignore[no-any-return]
292298
293299 def append_nest_to_list (self , key : Key ) -> None :
294300 cont = self .get_or_create_nest (key [:- 1 ])
@@ -302,9 +308,10 @@ def append_nest_to_list(self, key: Key) -> None:
302308 cont [last_key ] = [{}]
303309
304310
305- class Output (NamedTuple ):
306- data : NestedDict
307- flags : Flags
311+ class Output :
312+ def __init__ (self ) -> None :
313+ self .data = NestedDict ()
314+ self .flags = Flags ()
308315
309316
310317def skip_chars (src : str , pos : Pos , chars : Iterable [str ]) -> Pos :
@@ -493,9 +500,9 @@ def parse_one_line_basic_str(src: str, pos: Pos) -> tuple[Pos, str]:
493500
494501def parse_array (
495502 src : str , pos : Pos , parse_float : ParseFloat , nest_lvl : int
496- ) -> tuple [Pos , list ]:
503+ ) -> tuple [Pos , list [ Any ] ]:
497504 pos += 1
498- array : list = []
505+ array : list [ Any ] = []
499506
500507 pos = skip_comments_and_array_ws (src , pos )
501508 if src .startswith ("]" , pos ):
@@ -519,7 +526,7 @@ def parse_array(
519526
520527def parse_inline_table (
521528 src : str , pos : Pos , parse_float : ParseFloat , nest_lvl : int
522- ) -> tuple [Pos , dict ]:
529+ ) -> tuple [Pos , dict [ str , Any ] ]:
523530 pos += 1
524531 nested_dict = NestedDict ()
525532 flags = Flags ()
0 commit comments