Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import enum
import itertools
import logging
import math
import weakref
from collections.abc import Collection, Generator, Iterable, Iterator, Mapping, Sequence
from functools import cache, cached_property
Expand Down Expand Up @@ -720,6 +721,9 @@ def serialize(
# enum.IntEnum is an int instance, it causes json dumps error so we use its value.
if isinstance(var, enum.Enum):
return var.value
# These are not allowed in JSON. https://datatracker.ietf.org/doc/html/rfc8259#section-6
if isinstance(var, float) and (math.isnan(var) or math.isinf(var)):
return str(var)
return var
elif isinstance(var, dict):
return cls._encode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from __future__ import annotations

import json
import math
from collections.abc import Iterator
from datetime import datetime, timedelta

Expand Down Expand Up @@ -254,6 +255,9 @@ def __len__(self) -> int:
[
("test_str", None, equals),
(1, None, equals),
(math.nan, None, lambda a, b: b == "nan"),
(math.inf, None, lambda a, b: b == "inf"),
(-math.inf, None, lambda a, b: b == "-inf"),
(timezone.utcnow(), DAT.DATETIME, equal_time),
(timedelta(minutes=2), DAT.TIMEDELTA, equals),
(Timezone("UTC"), DAT.TIMEZONE, lambda a, b: a.name == b.name),
Expand Down