Frozen dict cannot use flatten, unflatten, subset or filter
Maintainer thường phản hồi trong vòng 2 ngày
Một pull request liên quan đã được merge.
- #614 của @itzzdev09 — đã merge
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 78/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Python
- Star
- 1.6k
- Fork
- 63
- Merge trung bình
- 1 ngày 23 giờ
- Pull request đã merge (30 ngày)
- 21
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của fabiocaccamo/python-benedict
-
Incompatibility with `toml-rs`Có thể đã có người làm @fabiocaccamo đã nhận hôm nay. Đang mởbug
fabiocaccamo/python-benedict#636 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Frozen benedict allows deleting nested keys through root keypath APIsCó thể đã có người làm @helo060228 đã nhận 1 ngày trước. Đang mở
Độ khó 3/5 Nửa ngày Mức phù hợp với người mới 62/100
fabiocaccamo/python-benedict#634 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Take over the BeneDict name on PyPICó thể làm lại được @fabiocaccamo đã nhận 694 ngày trước và không có pull request nào đang mở. Đang mởenhancement
fabiocaccamo/python-benedict#458 · 2 bình luận · 2 reaction · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Add possibility to use `requests-html` when calling `from_html` method.Có thể làm lại được @fabiocaccamo đã nhận 828 ngày trước và không có pull request nào đang mở. Đang mởenhancement
fabiocaccamo/python-benedict#425 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Wildcard support for renamingCó thể đã có người làm @fabiocaccamo đã nhận 1495 ngày trước. Đang mởenhancement
fabiocaccamo/python-benedict#113 · 12 bình luận · 1 reaction · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của fabiocaccamo/python-benedict
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
ok-oldking/ok-wuthering-waves#1785 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
healthchecks/healthchecks#1349 ·
-
deployment release-lag
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
nolte/kamerplanter#2374 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug v1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
modelcontextprotocol/python-sdk#3725 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
batchterms() drops queries without a similar() clause, misaligning results with inputsCó thể đã có người làm @MohammadHijjawi97 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
Maintainer thường phản hồi trong vòng 1 ngày