Skip to content

Commit 764c9ca

Browse files
committed
Reduce import time by removing string and tomli._types imports
1 parent 42a570d commit 764c9ca

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

pyproject.toml

+6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ commands = [
102102
["python", "benchmark/run.py"],
103103
]
104104

105+
[tool.tox.env."benchmark-import"]
106+
description = "Measure module import times. Tox sends Python output to stderr, so to filter use e.g. `tox -e benchmark-import 2> >(grep tomli)`."
107+
commands = [
108+
["python", "-X", "importtime", "-c", "import tomli"],
109+
]
110+
105111
[tool.tox.env."fuzz"]
106112
description = 'run the fuzzer against a local Tomli version (needs "apt install clang")'
107113
deps = [

src/tomli/_parser.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
from __future__ import annotations
66

7-
from collections.abc import Iterable
8-
import string
97
import sys
108
from types import MappingProxyType
11-
from typing import IO, Any, Final, NamedTuple
12-
import warnings
9+
from typing import IO, TYPE_CHECKING, Any, Final, NamedTuple
1310

1411
from ._re import (
1512
RE_DATETIME,
@@ -19,7 +16,11 @@
1916
match_to_localtime,
2017
match_to_number,
2118
)
22-
from ._types import Key, ParseFloat, Pos
19+
20+
if TYPE_CHECKING:
21+
from collections.abc import Iterable
22+
23+
from ._types import Key, ParseFloat, Pos
2324

2425
# Inline tables/arrays are implemented using recursion. Pathologically
2526
# nested documents cause pure Python to raise RecursionError (which is OK),
@@ -46,9 +47,11 @@
4647

4748
TOML_WS: Final = frozenset(" \t")
4849
TOML_WS_AND_NEWLINE: Final = TOML_WS | frozenset("\n")
49-
BARE_KEY_CHARS: Final = frozenset(string.ascii_letters + string.digits + "-_")
50+
BARE_KEY_CHARS: Final = frozenset(
51+
"abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_"
52+
)
5053
KEY_INITIAL_CHARS: Final = BARE_KEY_CHARS | frozenset("\"'")
51-
HEXDIGIT_CHARS: Final = frozenset(string.hexdigits)
54+
HEXDIGIT_CHARS: Final = frozenset("abcdef" "ABCDEF" "0123456789")
5255

5356
BASIC_STR_ESCAPE_REPLACEMENTS: Final = MappingProxyType(
5457
{
@@ -92,6 +95,8 @@ def __init__(
9295
or not isinstance(doc, str)
9396
or not isinstance(pos, int)
9497
):
98+
import warnings
99+
95100
warnings.warn(
96101
"Free-form arguments for TOMLDecodeError are deprecated. "
97102
"Please set 'msg' (str), 'doc' (str) and 'pos' (int) arguments only.",

src/tomli/_re.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
from datetime import date, datetime, time, timedelta, timezone, tzinfo
88
from functools import lru_cache
99
import re
10-
from typing import Any, Final
10+
from typing import TYPE_CHECKING, Any, Final
1111

12-
from ._types import ParseFloat
12+
if TYPE_CHECKING:
13+
from ._types import ParseFloat
1314

1415
# E.g.
1516
# - 00:32:00.999999

tests/test_misc.py

+9
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
@@ -128,3 +129,11 @@ def test_inline_table_recursion_limit(self):
128129
r"TOML inline arrays/tables are nested more than the allowed [0-9]+ levels",
129130
):
130131
tomllib.loads(recursive_table_toml)
132+
133+
def test_types_import(self):
134+
"""Test that `_types` module runs.
135+
136+
The module is for type annotations only, so it is otherwise
137+
never imported by tests.
138+
"""
139+
importlib.import_module(f"{tomllib.__name__}._types")

0 commit comments

Comments
 (0)