Frozen dict cannot use flatten, unflatten, subset or filter
Maintainers usually reply within 2 days
A pull request for this has already been merged.
- #614 by @itzzdev09 — merged
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 78/100
Research direction
Start with core/clone.py, especially the empty=True path and BaseDict.deepcopy, then inspect the affected call sites in core/filter.py, core/flatten.py, core/invert.py, core/subset.py, and core/unflatten.py. Done means these derivation methods work on frozen dictionaries without modifying the source, while ordinary clone() remains frozen.
Written by the indexing model from the issue text.
Description
Summary
flatten, unflatten, subset and filter raise TypeError on a frozen dict, although all four are documented as returning a new dict and none of them mutate the source.
from benedict import benedict
d = benedict({"a": 1, "nested": {"x": 1}}).freeze()
d.flatten()
# TypeError: 'benedict' object is frozen and cannot be modified.
clone, keypaths, search, match, groupby and to_json all work fine on the same frozen dict, so the behaviour is inconsistent — freezing blocks some read-only derivations but not others.
| method | mutates source | on a frozen dict |
|---|---|---|
clone |
no | works |
keypaths, search, match, groupby, to_json |
no | works |
flatten |
no | TypeError |
unflatten |
no | TypeError |
subset |
no | TypeError |
filter |
no | TypeError |
Cause
All four build their result with clone(d, empty=True). In benedict/core/clone.py:
d = copy.deepcopy(obj, memo)
if empty and isinstance(d, MutableMapping):
d.clear()
return d
BaseDict.__deepcopy__ deliberately carries the frozen flag over:
if self._frozen:
obj.freeze()
so the deep copy is frozen, and the following clear() hits _check_frozen() and raises. The exception therefore comes from the throwaway internal copy rather than from the dict the caller passed in — which is why the error message is a little misleading too.
Plain clone() is unaffected because it never clears.
Affected call sites: core/filter.py (x2), core/flatten.py, core/invert.py, core/subset.py, core/unflatten.py.
Expected
Freezing is meant to prevent modification of the dict. Deriving a new dict from it is a read, so it should be allowed, the way clone() already is. This matters for the obvious use of freeze() — holding an immutable config and taking subsets or flattened views of it.
Notes
Propagating frozen through __deepcopy__ looks intentional and worth keeping: a full clone() of an immutable dict staying immutable is reasonable. The issue is only the empty=True path, where the copy is emptied and refilled, so the source's frozen state has no meaning.
Reproduced on main at e9641ec, Python 3.12. PR follows.
- Dominant language
- Python
- Stars
- 1.6k
- Forks
- 63
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 21
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fabiocaccamo/python-benedict
-
Difficulty 3/5 Half a day Newbie friendliness 62/100
fabiocaccamo/python-benedict#634 ·
Maintainers usually reply within 2 days
-
Take over the BeneDict name on PyPIMay be free again @fabiocaccamo claimed this 693 days ago, and no pull request is open. Openenhancement
fabiocaccamo/python-benedict#458 · 2 comments · 2 reactions · 1 assignee ·
Maintainers usually reply within 2 days
-
Add possibility to use `requests-html` when calling `from_html` method.May be free again @fabiocaccamo claimed this 826 days ago, and no pull request is open. Openenhancement
fabiocaccamo/python-benedict#425 · 1 assignee ·
Maintainers usually reply within 2 days
-
Wildcard support for renamingPossibly taken @fabiocaccamo claimed this 1494 days ago. Openenhancement
fabiocaccamo/python-benedict#113 · 12 comments · 1 reaction · 1 assignee ·
Maintainers usually reply within 2 days
-
Add `xpath` support.May be free again @fabiocaccamo claimed this 1839 days ago, and no pull request is open. Openenhancement
fabiocaccamo/python-benedict#67 · 3 comments · 1 assignee ·
Maintainers usually reply within 2 days
All issues in fabiocaccamo/python-benedict
Similar issues
-
documentation good first issue
Difficulty 2/5 Half a day Newbie friendliness 72/100
sujithputta02/Esapay#13 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
gke-labs/kube-agents#3277 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
jbacalso24/mimry#291 ·
Maintainers usually reply within 1 day
-
good first issue
Difficulty 2/5 Under an hour Newbie friendliness 82/100
eli-alvarado/us-watchlist-alerts#14 ·
Maintainers usually reply within 1 day