Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce import time by lazy importing decimal #69

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading