Skip to content

Commit

Permalink
Reduce import time part 2: lazy import rarely used datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin committed Jan 9, 2025
1 parent 4188188 commit fbee0e7
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/tomli/_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

from __future__ import annotations

from datetime import date, datetime, time, timedelta, timezone, tzinfo
from functools import lru_cache
import re
from typing import TYPE_CHECKING, Any, Final

if TYPE_CHECKING:
from datetime import date, datetime, time, timezone

from ._types import ParseFloat

# E.g.
Expand Down Expand Up @@ -58,6 +59,8 @@ def match_to_datetime(match: re.Match) -> datetime | date:
Raises ValueError if the match does not correspond to a valid date
or datetime.
"""
from datetime import date, datetime, timezone, tzinfo

(
year_str,
month_str,
Expand Down Expand Up @@ -92,6 +95,8 @@ def match_to_datetime(match: re.Match) -> datetime | date:
# 24 (hours) * 60 (minutes) * 2 (offset direction) = 2880.
@lru_cache(maxsize=None)
def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
from datetime import timedelta, timezone

sign = 1 if sign_str == "+" else -1
return timezone(
timedelta(
Expand All @@ -102,6 +107,8 @@ def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:


def match_to_localtime(match: re.Match) -> time:
from datetime import 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)
Expand Down

0 comments on commit fbee0e7

Please sign in to comment.