Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude_none_in_lists is not recursively passed down #44

Open
DrumsnChocolate opened this issue Mar 9, 2024 · 1 comment · May be fixed by #45
Open

exclude_none_in_lists is not recursively passed down #44

DrumsnChocolate opened this issue Mar 9, 2024 · 1 comment · May be fixed by #45

Comments

@DrumsnChocolate
Copy link

from version 0.8.18:

def _dict_value(v, is_recursive, exclude_none, exclude_none_in_lists):
    if is_recursive and isinstance(v, Prodict):
        return v.to_dict(is_recursive=is_recursive, exclude_none=exclude_none)
    if exclude_none_in_lists and isinstance(v, List):
        return [
            item.to_dict(exclude_none=True, is_recursive=is_recursive) if isinstance(item, Prodict) else item
            for item in v]
    return v

Is there a reason that item.to_dict(exclude_none=True, is_recursive=is_recursive) omits the argument exclude_none_in_lists?
I would think that if we are recursively turning prodicts into dicts, we would also want the lists in sub-prodicts to adhere to the original exclude_none_in_lists we pass to the Prodict.to_dict method.

should
item.to_dict(exclude_none=True, is_recursive=is_recursive)
be replaced by
item.to_dict(exclude_none_in_lists=True, exclude_none=True, is_recursive=is_recursive)
or something to the extent?

@DrumsnChocolate
Copy link
Author

DrumsnChocolate commented Mar 9, 2024

Adding to this, I also found that the supposed recursive application of to_dict was actually obstructed by the check for if exclude_none_in_lists, since this flag is preventing the recursion from taking place for lists.

To have it adhere to what I understand about how this is supposed to function, I would rewrite the _dict_value function as follows:

def _dict_value(v, is_recursive, exclude_none, exclude_none_in_lists):
    if is_recursive and isinstance(v, Prodict):
        return v.to_dict(is_recursive=is_recursive, exclude_none=exclude_none, exclude_none_in_lists=exclude_none_in_lists)
    if is_recursive and isinstance(v, List):
        return [
            item.to_dict(
                exclude_none=exclude_none, 
                exclude_none_in_lists=exclude_none_in_lists,
                is_recursive=is_recursive
            ) if isinstance(item, Prodict) else item
            for item in v
            if _none_condition(item, is_recursive=is_recursive, exclude_none=exclude_none_in_lists)
        ]
    return v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant