Skip to content

Nested types with lists and excluding None. #25

@andrewmellinger

Description

@andrewmellinger

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions