-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add node_value
& get_node_value
to TaskSocket
#299
Conversation
This is basically just syntactic sugar to directly access the associated Python variable of a `Socket`, avoiding `.value.value` constructs. A few notes regarding the implementation: - I'm checking first the nested case, as the non-nested one should always match, and, in the case of an AiiDA ORM type, return the AiiDA ORM variable, not its value - I implemented it in `TaskProperty` not `SocketAny`, as all sockets inherit from `TaskProperty` but not `SocketAny` - In the future, when we (re-)introduce the `SocketAiiDADict`, we can overwrite the method in this specific class, using `get_dict` rather than `.value.value` - Do we even need to provide a `property`? We can just provide the `get_node_value` method? The `property` is useful for caching, though. - One could use try-except to avoid writing the attribute that is being checked as string which can be a pain for later refactoring. However, for now I think the `hasattr` call reads cleaner - In the parametrization of the tests, passing AiiDA ORM types would require loading a profile (passing the `aiida_profile` fixture) doesn't work, so I'm just type-hinting AiiDA ORM types, but passing the Python types, which still creates the correct sockets, but maybe there's a better solution?
Hi @GeigerJ2 , thanks for the work!
I think you mean
I think we need it, because it's convenient.
I don't have a solution either 😢 . However, since the value will be converted to an orm.Data object inside the socket, the test successfully achieves its intended goal. I also have a few comments on the code:
|
Cheers!
That was indeed a spelling mistake, my bad!
Yeah, apart from the shorter name, the idea was also to store it in the private
That's not possible when only implemented in
OK!
The thing is, if implementing only in In my last commit, the implementation is as you propose. For me, that's fine to get merged. Locally, I have another version where I define a from aiida import orm
class SocketBase(TaskSocket):
def get_node_value(self):
"Obtain the value of the associated data of a `Socket`."
if isinstance(self.value, orm.Data):
if hasattr(self.value, "value"):
return self.value.value
else:
raise ValueError(
"Data node does not have a value attribute. We do not know how to extract the raw Python value."
)
else:
return self.value
class SocketAny(SocketBase, SerializePickle):
"""Any socket."""
...
class SocketFloat(SocketBase, SerializeJson):
"""Float socket."""
...
class SocketAiiDAFloat(SocketBase, SerializeJson):
"""AiiDAFloat socket."""
... Though, not sure if that's useful (beyond the current case) or even desired design, so I didn't commit it. |
I see. Let's make it simple by moving the |
cbdefd5
to
849032b
Compare
849032b
to
6394819
Compare
My saying :D OK, I reverted the latest commit, and updated the implementation. Now it should be ready to merge. Could you please give it a final review? |
a0f64e1
to
6394819
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Add `node_value` & `get_node_value` to `TaskSocket`. This is basically just syntactic sugar to directly access the associated raw Python value of a `Socket` which has a orm.Data as its value. Therefore, we can avoid, e.g., `.value.value`.
This is basically just syntactic sugar to directly access the associated Python variable of a
Socket
, avoiding.value.value
constructs.Fixes #293.
A few notes regarding the implementation:
TaskProperty
notSocketAny
, as all sockets inherit fromTaskProperty
but notSocketAny
SocketAiiDADict
, we can overwrite the method in this specific class, usingget_dict
rather than.value.value
property
? We can just provide theget_node_value
method? Theproperty
is useful for caching, though.hasattr
call reads cleaneraiida_profile
fixture doesn't work), so I'm just type-hinting AiiDA ORM types, but passing the Python types, which still creates the correct sockets, but maybe there's a better solution?