Skip to content

Update tomllib from 3.13.5 #5902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update tomllib from 3.13.5
  • Loading branch information
ShaharNaveh committed Jul 4, 2025
commit 61e51e424e5895148f5c0e0ed78ee83b5a73675d
2 changes: 1 addition & 1 deletion Lib/test/test_tomllib/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from test.test_tomllib import load_tests
from . import load_tests


unittest.main()
27 changes: 19 additions & 8 deletions Lib/test/test_tomllib/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
import tempfile
import unittest
from test import support

from . import tomllib

Expand Down Expand Up @@ -92,13 +93,23 @@ def test_deepcopy(self):
self.assertEqual(obj_copy, expected_obj)

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

def test_inline_table_recursion_limit(self):
# 310 with default recursion limit
nest_count = int(sys.getrecursionlimit() * 0.31)
recursive_table_toml = nest_count * "key = {" + nest_count * "}"
tomllib.loads(recursive_table_toml)
with support.infinite_recursion(max_depth=100):
available = support.get_recursion_available()
nest_count = (available // 3) - 1
# Add details if the test fails
with self.subTest(limit=sys.getrecursionlimit(),
available=available,
nest_count=nest_count):
recursive_table_toml = nest_count * "key = {" + nest_count * "}"
tomllib.loads(recursive_table_toml)
14 changes: 7 additions & 7 deletions Lib/tomllib/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Flags:
EXPLICIT_NEST = 1

def __init__(self) -> None:
self._flags: dict[str, dict] = {}
self._flags: dict[str, dict[Any, Any]] = {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider using typing.TypedDict for more explicit type safety, especially since the keys in this dictionary are known and fixed. This can help catch errors during development.[^1]

from typing import Any, TypedDict

class FlagsDict(TypedDict):
    flags: set[int]
    recursive_flags: set[int]
    nested: dict[str, Any]

    _flags: dict[str, FlagsDict] = {}

self._pending_flags: set[tuple[Key, int]] = set()

def add_pending(self, key: Key, flag: int) -> None:
Expand Down Expand Up @@ -200,7 +200,7 @@ def get_or_create_nest(
key: Key,
*,
access_lists: bool = True,
) -> dict:
) -> dict[str, Any]:
cont: Any = self.dict
for k in key:
if k not in cont:
Expand All @@ -210,7 +210,7 @@ def get_or_create_nest(
cont = cont[-1]
if not isinstance(cont, dict):
raise KeyError("There is no nest behind this key")
return cont
return cont # type: ignore[no-any-return]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type ignore here might hide potential type errors. It would be better to ensure that the return type is always a dict[str, Any] or refactor the code to handle other return types explicitly. Consider adding a check to ensure that cont is indeed a dictionary before returning it, and raise a more informative error if it is not.[^1]

if not isinstance(cont, dict):
    raise TypeError("Expected a dictionary, but got {type(cont)}")
return cont


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


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

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


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

def safe_parse_float(float_str: str) -> Any:
Expand Down
6 changes: 3 additions & 3 deletions Lib/tomllib/_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
)


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


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


def match_to_number(match: re.Match, parse_float: ParseFloat) -> Any:
def match_to_number(match: re.Match[str], parse_float: ParseFloat) -> Any:
if match.group("floatpart"):
return parse_float(match.group())
return int(match.group(), 0)
17 changes: 17 additions & 0 deletions Lib/tomllib/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Config file for running mypy on tomllib.
# Run mypy by invoking `mypy --config-file Lib/tomllib/mypy.ini`
# on the command-line from the repo root

[mypy]
files = Lib/tomllib
mypy_path = $MYPY_CONFIG_FILE_DIR/../../Misc/mypy
explicit_package_bases = True
python_version = 3.12
pretty = True

# Enable most stricter settings
enable_error_code = ignore-without-code
strict = True
strict_bytes = True
local_partial_types = True
warn_unreachable = True