-
Notifications
You must be signed in to change notification settings - Fork 26
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
Allow reassigning and resetting AbstractContainer attributes/fields #868
base: dev
Are you sure you want to change the base?
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## dev #868 +/- ##
==========================================
+ Coverage 88.33% 88.39% +0.06%
==========================================
Files 45 45
Lines 9283 9288 +5
Branches 2651 2652 +1
==========================================
+ Hits 8200 8210 +10
+ Misses 765 762 -3
+ Partials 318 316 -2
☔ View full report in Codecov by Sentry. |
PyNWB tests need to be updated because container attributes that were previously set to None resulted in no entry in
The attributes marked with a I made this change to allow users to reset an attribute to None and make the behavior not dependent on the current state of the container. What do you think? Is this a worthwhile change? If not, I can add logic in the setters that says if the value is None AND the field is not set, do nothing and return. |
Thanks for tackling this, @rly ! I'm testing incorporation with catalystneuro/neuroconv#475 now.. |
While I like the ability to change values, I think too much freedom could be confusing for users. For instance, now a user is able to run: from pynwb.testing.mock.base import mock_TimeSeries
ts = mock_TimeSeries(timestamps=[1,2,3,4], rate=None)
ts.rate = 5 This will create a Would it be possible to perform validation every time a |
@bendichter Good point. Yes, we can do that - we can call a generic validation function in the generic setter. Moving all validation in PyNWB classes to that function and making it work for auto-generated classes will take some more work though, but I think ultimately this would be a large improvement in usability. For now, what if we constrain the changes in this PR to allow reassigning only |
Sounds good to me! |
wait, no, I need to overwrite from pynwb.testing.mock.base import mock_TimeSeries
ts = mock_TimeSeries(timestamps=[1,2,3,4], rate=None)
ts.rate = 5 # this works
ts.data = [2,3,4,5] # this does not work ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In [6], line 7
5 ts = mock_TimeSeries(timestamps=[1,2,3,4], rate=None)
6 ts.rate = 5
----> 7 ts.data = [2,3,4,5]
AttributeError: can't set attribute |
Note: Now that the IO object is available on Container objects, for attributes, we may be able to easily modify any written attributes. We would need to store the path to the attribute and have a setter that detects if it was read from a file, the file is writeable, and then go to the object, and overwrite the value of the attribute. |
Motivation
Fix #411, fix #865
Major change to allow setting attributes on Container objects to new values, including None. If the attributes were read from a file (i.e.
container_source is not None
), then a warning is raised.Note that we currently do a lot of validation at the constructor level (type validation, shape validation) in docval. We do not do that for each setter. This can result in errors on write if the user is not careful. We could instead move validation of attributes to outside of docval and into the setter. That is significantly more development, but I think worthwhile in the long run. I will open a separate issue for this.
Checklist
ruff
from the source directory.