Skip to content

Commit 3c7ec04

Browse files
authored
Update tomllib from 3.13.5 (#5902)
1 parent f57f6d7 commit 3c7ec04

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

Lib/test/test_tomllib/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from test.test_tomllib import load_tests
3+
from . import load_tests
44

55

66
unittest.main()

Lib/test/test_tomllib/test_misc.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import tempfile
1111
import unittest
12+
from test import support
1213

1314
from . import tomllib
1415

@@ -92,13 +93,23 @@ def test_deepcopy(self):
9293
self.assertEqual(obj_copy, expected_obj)
9394

9495
def test_inline_array_recursion_limit(self):
95-
# 465 with default recursion limit
96-
nest_count = int(sys.getrecursionlimit() * 0.465)
97-
recursive_array_toml = "arr = " + nest_count * "[" + nest_count * "]"
98-
tomllib.loads(recursive_array_toml)
96+
with support.infinite_recursion(max_depth=100):
97+
available = support.get_recursion_available()
98+
nest_count = (available // 2) - 2
99+
# Add details if the test fails
100+
with self.subTest(limit=sys.getrecursionlimit(),
101+
available=available,
102+
nest_count=nest_count):
103+
recursive_array_toml = "arr = " + nest_count * "[" + nest_count * "]"
104+
tomllib.loads(recursive_array_toml)
99105

100106
def test_inline_table_recursion_limit(self):
101-
# 310 with default recursion limit
102-
nest_count = int(sys.getrecursionlimit() * 0.31)
103-
recursive_table_toml = nest_count * "key = {" + nest_count * "}"
104-
tomllib.loads(recursive_table_toml)
107+
with support.infinite_recursion(max_depth=100):
108+
available = support.get_recursion_available()
109+
nest_count = (available // 3) - 1
110+
# Add details if the test fails
111+
with self.subTest(limit=sys.getrecursionlimit(),
112+
available=available,
113+
nest_count=nest_count):
114+
recursive_table_toml = nest_count * "key = {" + nest_count * "}"
115+
tomllib.loads(recursive_table_toml)

Lib/tomllib/_parser.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class Flags:
142142
EXPLICIT_NEST = 1
143143

144144
def __init__(self) -> None:
145-
self._flags: dict[str, dict] = {}
145+
self._flags: dict[str, dict[Any, Any]] = {}
146146
self._pending_flags: set[tuple[Key, int]] = set()
147147

148148
def add_pending(self, key: Key, flag: int) -> None:
@@ -200,7 +200,7 @@ def get_or_create_nest(
200200
key: Key,
201201
*,
202202
access_lists: bool = True,
203-
) -> dict:
203+
) -> dict[str, Any]:
204204
cont: Any = self.dict
205205
for k in key:
206206
if k not in cont:
@@ -210,7 +210,7 @@ def get_or_create_nest(
210210
cont = cont[-1]
211211
if not isinstance(cont, dict):
212212
raise KeyError("There is no nest behind this key")
213-
return cont
213+
return cont # type: ignore[no-any-return]
214214

215215
def append_nest_to_list(self, key: Key) -> None:
216216
cont = self.get_or_create_nest(key[:-1])
@@ -409,9 +409,9 @@ def parse_one_line_basic_str(src: str, pos: Pos) -> tuple[Pos, str]:
409409
return parse_basic_str(src, pos, multiline=False)
410410

411411

412-
def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, list]:
412+
def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, list[Any]]:
413413
pos += 1
414-
array: list = []
414+
array: list[Any] = []
415415

416416
pos = skip_comments_and_array_ws(src, pos)
417417
if src.startswith("]", pos):
@@ -433,7 +433,7 @@ def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, list]
433433
return pos + 1, array
434434

435435

436-
def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, dict]:
436+
def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, dict[str, Any]]:
437437
pos += 1
438438
nested_dict = NestedDict()
439439
flags = Flags()
@@ -679,7 +679,7 @@ def make_safe_parse_float(parse_float: ParseFloat) -> ParseFloat:
679679
instead of returning illegal types.
680680
"""
681681
# The default `float` callable never returns illegal types. Optimize it.
682-
if parse_float is float: # type: ignore[comparison-overlap]
682+
if parse_float is float:
683683
return float
684684

685685
def safe_parse_float(float_str: str) -> Any:

Lib/tomllib/_re.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
)
5050

5151

52-
def match_to_datetime(match: re.Match) -> datetime | date:
52+
def match_to_datetime(match: re.Match[str]) -> datetime | date:
5353
"""Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`.
5454
5555
Raises ValueError if the match does not correspond to a valid date
@@ -95,13 +95,13 @@ def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
9595
)
9696

9797

98-
def match_to_localtime(match: re.Match) -> time:
98+
def match_to_localtime(match: re.Match[str]) -> time:
9999
hour_str, minute_str, sec_str, micros_str = match.groups()
100100
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
101101
return time(int(hour_str), int(minute_str), int(sec_str), micros)
102102

103103

104-
def match_to_number(match: re.Match, parse_float: ParseFloat) -> Any:
104+
def match_to_number(match: re.Match[str], parse_float: ParseFloat) -> Any:
105105
if match.group("floatpart"):
106106
return parse_float(match.group())
107107
return int(match.group(), 0)

Lib/tomllib/mypy.ini

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Config file for running mypy on tomllib.
2+
# Run mypy by invoking `mypy --config-file Lib/tomllib/mypy.ini`
3+
# on the command-line from the repo root
4+
5+
[mypy]
6+
files = Lib/tomllib
7+
mypy_path = $MYPY_CONFIG_FILE_DIR/../../Misc/mypy
8+
explicit_package_bases = True
9+
python_version = 3.12
10+
pretty = True
11+
12+
# Enable most stricter settings
13+
enable_error_code = ignore-without-code
14+
strict = True
15+
strict_bytes = True
16+
local_partial_types = True
17+
warn_unreachable = True

0 commit comments

Comments
 (0)