Skip to content

Commit

Permalink
Reduce import time by removing string and tomli._types imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin committed Jan 9, 2025
1 parent 42a570d commit 4188188
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ commands = [
["python", "benchmark/run.py"],
]

[tool.tox.env."benchmark-import"]
description = "Measure module import times. Tox sends Python output to stderr, so to filter use e.g. `tox -e benchmark-import 2> >(grep tomli)`."
commands = [
["python", "-X", "importtime", "-c", "import tomli"],
]

[tool.tox.env."fuzz"]
description = 'run the fuzzer against a local Tomli version (needs "apt install clang")'
deps = [
Expand Down
19 changes: 12 additions & 7 deletions src/tomli/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

from __future__ import annotations

from collections.abc import Iterable
import string
import sys
from types import MappingProxyType
from typing import IO, Any, Final, NamedTuple
import warnings
from typing import IO, TYPE_CHECKING, Any, Final, NamedTuple

from ._re import (
RE_DATETIME,
Expand All @@ -19,7 +16,11 @@
match_to_localtime,
match_to_number,
)
from ._types import Key, ParseFloat, Pos

if TYPE_CHECKING:
from collections.abc import Iterable

from ._types import Key, ParseFloat, Pos

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

TOML_WS: Final = frozenset(" \t")
TOML_WS_AND_NEWLINE: Final = TOML_WS | frozenset("\n")
BARE_KEY_CHARS: Final = frozenset(string.ascii_letters + string.digits + "-_")
BARE_KEY_CHARS: Final = frozenset(
"abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_"
)
KEY_INITIAL_CHARS: Final = BARE_KEY_CHARS | frozenset("\"'")
HEXDIGIT_CHARS: Final = frozenset(string.hexdigits)
HEXDIGIT_CHARS: Final = frozenset("abcdef" "ABCDEF" "0123456789")

BASIC_STR_ESCAPE_REPLACEMENTS: Final = MappingProxyType(
{
Expand Down Expand Up @@ -92,6 +95,8 @@ def __init__(
or not isinstance(doc, str)
or not isinstance(pos, int)
):
import warnings

warnings.warn(
"Free-form arguments for TOMLDecodeError are deprecated. "
"Please set 'msg' (str), 'doc' (str) and 'pos' (int) arguments only.",
Expand Down
5 changes: 3 additions & 2 deletions src/tomli/_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from datetime import date, datetime, time, timedelta, timezone, tzinfo
from functools import lru_cache
import re
from typing import Any, Final
from typing import TYPE_CHECKING, Any, Final

from ._types import ParseFloat
if TYPE_CHECKING:
from ._types import ParseFloat

# E.g.
# - 00:32:00.999999
Expand Down
9 changes: 9 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import copy
import datetime
from decimal import Decimal as D
import importlib
from pathlib import Path
import sys
import tempfile
Expand Down Expand Up @@ -128,3 +129,11 @@ def test_inline_table_recursion_limit(self):
r"TOML inline arrays/tables are nested more than the allowed [0-9]+ levels",
):
tomllib.loads(recursive_table_toml)

def test_types_import(self):
"""Test that `_types` module runs.
The module is for type annotations only, so it is otherwise
never imported by tests.
"""
importlib.import_module(f"{tomllib.__name__}._types")

0 comments on commit 4188188

Please sign in to comment.