Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👌 Return frozendict for stored Dict nodes #6185

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion aiida/orm/nodes/data/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"""`Data` sub class to represent a dictionary."""
import copy

from frozendict import frozendict

from aiida.common import exceptions

from .base import to_aiida_type
Expand Down Expand Up @@ -62,15 +64,28 @@ def __init__(self, value=None, **kwargs):
self.set_dict(dictionary)

def __getitem__(self, key):
"""Get the ``value`` of a key.

If the node is not stored, the value is returned as is (if present). Once the node is stored, we check if the
value is a dictionary and if so, we return a ``frozendict`` instance to prevent modifications. This is to
prevent the user from modifying the dictionary in place, which would not be reflected in the database.
"""
try:
return self.base.attributes.get(key)
value = self.base.attributes.get(key)
except AttributeError as exc:
raise KeyError from exc

if self.is_stored and isinstance(value, dict):
return frozendict(value)

return value

def __setitem__(self, key, value):
"""Set the ``value`` of a key."""
self.base.attributes.set(key, value)

def __eq__(self, other):
"""Compare if the current node is equal to another node or to a dictionary."""
if isinstance(other, Dict):
return self.get_dict() == other.get_dict()
return self.get_dict() == other
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
- click~=8.1
- disk-objectstore~=1.0
- docstring_parser
- frozendict~=2.3
- get-annotations~=0.1
- python-graphviz~=0.19
- ipython>=7
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"click~=8.1",
"disk-objectstore~=1.0",
"docstring-parser",
"frozendict~=2.3",

Check warning on line 36 in pyproject.toml

View workflow job for this annotation

GitHub Actions / check-requirements

No match for dependency 'frozendict~=2.3' in: requirements/requirements-py-3.9.txt,requirements/requirements-py-3.11.txt,requirements/requirements-py-3.12.txt,requirements/requirements-py-3.10.txt

Check warning on line 36 in pyproject.toml

View workflow job for this annotation

GitHub Actions / validate-dependency-specification

No match for dependency 'frozendict~=2.3' in: requirements/requirements-py-3.10.txt,requirements/requirements-py-3.11.txt,requirements/requirements-py-3.12.txt,requirements/requirements-py-3.9.txt
"get-annotations~=0.1;python_version<'3.10'",
"graphviz~=0.19",
"ipython>=7",
Expand Down
Loading