8000 gh-118761: Improve import time of `tomllib` (#128907) · python/cpython@ea6cc26 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea6cc26

Browse files
authored
gh-118761: Improve import time of tomllib (#128907)
Improve import time of `tomllib` (in sync with upstream)
1 parent c879de7 commit ea6cc26

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

Lib/test/test_tomllib/test_misc.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import copy
66
import datetime
77
from decimal import Decimal as D
8+
import importlib
89
from pathlib import Path
910
import sys
1011
import tempfile
@@ -113,3 +114,11 @@ def test_inline_table_recursion_limit(self):
113114
nest_count=nest_count):
114115
recursive_table_toml = nest_count * "key = {" + nest_count * "}"
115116
tomllib.loads(recursive_table_toml)
117+
118+
def test_types_import(self):
119+
"""Test that `_types` module runs.
120+
121+
The module is for type annotations only, so it is otherwise
122+
never imported by tests.
123+
"""
124+
importlib.import_module(f"{tomllib.__name__}._types")

Lib/tomllib/_parser.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44

55
from __future__ import annotations
66

7-
from collections.abc import Iterable
8-
import string
97
from types import MappingProxyType
10-
from typing import Any, BinaryIO, NamedTuple
11-
import warnings
128

139
from ._re import (
1410
RE_DATETIME,
@@ -18,7 +14,13 @@
1814
match_to_localtime,
1915
match_to_number,
2016
)
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
2224

2325
ASCII_CTRL = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))
2426

@@ -34,9 +36,11 @@
3436

3537
TOML_WS = frozenset(" \t")
3638
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+
)
3842
KEY_INITIAL_CHARS = BARE_KEY_CHARS | frozenset("\"'")
39-
HEXDIGIT_CHARS = frozenset(string.hexdigits)
43+
HEXDIGIT_CHARS = frozenset("abcdef" "ABCDEF" "0123456789")
4044

4145
BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType(
4246
{
@@ -80,6 +84,8 @@ def __init__(
8084
or not isinstance(doc, str)
8185
or not isinstance(pos, int)
8286
):
87+
import warnings
88+
8389
warnings.warn(
8490
"Free-form arguments for TOMLDecodeError are deprecated. "
8591
"Please set 'msg' (str), 'doc' (str) and 'pos' (int) arguments only.",
@@ -115,7 +121,7 @@ def __init__(
115121
self.colno = colno
116122

117123

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]:
119125
"""Parse TOML from a binary file object."""
120126
b = fp.read()
121127
try:
@@ -139,7 +145,7 @@ def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # n
139145
f"Expected str object, not '{type(s).__qualname__}'"
140146
) from None
141147
pos = 0
142-
out = Output(NestedDict(), Flags())
148+
out = Output()
143149
header: Key = ()
144150
parse_float = make_safe_parse_float(parse_float)
145151

@@ -290,9 +296,10 @@ def append_nest_to_list(self, key: Key) -> None:
290296
cont[last_key] = [{}]
291297

292298

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()
296303

297304

298305
def skip_chars(src: str, pos: Pos, chars: Iterable[str]) -> Pos:

Lib/tomllib/_re.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
from datetime import date, datetime, time, timedelta, timezone, tzinfo
88
from functools import lru_cache
99
import re
10-
from typing import Any
1110

12-
from ._types import ParseFloat
11+
TYPE_CHECKING = False
12+
if TYPE_CHECKING:
13+
from typing import Any
14+
15+
from ._types import ParseFloat
1316

1417
# E.g.
1518
# - 00:32:00.999999
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve import time of :mod:`tomllib` by removing ``typing``, ``string``,
2+
and ``tomllib._types`` imports. Patch by Taneli Hukkinen.

0 commit comments

Comments
 (0)
0