22# SPDX-FileCopyrightText: 2021 Taneli Hukkinen
33# Licensed to PSF under a Contributor Agreement.
44
5- from __future__ import annotations
6-
75import sys
86from types import MappingProxyType
97
1614 match_to_number ,
1715)
1816
19- TYPE_CHECKING = False
20- if TYPE_CHECKING :
21- from collections .abc import Iterable
22- from typing import IO , Any , Final
17+ from typing import IO , Any , Dict , Tuple , Iterable , List
2318
24- from ._types import Key , ParseFloat , Pos
19+ from ._types import Key , ParseFloat , Pos
2520
2621# Inline tables/arrays are implemented using recursion. Pathologically
2722# nested documents cause pure Python to raise RecursionError (which is OK),
3227# Choosing `sys.getrecursionlimit()` as maximum inline table/array nesting
3328# level, as it allows more nesting than pure Python, but still seems a far
3429# lower number than where mypyc binaries crash.
35- MAX_INLINE_NESTING : Final = sys .getrecursionlimit ()
30+ MAX_INLINE_NESTING = sys .getrecursionlimit ()
3631
37- ASCII_CTRL : Final = frozenset (chr (i ) for i in range (32 )) | frozenset (chr (127 ))
32+ ASCII_CTRL = frozenset (chr (i ) for i in range (32 )) | frozenset (chr (127 ))
3833
3934# Neither of these sets include quotation mark or backslash. They are
4035# currently handled as separate cases in the parser functions.
41- ILLEGAL_BASIC_STR_CHARS : Final = ASCII_CTRL - frozenset ("\t " )
42- ILLEGAL_MULTILINE_BASIC_STR_CHARS : Final = ASCII_CTRL - frozenset ("\t \n " )
36+ ILLEGAL_BASIC_STR_CHARS = ASCII_CTRL - frozenset ("\t " )
37+ ILLEGAL_MULTILINE_BASIC_STR_CHARS = ASCII_CTRL - frozenset ("\t \n " )
4338
44- ILLEGAL_LITERAL_STR_CHARS : Final = ILLEGAL_BASIC_STR_CHARS
45- ILLEGAL_MULTILINE_LITERAL_STR_CHARS : Final = ILLEGAL_MULTILINE_BASIC_STR_CHARS
39+ ILLEGAL_LITERAL_STR_CHARS = ILLEGAL_BASIC_STR_CHARS
40+ ILLEGAL_MULTILINE_LITERAL_STR_CHARS = ILLEGAL_MULTILINE_BASIC_STR_CHARS
4641
47- ILLEGAL_COMMENT_CHARS : Final = ILLEGAL_BASIC_STR_CHARS
42+ ILLEGAL_COMMENT_CHARS = ILLEGAL_BASIC_STR_CHARS
4843
49- TOML_WS : Final = frozenset (" \t " )
50- TOML_WS_AND_NEWLINE : Final = TOML_WS | frozenset ("\n " )
51- BARE_KEY_CHARS : Final = frozenset (
44+ TOML_WS = frozenset (" \t " )
45+ TOML_WS_AND_NEWLINE = TOML_WS | frozenset ("\n " )
46+ BARE_KEY_CHARS = frozenset (
5247 "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_"
5348)
54- KEY_INITIAL_CHARS : Final = BARE_KEY_CHARS | frozenset ("\" '" )
55- HEXDIGIT_CHARS : Final = frozenset ("abcdef" "ABCDEF" "0123456789" )
49+ KEY_INITIAL_CHARS = BARE_KEY_CHARS | frozenset ("\" '" )
50+ HEXDIGIT_CHARS = frozenset ("abcdef" "ABCDEF" "0123456789" )
5651
57- BASIC_STR_ESCAPE_REPLACEMENTS : Final = MappingProxyType (
52+ BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType (
5853 {
5954 "\\ b" : "\u0008 " , # backspace
6055 "\\ t" : "\u0009 " , # tab
@@ -85,9 +80,9 @@ class TOMLDecodeError(ValueError):
8580
8681 def __init__ (
8782 self ,
88- msg : str | type [ DEPRECATED_DEFAULT ] = DEPRECATED_DEFAULT ,
89- doc : str | type [ DEPRECATED_DEFAULT ] = DEPRECATED_DEFAULT ,
90- pos : Pos | type [ DEPRECATED_DEFAULT ] = DEPRECATED_DEFAULT ,
83+ msg : str = DEPRECATED_DEFAULT ,
84+ doc : str = DEPRECATED_DEFAULT ,
85+ pos : Pos = DEPRECATED_DEFAULT ,
9186 * args : Any ,
9287 ):
9388 if (
@@ -133,7 +128,7 @@ def __init__(
133128 self .colno = colno
134129
135130
136- def load (__fp : IO [bytes ], * , parse_float : ParseFloat = float ) -> dict [str , Any ]:
131+ def load (__fp : IO [bytes ], * , parse_float : ParseFloat = float ) -> Dict [str , Any ]:
137132 """Parse TOML from a binary file object."""
138133 b = __fp .read ()
139134 try :
@@ -145,7 +140,7 @@ def load(__fp: IO[bytes], *, parse_float: ParseFloat = float) -> dict[str, Any]:
145140 return loads (s , parse_float = parse_float )
146141
147142
148- def loads (__s : str , * , parse_float : ParseFloat = float ) -> dict [str , Any ]: # noqa: C901
143+ def loads (__s : str , * , parse_float : ParseFloat = float ) -> Dict [str , Any ]: # noqa: C901
149144 """Parse TOML from a string."""
150145
151146 # The spec allows converting "\r\n" to "\n", even in string
@@ -187,7 +182,7 @@ def loads(__s: str, *, parse_float: ParseFloat = float) -> dict[str, Any]: # no
187182 pos = skip_chars (src , pos , TOML_WS )
188183 elif char == "[" :
189184 try :
190- second_char : str | None = src [pos + 1 ]
185+ second_char = src [pos + 1 ]
191186 except IndexError :
192187 second_char = None
193188 out .flags .finalize_pending ()
@@ -220,14 +215,14 @@ class Flags:
220215 """Flags that map to parsed keys/namespaces."""
221216
222217 # Marks an immutable namespace (inline array or inline table).
223- FROZEN : Final = 0
218+ FROZEN = 0
224219 # Marks a nest that has been explicitly created and can no longer
225220 # be opened using the "[table]" syntax.
226- EXPLICIT_NEST : Final = 1
221+ EXPLICIT_NEST = 1
227222
228223 def __init__ (self ) -> None :
229- self ._flags : dict [str , dict [Any , Any ]] = {}
230- self ._pending_flags : set [tuple [Key , int ]] = set ()
224+ self ._flags : Dict [str , Dict [Any , Any ]] = {}
225+ self ._pending_flags : set [Tuple [Key , int ]] = set ()
231226
232227 def add_pending (self , key : Key , flag : int ) -> None :
233228 self ._pending_flags .add ((key , flag ))
@@ -277,14 +272,14 @@ def is_(self, key: Key, flag: int) -> bool:
277272class NestedDict :
278273 def __init__ (self ) -> None :
279274 # The parsed content of the TOML document
280- self .dict : dict [str , Any ] = {}
275+ self .dict : Dict [str , Any ] = {}
281276
282277 def get_or_create_nest (
283278 self ,
284279 key : Key ,
285280 * ,
286281 access_lists : bool = True ,
287- ) -> dict [str , Any ]:
282+ ) -> Dict [str , Any ]:
288283 cont : Any = self .dict
289284 for k in key :
290285 if k not in cont :
@@ -328,7 +323,7 @@ def skip_until(
328323 pos : Pos ,
329324 expect : str ,
330325 * ,
331- error_on : frozenset [ str ] ,
326+ error_on : frozenset ,
332327 error_on_eof : bool ,
333328) -> Pos :
334329 try :
@@ -347,7 +342,7 @@ def skip_until(
347342
348343def skip_comment (src : str , pos : Pos ) -> Pos :
349344 try :
350- char : str | None = src [pos ]
345+ char = src [pos ]
351346 except IndexError :
352347 char = None
353348 if char == "#" :
@@ -366,7 +361,7 @@ def skip_comments_and_array_ws(src: str, pos: Pos) -> Pos:
366361 return pos
367362
368363
369- def create_dict_rule (src : str , pos : Pos , out : Output ) -> tuple [Pos , Key ]:
364+ def create_dict_rule (src : str , pos : Pos , out : Output ) -> Tuple [Pos , Key ]:
370365 pos += 1 # Skip "["
371366 pos = skip_chars (src , pos , TOML_WS )
372367 pos , key = parse_key (src , pos )
@@ -386,7 +381,7 @@ def create_dict_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key]:
386381 return pos + 1 , key
387382
388383
389- def create_list_rule (src : str , pos : Pos , out : Output ) -> tuple [Pos , Key ]:
384+ def create_list_rule (src : str , pos : Pos , out : Output ) -> Tuple [Pos , Key ]:
390385 pos += 2 # Skip "[["
391386 pos = skip_chars (src , pos , TOML_WS )
392387 pos , key = parse_key (src , pos )
@@ -445,10 +440,10 @@ def key_value_rule(
445440
446441def parse_key_value_pair (
447442 src : str , pos : Pos , parse_float : ParseFloat , nest_lvl : int
448- ) -> tuple [Pos , Key , Any ]:
443+ ) -> Tuple [Pos , Key , Any ]:
449444 pos , key = parse_key (src , pos )
450445 try :
451- char : str | None = src [pos ]
446+ char = src [pos ]
452447 except IndexError :
453448 char = None
454449 if char != "=" :
@@ -459,13 +454,13 @@ def parse_key_value_pair(
459454 return pos , key , value
460455
461456
462- def parse_key (src : str , pos : Pos ) -> tuple [Pos , Key ]:
457+ def parse_key (src : str , pos : Pos ) -> Tuple [Pos , Key ]:
463458 pos , key_part = parse_key_part (src , pos )
464459 key : Key = (key_part ,)
465460 pos = skip_chars (src , pos , TOML_WS )
466461 while True :
467462 try :
468- char : str | None = src [pos ]
463+ char = src [pos ]
469464 except IndexError :
470465 char = None
471466 if char != "." :
@@ -477,9 +472,9 @@ def parse_key(src: str, pos: Pos) -> tuple[Pos, Key]:
477472 pos = skip_chars (src , pos , TOML_WS )
478473
479474
480- def parse_key_part (src : str , pos : Pos ) -> tuple [Pos , str ]:
475+ def parse_key_part (src : str , pos : Pos ) -> Tuple [Pos , str ]:
481476 try :
482- char : str | None = src [pos ]
477+ char = src [pos ]
483478 except IndexError :
484479 char = None
485480 if char in BARE_KEY_CHARS :
@@ -493,16 +488,16 @@ def parse_key_part(src: str, pos: Pos) -> tuple[Pos, str]:
493488 raise TOMLDecodeError ("Invalid initial character for a key part" , src , pos )
494489
495490
496- def parse_one_line_basic_str (src : str , pos : Pos ) -> tuple [Pos , str ]:
491+ def parse_one_line_basic_str (src : str , pos : Pos ) -> Tuple [Pos , str ]:
497492 pos += 1
498493 return parse_basic_str (src , pos , multiline = False )
499494
500495
501496def parse_array (
502497 src : str , pos : Pos , parse_float : ParseFloat , nest_lvl : int
503- ) -> tuple [Pos , list [Any ]]:
498+ ) -> Tuple [Pos , List [Any ]]:
504499 pos += 1
505- array : list [Any ] = []
500+ array : List [Any ] = []
506501
507502 pos = skip_comments_and_array_ws (src , pos )
508503 if src .startswith ("]" , pos ):
@@ -526,7 +521,7 @@ def parse_array(
526521
527522def parse_inline_table (
528523 src : str , pos : Pos , parse_float : ParseFloat , nest_lvl : int
529- ) -> tuple [Pos , dict [str , Any ]]:
524+ ) -> Tuple [Pos , Dict [str , Any ]]:
530525 pos += 1
531526 nested_dict = NestedDict ()
532527 flags = Flags ()
@@ -560,7 +555,7 @@ def parse_inline_table(
560555
561556def parse_basic_str_escape (
562557 src : str , pos : Pos , * , multiline : bool = False
563- ) -> tuple [Pos , str ]:
558+ ) -> Tuple [Pos , str ]:
564559 escape_id = src [pos : pos + 2 ]
565560 pos += 2
566561 if multiline and escape_id in {"\\ " , "\\ \t " , "\\ \n " }:
@@ -587,11 +582,11 @@ def parse_basic_str_escape(
587582 raise TOMLDecodeError ("Unescaped '\\ ' in a string" , src , pos ) from None
588583
589584
590- def parse_basic_str_escape_multiline (src : str , pos : Pos ) -> tuple [Pos , str ]:
585+ def parse_basic_str_escape_multiline (src : str , pos : Pos ) -> Tuple [Pos , str ]:
591586 return parse_basic_str_escape (src , pos , multiline = True )
592587
593588
594- def parse_hex_char (src : str , pos : Pos , hex_len : int ) -> tuple [Pos , str ]:
589+ def parse_hex_char (src : str , pos : Pos , hex_len : int ) -> Tuple [Pos , str ]:
595590 hex_str = src [pos : pos + hex_len ]
596591 if len (hex_str ) != hex_len or not HEXDIGIT_CHARS .issuperset (hex_str ):
597592 raise TOMLDecodeError ("Invalid hex value" , src , pos )
@@ -604,7 +599,7 @@ def parse_hex_char(src: str, pos: Pos, hex_len: int) -> tuple[Pos, str]:
604599 return pos , chr (hex_int )
605600
606601
607- def parse_literal_str (src : str , pos : Pos ) -> tuple [Pos , str ]:
602+ def parse_literal_str (src : str , pos : Pos ) -> Tuple [Pos , str ]:
608603 pos += 1 # Skip starting apostrophe
609604 start_pos = pos
610605 pos = skip_until (
@@ -613,7 +608,7 @@ def parse_literal_str(src: str, pos: Pos) -> tuple[Pos, str]:
613608 return pos + 1 , src [start_pos :pos ] # Skip ending apostrophe
614609
615610
616- def parse_multiline_str (src : str , pos : Pos , * , literal : bool ) -> tuple [Pos , str ]:
611+ def parse_multiline_str (src : str , pos : Pos , * , literal : bool ) -> Tuple [Pos , str ]:
617612 pos += 3
618613 if src .startswith ("\n " , pos ):
619614 pos += 1
@@ -644,7 +639,7 @@ def parse_multiline_str(src: str, pos: Pos, *, literal: bool) -> tuple[Pos, str]
644639 return pos , result + (delim * 2 )
645640
646641
647- def parse_basic_str (src : str , pos : Pos , * , multiline : bool ) -> tuple [Pos , str ]:
642+ def parse_basic_str (src : str , pos : Pos , * , multiline : bool ) -> Tuple [Pos , str ]:
648643 if multiline :
649644 error_on = ILLEGAL_MULTILINE_BASIC_STR_CHARS
650645 parse_escapes = parse_basic_str_escape_multiline
@@ -678,7 +673,7 @@ def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Pos, str]:
678673
679674def parse_value ( # noqa: C901
680675 src : str , pos : Pos , parse_float : ParseFloat , nest_lvl : int
681- ) -> tuple [Pos , Any ]:
676+ ) -> Tuple [Pos , Any ]:
682677 if nest_lvl > MAX_INLINE_NESTING :
683678 # Pure Python should have raised RecursionError already.
684679 # This ensures mypyc binaries eventually do the same.
@@ -688,7 +683,7 @@ def parse_value( # noqa: C901
688683 )
689684
690685 try :
691- char : str | None = src [pos ]
686+ char = src [pos ]
692687 except IndexError :
693688 char = None
694689
0 commit comments