Skip to content

Robustness improvements for _dict_utils.py: Handling edge cases and preventing RuntimeErrors #3

@SalimMessaad1

Description

@SalimMessaad1

Hi there,

I was looking through the utility functions in _dict_utils.py and noticed a few spots where the code might break or behave unexpectedly when hitting certain edge cases. I think adding some defensive checks could save users from some confusing debugging sessions.

Here are the main points I found:

  1. Potential RuntimeError in delete_keys
    In the else block of delete_keys, the code iterates over selected_keys and deletes items from the dictionary. If a user passes a live view (like my_dict.keys()), Python will throw a RuntimeError because the dictionary size changes during iteration.

Fix: It’s safer to wrap selected_keys in a list() or set() to ensure we’re iterating over a static snapshot.

  1. Brittle sequence/dict conversions

In seq_of_dict_to_dict_of_seq, the code assumes all dictionaries have the same keys as the first one. If they don't, it'll either raise a KeyError or produce mismatched list lengths.
In dict_of_seq_to_seq_of_dict, passing an empty dictionary causes next(iter(values.keys())) to raise a StopIteration error.

Fix: Adding a quick check for empty inputs and ensuring key consistency would make this much more robust.

  1. Implicit data loss in rename_keys
    The logic for handling collisions in rename_keys (via the omit set) is clever, but it might surprise users by silently dropping data if they aren't careful with their mapping.

Fix: Maybe a simple warning or just a clearer docstring note about how collisions are handled would help.

I've already played around with some fixes for these. Would you be open to a PR?

Best, Salim

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions