4
4
5
5
from __future__ import annotations
6
6
7
- from collections .abc import Iterable
8
- import string
9
7
from types import MappingProxyType
10
- from typing import Any , BinaryIO , NamedTuple
11
- import warnings
12
8
13
9
from ._re import (
14
10
RE_DATETIME ,
18
14
match_to_localtime ,
19
15
match_to_number ,
20
16
)
21
- from ._types import Key , ParseFloat , Pos
17
+
18
+ TYPE_CHECKING = False
19
+ if TYPE_CHECKING :
20
+ from collections .abc import Iterable
21
+ from typing import IO , Any
22
+
23
+ from ._types import Key , ParseFloat , Pos
22
24
23
25
ASCII_CTRL = frozenset (chr (i ) for i in range (32 )) | frozenset (chr (127 ))
24
26
34
36
35
37
TOML_WS = frozenset (" \t " )
36
38
TOML_WS_AND_NEWLINE = TOML_WS | frozenset ("\n " )
37
- BARE_KEY_CHARS = frozenset (string .ascii_letters + string .digits + "-_" )
39
+ BARE_KEY_CHARS = frozenset (
40
+ "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_"
41
+ )
38
42
KEY_INITIAL_CHARS = BARE_KEY_CHARS | frozenset ("\" '" )
39
- HEXDIGIT_CHARS = frozenset (string . hexdigits )
43
+ HEXDIGIT_CHARS = frozenset ("abcdef" "ABCDEF" "0123456789" )
40
44
41
45
BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType (
42
46
{
@@ -80,6 +84,8 @@ def __init__(
80
84
or not isinstance (doc , str )
81
85
or not isinstance (pos , int )
82
86
):
87
+ import warnings
88
+
83
89
warnings .warn (
84
90
"Free-form arguments for TOMLDecodeError are deprecated. "
85
91
"Please set 'msg' (str), 'doc' (str) and 'pos' (int) arguments only." ,
@@ -115,7 +121,7 @@ def __init__(
115
121
self .colno = colno
116
122
117
123
118
- def load (fp : BinaryIO , / , * , parse_float : ParseFloat = float ) -> dict [str , Any ]:
124
+ def load (fp : IO [ bytes ] , / , * , parse_float : ParseFloat = float ) -> dict [str , Any ]:
119
125
"""Parse TOML from a binary file object."""
120
126
b = fp .read ()
121
127
try :
@@ -139,7 +145,7 @@ def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # n
139
145
f"Expected str object, not '{ type (s ).__qualname__ } '"
140
146
) from None
141
147
pos = 0
142
- out = Output (NestedDict (), Flags () )
148
+ out = Output ()
143
149
header : Key = ()
144
150
parse_float = make_safe_parse_float (parse_float )
145
151
@@ -290,9 +296,10 @@ def append_nest_to_list(self, key: Key) -> None:
290
296
cont [last_key ] = [{}]
291
297
292
298
293
- class Output (NamedTuple ):
294
- data : NestedDict
295
- flags : Flags
299
+ class Output :
300
+ def __init__ (self ) -> None :
301
+ self .data = NestedDict ()
302
+ self .flags = Flags ()
296
303
297
304
298
305
def skip_chars (src : str , pos : Pos , chars : Iterable [str ]) -> Pos :
0 commit comments