Skip to content

HDF5Dict.flatten() infinitely recurses on an empty group #108

Description

@BenWibking

HDF5Dict.flatten() infinitely recurses on an empty group

Summary

Flattening an HDF5Dict that contains an empty group raises RecursionError instead
of returning the flattened non-empty datasets (or an empty mapping).

Explanation

HDF5Dict.flatten() uses if not d: d = self to distinguish an omitted argument
from a recursive sub-dictionary. During recursion, an empty child dictionary is
therefore replaced with the top-level object and traversed again indefinitely.

from jaff.types import HDF5Dict

h5dict = HDF5Dict({"empty": {}})
h5dict.flatten()  # RecursionError: maximum recursion depth exceeded

The same truthiness check also makes flatten({}) and nested({}) operate on
self, even though the caller explicitly supplied an empty mapping.

Proposed patch

Change both signatures to use a sentinel, for example d: dict | None = None, and
substitute self only when d is None. An empty recursive group will then return an
empty mapping normally. Add regression tests for a top-level empty object, an empty
nested group beside a populated group, and explicit flatten({}) / nested({}).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-code-auditIssues found by AI code audit

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions