prodict icon indicating copy to clipboard operation
prodict copied to clipboard

Nested types with lists and excluding None.

Open andrewmellinger opened this issue 4 years ago • 0 comments

Greetings!

This is a similar issue to #23 except I have the list inside another Prodict. I seems like the exclude flag isn't getting passed down properly. I have attached another case to reproduce the issue and it currently occurs on 0.8.18.

#! /usr/bin/env python3

import json

from prodict import Prodict, List


class ImagesSource(Prodict):
    dir: str
    label: int
    count: int


class ImageData(Prodict):
    task_type: str
    sources: List[ImagesSource]


class Config(Prodict):
    image_data: ImageData


data = {
    "image_data": {
        "sources": [
            {
                "dir": "some/dir",
                "label": 0
            },
            {
                "dir": "other/dir",
                "label": 1
            }
        ]
    }
}

model = Config.from_dict(data)
print(json.dumps(model.to_dict(exclude_none=True, exclude_none_in_lists=True, is_recursive=True)))

I get: {"image_data": {"sources": [{"dir": "some/dir", "label": 0, "count": null}, {"dir": "other/dir", "label": 1, "count": null}]}}

I expect: {"image_data": {"sources": [{"dir": "some/dir", "label": 0}, {"dir": "other/dir", "label": 1}]}}

Thanks for any help!

andrewmellinger avatar Jul 26 '21 21:07 andrewmellinger