Skip to content

Commit

Permalink
Reduce import time by lazy importing decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin committed Jan 13, 2025
1 parent 95cc6c7 commit 90fff30
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/tomli_w/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from collections.abc import Mapping
from datetime import date, datetime, time
from decimal import Decimal
from types import MappingProxyType

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Generator
from decimal import Decimal
from typing import IO, Any, Final

ASCII_CTRL = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))
Expand Down Expand Up @@ -102,8 +102,6 @@ def format_literal(obj: object, ctx: Context, *, nest_level: int = 0) -> str:
return "true" if obj else "false"
if isinstance(obj, (int, float, date, datetime)):
return str(obj)
if isinstance(obj, Decimal):
return format_decimal(obj)
if isinstance(obj, time):
if obj.tzinfo:
raise ValueError("TOML does not support offset times")
Expand All @@ -114,6 +112,12 @@ def format_literal(obj: object, ctx: Context, *, nest_level: int = 0) -> str:
return format_inline_array(obj, ctx, nest_level)
if isinstance(obj, Mapping):
return format_inline_table(obj, ctx)

# Lazy import to improve module import time
from decimal import Decimal

if isinstance(obj, Decimal):
return format_decimal(obj)
raise TypeError(
f"Object of type '{type(obj).__qualname__}' is not TOML serializable"
)
Expand All @@ -122,10 +126,8 @@ def format_literal(obj: object, ctx: Context, *, nest_level: int = 0) -> str:
def format_decimal(obj: Decimal) -> str:
if obj.is_nan():
return "nan"
if obj == Decimal("inf"):
return "inf"
if obj == Decimal("-inf"):
return "-inf"
if obj.is_infinite():
return "-inf" if obj.is_signed() else "inf"
dec_str = str(obj).lower()
return dec_str if "." in dec_str or "e" in dec_str else dec_str + ".0"

Expand Down

0 comments on commit 90fff30

Please sign in to comment.