Frozen dict cannot use flatten, unflatten, subset or filter
Los mantenedores suelen responder en 2 días
Ya se ha fusionado un pull request relacionado.
- #614 de @itzzdev09 — fusionado
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 78/100
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 1.6k
- Forks
- 67
- Merge medio
- 2 d 5 h
- PR fusionados (30 d)
- 27
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de fabiocaccamo/python-benedict
-
Take over the BeneDict name on PyPIQuizá libre de nuevo @fabiocaccamo la tomó hace 699 días y no hay ningún pull request abierto. Abiertoenhancement
fabiocaccamo/python-benedict#458 · 2 comentarios · 2 reacciones · 1 asignado ·
Los mantenedores suelen responder en 2 días
-
Add possibility to use `requests-html` when calling `from_html` method.Quizá libre de nuevo @fabiocaccamo la tomó hace 832 días y no hay ningún pull request abierto. Abiertoenhancement
fabiocaccamo/python-benedict#425 · 1 asignado ·
Los mantenedores suelen responder en 2 días
-
Wildcard support for renamingPosiblemente ocupada @fabiocaccamo la tomó hace 1499 días. Abiertoenhancement
fabiocaccamo/python-benedict#113 · 12 comentarios · 1 reacción · 1 asignado ·
Los mantenedores suelen responder en 2 días
-
Add `xpath` support.Quizá libre de nuevo @fabiocaccamo la tomó hace 1845 días y no hay ningún pull request abierto. Abiertoenhancement
fabiocaccamo/python-benedict#67 · 3 comentarios · 1 asignado ·
Los mantenedores suelen responder en 2 días
Todos los issues de fabiocaccamo/python-benedict
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
pgvector/pgvector-python#169 ·
-
ChaosEngine: backup-remote doctor row passes while consumer mode hides bot memory from gitPosiblemente ocupada @MohabMohie la tomó hoy. Abiertoagentic
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
ShaftHQ/SHAFT_ENGINE#7263 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 87/100
huggingface/smolagents#3045 ·
Los mantenedores suelen responder en 1 día