Frozen dict cannot use flatten, unflatten, subset or filter
维护者通常 2 天内回复
已经有一个关联 PR 被合并了。
- #614 来自 @itzzdev09 —— 已合并
评估
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Python
- 星标
- 1.6k
- 派生
- 63
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 21
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
fabiocaccamo/python-benedict 的其他 Issue
-
难度 3/5 半天 新手友好度 62/100
fabiocaccamo/python-benedict#634 ·
维护者通常 2 天内回复
-
Take over the BeneDict name on PyPI可能重新可做 @fabiocaccamo 于 693 天前认领,目前没有进行中的 PR。 未关闭enhancement
fabiocaccamo/python-benedict#458 · 2 条评论 · 2 个 reaction · 已指派 1 人 ·
维护者通常 2 天内回复
-
Add possibility to use `requests-html` when calling `from_html` method.可能重新可做 @fabiocaccamo 于 826 天前认领,目前没有进行中的 PR。 未关闭enhancement
fabiocaccamo/python-benedict#425 · 已指派 1 人 ·
维护者通常 2 天内回复
-
Wildcard support for renaming可能已有人在做 @fabiocaccamo 于 1494 天前认领。 未关闭enhancement
fabiocaccamo/python-benedict#113 · 12 条评论 · 1 个 reaction · 已指派 1 人 ·
维护者通常 2 天内回复
-
Add `xpath` support.可能重新可做 @fabiocaccamo 于 1839 天前认领,目前没有进行中的 PR。 未关闭enhancement
fabiocaccamo/python-benedict#67 · 3 条评论 · 已指派 1 人 ·
维护者通常 2 天内回复
查看 fabiocaccamo/python-benedict 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
huggingface/sentence-transformers#4186 ·
维护者通常 5 天内回复
-
documentation good first issue
难度 2/5 半天 新手友好度 72/100
sujithputta02/Esapay#13 ·
-
bot-triaged module: correctness (silent) module: mps module: NaNs and Infs triage review
难度 2/5 半天 新手友好度 62/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
SCIInstitute/SCIRun#2842 ·
维护者通常 3 天内回复