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

New item/attr setter for Dict nodes #4351

Merged
merged 3 commits into from
Sep 3, 2020

Conversation

ramirezfranciscof
Copy link
Member

Fixes #2883

The following two ways for setting (key:val) pairs in the dicts stored inside a Dict node were failing (the first one silently, the second one with an error message) but are now valid:

dict_node = Dict()
dict_node.dict.x = 'Set a value for x'
dict_node['x'] = 'Overwrite the value for x'

The following two ways for setting (key:val) pairs in the dicts stored
inside a Dict node were failing (the first one silently, the second one
with an error message) but now are valid:

dict_node = Dict()
dict_node.dict.x = 'Set a value for x'
dict_node['x'] = 'Overwrite the value for x'
@codecov
Copy link

codecov bot commented Sep 3, 2020

Codecov Report

Merging #4351 into develop will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #4351      +/-   ##
===========================================
- Coverage    79.15%   79.15%   -0.00%     
===========================================
  Files          468      468              
  Lines        34616    34620       +4     
===========================================
+ Hits         27397    27399       +2     
- Misses        7219     7221       +2     
Flag Coverage Δ
#django 72.77% <100.00%> (+0.01%) ⬆️
#sqlalchemy 71.96% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiida/orm/nodes/data/dict.py 85.00% <100.00%> (+0.79%) ⬆️
aiida/orm/utils/managers.py 92.07% <100.00%> (+0.27%) ⬆️
aiida/engine/daemon/runner.py 79.32% <0.00%> (-3.44%) ⬇️
aiida/transports/plugins/local.py 81.29% <0.00%> (-0.25%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0f0dda4...9884b41. Read the comment docs.

Copy link
Contributor

@sphuber sphuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ramirezfranciscof . Have some suggestions

aiida/orm/utils/managers.py Outdated Show resolved Hide resolved
aiida/orm/utils/managers.py Show resolved Hide resolved
tests/orm/data/test_dict.py Outdated Show resolved Hide resolved
tests/orm/data/test_dict.py Outdated Show resolved Hide resolved
Comment on lines 24 to 26
The dictionary key-value pairs are stored in the database in a column named `attributes`.
We will then say that these are "database-attributes", or "attributes in a database sense".
However, these are not "python-attributes" ("attributes in the python sense"), and can only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I see the point of adding this, given that this was not clear for you either, this does not just apply to the Dict nodes, but to all nodes. All node specific data are stored as attributes in the database. So although this may help clear things up, it may also confuse, since it may suggest that this is behavior specific to the Dict plugin.

I would propose a text something like:

    The dictionary contents of a `Dict` node are stored in the database as attributes. The dictionary can be initialized
    through the `dict` argument in the constructor. After construction, values can be retrieved and updated through the
    item getters and setters, respectively:

        node['key'] = 'value'

    Alternatively, the `dict` property returns an instance of the `AttributeManager` that can be used to get and set
    values through attribute notation:

        node.dict.key = 'value'

    Note that trying to set dictionary values directly on the node, e.g. `node.key = value`, will not work as intended.
    It will merely set the `key` attribute on the node instance, but will not be stored in the database. As soon as the
    node goes out of scope, the value will be lost.

    Finally, all dictionary mutations will be forbidden once the node is stored.

Copy link
Member Author

@ramirezfranciscof ramirezfranciscof Sep 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, would it still be ok to add something like this between the last two paragraphs?

    node goes out of scope, the value will be lost.

    Note that here it becomes apparent the difference in something being an "attribute of a node" (in
    the sense that it is stored in the "attribute" column of the database when the node is stored) and
    something being an "attribute of a python object" (in the sense of being able to modify and access
    it as if it was a property of the variable, e.g. `node.key = value`). This is true of all types of
    nodes, but it becomes more relevant for `Dict` nodes where one is constantly manipulating these
    attributes.

    Finally, all dictionary mutations will be forbidden once the node is stored.

Co-authored-by: Sebastiaan Huber <mail@sphuber.net>
@sphuber sphuber merged commit 8f4eb96 into aiidateam:develop Sep 3, 2020
@sphuber sphuber deleted the issue2883 branch September 3, 2020 19:32
@sphuber
Copy link
Contributor

sphuber commented Sep 3, 2020

Thanks a lot @ramirezfranciscof !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setting attributes for unstored Dict nodes fails silently
2 participants