diff --git a/pyproject.toml b/pyproject.toml index a96cfd5..79cf41c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ diff --git a/src/tomli/_parser.py b/src/tomli/_parser.py index b548e8b..e251c70 100644 --- a/src/tomli/_parser.py +++ b/src/tomli/_parser.py @@ -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, @@ -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), @@ -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( { @@ -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.", diff --git a/src/tomli/_re.py b/src/tomli/_re.py index 5134866..fff272f 100644 --- a/src/tomli/_re.py +++ b/src/tomli/_re.py @@ -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 diff --git a/tests/test_misc.py b/tests/test_misc.py index 6d0c463..c2f6bb7 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -5,6 +5,7 @@ import copy import datetime from decimal import Decimal as D +import importlib from pathlib import Path import sys import tempfile @@ -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")