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

[Bug] Assert CompositeSpec order guarantees #2482

Open
vmoens opened this issue Oct 10, 2024 Discussed in #2341 · 0 comments
Open

[Bug] Assert CompositeSpec order guarantees #2482

vmoens opened this issue Oct 10, 2024 Discussed in #2341 · 0 comments

Comments

@vmoens
Copy link
Contributor

vmoens commented Oct 10, 2024

Discussed in #2341

Originally posted by hesic73 July 31, 2024
I have a question about the order of keys returned by CompositeSpec.keys(include_nested=True, leaves_only=False). Is there any guarantee on the order of the keys, specifically that the iterator iterates from the deepest nodes to the root?

I want to write a function that removes all empty entries from a CompositeSpec:

def _remove_empty_entries(spec: CompositeSpec) -> CompositeSpec:
    for key in list(spec.keys(True, False)):
        if not isinstance(spec[key], CompositeSpec):
            continue
        if spec[key].is_empty():
            del spec[key]
    return spec

As shown in the function above, it assumes that the iterator processes from the deepest nodes to the root. This assumption is crucial for the function to work correctly.

If there is no such guarantee, how can I achieve this goal? A CompositeSpec with empty entries is quite undesirable.

Thank you!

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

No branches or pull requests

1 participant