Open
Description
to_jsonable_python
only seems to apply exclude_none
for pydantic models, presumbly for dataclass.dataclass
or dict
types it is just returning asdict
or the supplied value for the top level
Expected
exclude_none=True
should be honoured for all dict members in result of to_jsonable_python
..
.... I feel that the behaviour should be homogeneous here, since to_jsonable_python
is accepting ( and serialising ) none pydantic types, the fact that this top level instruction is ignored, while others are honoured, seems inconsistent.
Test Case
@pydantic.dataclasses.dataclass
class PDCNoneableModel:
status_code: int = 200
test1: str = "ok"
test2: str | None = None
@dataclasses.dataclass
class DataclassNoneable:
status_code: int = 200
test1: str = "ok"
test2: str | None = None
@pytest.mark.parametrize("modellike", [
PDCNoneableModel(), DataclassNoneable(), {"test1": "ok", "test2": None}
])
async def test_jsonable_exclude_none(modellike: PDCNoneableModel | DataclassNoneable | dict):
raw_out = to_jsonable_python(modellike, exclude_none=True)
assert isinstance(raw_out, dict)
assert raw_out["status_code"] == 200
assert "test1" in raw_out
assert "test2" not in raw_out