|
7 | 7 | from datetime import date, datetime, time, timedelta, timezone |
8 | 8 | from decimal import Decimal |
9 | 9 | from enum import Enum |
| 10 | +from math import inf, isinf, isnan, nan |
10 | 11 | from pathlib import Path |
11 | 12 | from typing import ClassVar |
12 | 13 |
|
@@ -603,3 +604,43 @@ def test_numpy_float(any_serializer): |
603 | 604 | any_serializer.to_python(numpy.float16(1.0), mode='json') |
604 | 605 | with pytest.raises(PydanticSerializationError, match=r"Unable to serialize unknown type: <class 'numpy\.float16'>"): |
605 | 606 | any_serializer.to_json(numpy.float16(1.0)) |
| 607 | + |
| 608 | + |
| 609 | +def test_ser_json_inf_nan_with_any() -> None: |
| 610 | + s = SchemaSerializer(core_schema.any_schema(), core_schema.CoreConfig(ser_json_inf_nan='constants')) |
| 611 | + assert isinf(s.to_python(inf)) |
| 612 | + assert isinf(s.to_python(inf, mode='json')) |
| 613 | + assert s.to_json(inf) == b'Infinity' |
| 614 | + assert isnan(s.to_python(nan)) |
| 615 | + assert isnan(s.to_python(nan, mode='json')) |
| 616 | + assert s.to_json(nan) == b'NaN' |
| 617 | + |
| 618 | + s = SchemaSerializer(core_schema.any_schema(), core_schema.CoreConfig(ser_json_inf_nan='null')) |
| 619 | + assert isinf(s.to_python(inf)) |
| 620 | + assert s.to_python(inf, mode='json') is None |
| 621 | + assert s.to_json(inf) == b'null' |
| 622 | + assert isnan(s.to_python(nan)) |
| 623 | + assert s.to_python(nan, mode='json') is None |
| 624 | + assert s.to_json(nan) == b'null' |
| 625 | + |
| 626 | + |
| 627 | +def test_ser_json_inf_nan_with_list_of_any() -> None: |
| 628 | + s = SchemaSerializer( |
| 629 | + core_schema.list_schema(core_schema.any_schema()), core_schema.CoreConfig(ser_json_inf_nan='constants') |
| 630 | + ) |
| 631 | + assert isinf(s.to_python([inf])[0]) |
| 632 | + assert isinf(s.to_python([inf], mode='json')[0]) |
| 633 | + assert s.to_json([inf]) == b'[Infinity]' |
| 634 | + assert isnan(s.to_python([nan])[0]) |
| 635 | + assert isnan(s.to_python([nan], mode='json')[0]) |
| 636 | + assert s.to_json([nan]) == b'[NaN]' |
| 637 | + |
| 638 | + s = SchemaSerializer( |
| 639 | + core_schema.list_schema(core_schema.any_schema()), core_schema.CoreConfig(ser_json_inf_nan='null') |
| 640 | + ) |
| 641 | + assert isinf(s.to_python([inf])[0]) |
| 642 | + assert s.to_python([inf], mode='json')[0] is None |
| 643 | + assert s.to_json([inf]) == b'[null]' |
| 644 | + assert isnan(s.to_python([nan])[0]) |
| 645 | + assert s.to_python([nan], mode='json')[0] is None |
| 646 | + assert s.to_json([nan]) == b'[null]' |
0 commit comments