Frozen dict cannot use flatten, unflatten, subset or filter
I maintainer di solito rispondono entro 2 giorni
Una pull request collegata è già stata integrata.
- #614 di @itzzdev09 — integrata
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 78/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Python
- Stelle
- 1.6k
- Fork
- 63
- Merge medio
- 1g 23h
- PR unite (30g)
- 21
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di fabiocaccamo/python-benedict
-
Difficoltà 3/5 Mezza giornata Idoneità per principianti 62/100
fabiocaccamo/python-benedict#634 ·
I maintainer di solito rispondono entro 2 giorni
-
Take over the BeneDict name on PyPIForse di nuovo libera @fabiocaccamo l’ha presa 693 giorni fa e non c’è nessuna pull request aperta. Apertaenhancement
fabiocaccamo/python-benedict#458 · 2 commenti · 2 reazioni · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Add possibility to use `requests-html` when calling `from_html` method.Forse di nuovo libera @fabiocaccamo l’ha presa 827 giorni fa e non c’è nessuna pull request aperta. Apertaenhancement
fabiocaccamo/python-benedict#425 · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Wildcard support for renamingForse già presa @fabiocaccamo l’ha presa 1494 giorni fa. Apertaenhancement
fabiocaccamo/python-benedict#113 · 12 commenti · 1 reazione · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Add `xpath` support.Forse di nuovo libera @fabiocaccamo l’ha presa 1839 giorni fa e non c’è nessuna pull request aperta. Apertaenhancement
fabiocaccamo/python-benedict#67 · 3 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di fabiocaccamo/python-benedict
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
huggingface/sentence-transformers#4186 ·
I maintainer di solito rispondono entro 5 giorni
-
documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
QuantEcon/QuantEcon.py#988 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
fabriziosalmi/certmate#1247 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
documentation good first issue
Difficoltà 2/5 Mezza giornata Idoneità per principianti 72/100
sujithputta02/Esapay#13 ·