Robustness improvements for _dict_utils.py: Handling edge cases and preventing RuntimeErrors
- Dominant language
- Python
- Stars
- 78
- Forks
- 9
- PR merge metrics
- No merged PRs in 30d
Description
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.
2. 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.
3. 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
Contributor guide
Research direction
Read _dict_utils.py and inspect delete_keys, seq_of_dict_to_dict_of_seq, dict_of_seq_to_seq_of_dict, and rename_keys. Reproduce the live-view, inconsistent-key, empty-input, and collision cases described in the issue, then confirm the chosen behavior is documented and the edge cases behave consistently without unexpected runtime errors or silent data loss.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100