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

♻️ REFACTOR: EntityAttributesMixin -> NodeAttributes #5442

Merged
merged 7 commits into from
Apr 8, 2022

Conversation

chrisjsewell
Copy link
Member

@chrisjsewell chrisjsewell commented Mar 14, 2022

Part of addressing #4976 and https://github.com/aiidateam/aiida-core/discussions/5465

Similar to #5439, it moves the node's attributes methods to a separate "namespace", in a back compatible manner, and is another step towards #4976 (comment): removing another 10 methods (i.e. #5439 + #5442, goes from 83 -> 58)

the following deprecations are made:

    _deprecated_attr_methods = {
        'attributes': 'all',
        'get_attribute': 'get',
        'get_attribute_many': 'get_many',
        'set_attribute': 'set',
        'set_attribute_many': 'set_many',
        'reset_attributes': 'reset',
        'delete_attribute': 'delete',
        'delete_attribute_many': 'delete_many',
        'clear_attributes': 'clear',
        'attributes_items': 'items',
        'attributes_keys': 'keys',
    }

e.g. Node.get_attribute to Node.base.attributes.get

It also actually fixes a bug with the Sealable mixin of ProcessNode, whereby it only overrode set_attribute and delete_attribute, to change mutability behaviour, but not any other mutation methods, like set_attribute_many, etc

@chrisjsewell chrisjsewell changed the title ♻️ REFACTOR: NodeRepositoryMixin -> NodeRepository ♻️ REFACTOR: EntityAttributesMixin -> NodeAttributes Mar 14, 2022
@chrisjsewell chrisjsewell force-pushed the orm-node-attributes branch 2 times, most recently from e71d94d to 5e8f824 Compare March 14, 2022 22:20
@ramirezfranciscof ramirezfranciscof added this to the v2.0.0 milestone Apr 6, 2022
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 @chrisjsewell , just some minor things.

aiida/orm/nodes/attributes.py Outdated Show resolved Hide resolved
Comment on lines 35 to 36
self._entity = node
self._backend_entity = node.backend_entity
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to keep the name entity here when it only ever applies to a node? Would _node and _backend_node not be clearer?

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Comment on lines 38 to 40
def __contains__(self, key: str) -> bool:
"""Check if the node contains an attribute with the given key."""
return key in self._backend_entity.attributes
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an addition I think? Don't think I have seen an added test for this, correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've removed the method for now

Copy link
Member Author

Choose a reason for hiding this comment

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

re-instated because there is actually a test, hence why they were failing

Copy link
Member Author

Choose a reason for hiding this comment

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

Note @sphuber, it's my intention to look into making attributes and extras a subclass of MutableMapping (https://docs.python.org/3/library/collections.abc.html?highlight=mutablemapping#collections-abstract-base-classes), which would actually remove this method.

But this change is just additive, i.e. is just adding extra methods and not breaking anything. So don't want to hold up this initial re-structuring with it

aiida/orm/nodes/node.py Outdated Show resolved Hide resolved
@@ -194,6 +200,16 @@ def base(self) -> NodeBase:
"""Return the node base namespace."""
return NodeBase(self)

def _check_mutability_attributes(self, keys: Optional[List[str]] = None) -> None: # pylint: disable=unused-argument
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need an explicit method for mutability of attributes? I don't think there are degrees of mutabillity are there? If it doesn't already exist, I would just have a _check_mutability. And really, since you are calling it from outside of the class, it should be public. I know that you probably do this to keep the namespace clean, but this seems to be a fundamental property. Alternatively, put it in Node.base.check_mutability?

I see now, this is to do with the Sealable aspect. I also notice that you essentially fixed a bug. I think that set_attribute_many would have been possible on sealed nodes because Sealable didn't override that method. Same goes for delete_attribute_many. Anyway, I don't have a better solution for this at this point, so fine to keep for now.

chrisjsewell and others added 2 commits April 7, 2022 11:40
Co-authored-by: Sebastiaan Huber <mail@sphuber.net>
@sphuber
Copy link
Contributor

sphuber commented Apr 7, 2022

@chrisjsewell just to let you know that tests are failing, so cannot merge yet

@sphuber
Copy link
Contributor

sphuber commented Apr 7, 2022

You forgot to apply the backend_entity -> backend_node in __contains__ I think @chrisjsewell

@chrisjsewell
Copy link
Member Author

You forgot to apply the backend_entity -> backend_node in contains I think @chrisjsewell

Third times a charm 🤞

@chrisjsewell
Copy link
Member Author

All tests passing @sphuber

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.

There are still uses of the deprecated methods:

 tests/tools/archive/test_complex.py::test_reexport
tests/tools/archive/test_complex.py::test_reexport
tests/tools/archive/test_complex.py::test_reexport
tests/tools/archive/test_complex.py::test_reexport
  /home/runner/work/aiida-core/aiida-core/tests/tools/archive/test_complex.py:181: AiidaDeprecationWarning: `Dict.attributes` is deprecated, use `Dict.base.attributes.all` instead. (this will be removed in v3)
    item['param']['*'].attributes,

tests/tools/archive/test_complex.py::test_reexport
tests/tools/archive/test_complex.py::test_reexport
tests/tools/archive/test_complex.py::test_reexport
tests/tools/archive/test_complex.py::test_reexport
  /home/runner/work/aiida-core/aiida-core/tests/tools/archive/test_complex.py:186: AiidaDeprecationWarning: `CalculationNode.attributes` is deprecated, use `CalculationNode.base.attributes.all` instead. (this will be removed in v3)
    item['calc']['*'].attributes,

tests/tools/archive/test_complex.py::test_reexport
tests/tools/archive/test_complex.py::test_reexport
tests/tools/archive/test_complex.py::test_reexport
tests/tools/archive/test_complex.py::test_reexport
  /home/runner/work/aiida-core/aiida-core/tests/tools/archive/test_complex.py:187: AiidaDeprecationWarning: `ArrayData.attributes` is deprecated, use `ArrayData.base.attributes.all` instead. (this will be removed in v3)
    item['array']['*'].attributes,

tests/tools/archive/test_simple.py::test_calc_of_structuredata
  /home/runner/work/aiida-core/aiida-core/tests/tools/archive/test_simple.py:66: AiidaDeprecationWarning: `StructureData.attributes` is deprecated, use `StructureData.base.attributes.all` instead. (this will be removed in v3)
    for k in node.attributes.keys():

tests/tools/archive/test_simple.py::test_calc_of_structuredata
  /home/runner/work/aiida-core/aiida-core/tests/tools/archive/test_simple.py:66: AiidaDeprecationWarning: `CalcJobNode.attributes` is deprecated, use `CalcJobNode.base.attributes.all` instead. (this will be removed in v3)
    for k in node.attributes.keys():

tests/tools/archive/test_specific_import.py::test_cycle_structure_data
  /home/runner/work/aiida-core/aiida-core/tests/tools/archive/test_specific_import.py:124: AiidaDeprecationWarning: `CalculationNode.attributes` is deprecated, use `CalculationNode.base.attributes.all` instead. (this will be removed in v3)
    assert isinstance(calculation.attributes, dict)

tests/tools/archive/test_specific_import.py::test_cycle_structure_data
  /home/runner/work/aiida-core/aiida-core/tests/tools/archive/test_specific_import.py:125: AiidaDeprecationWarning: `CalculationNode.attributes` is deprecated, use `CalculationNode.base.attributes.all` instead. (this will be removed in v3)
    assert len(calculation.attributes) != 0

tests/tools/groups/test_paths.py::test_walk_nodes
  /home/runner/work/aiida-core/aiida-core/tests/tools/groups/test_paths.py:137: AiidaDeprecationWarning: `Data.attributes` is deprecated, use `Data.base.attributes.all` instead. (this will be removed in v3)
    assert [(r.group_path.path, r.node.attributes) for r in group_path.walk_nodes()] == [('a', {'i': 1, 'j': 2})]

Would be good to fix those, then we can merge

@chrisjsewell
Copy link
Member Author

There are still uses of the deprecated methods:

How helpful are those deprecation warnings though 😉

@sphuber
Copy link
Contributor

sphuber commented Apr 7, 2022

How helpful are those deprecation warnings though 😉

very 👍

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 @chrisjsewell good to go

@chrisjsewell chrisjsewell merged commit 8bb6c3f into aiidateam:develop Apr 8, 2022
@chrisjsewell chrisjsewell deleted the orm-node-attributes branch April 8, 2022 08:32
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.

3 participants