-
Notifications
You must be signed in to change notification settings - Fork 192
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
List
/Dict
: Add value
property
#5297
Conversation
Add the `value` property to the `List` and `Dict` classes, to make them more consistent with other base type classes.
Codecov Report
@@ Coverage Diff @@
## develop #5297 +/- ##
===========================================
- Coverage 81.51% 81.51% -0.00%
===========================================
Files 530 530
Lines 37103 37111 +8
===========================================
+ Hits 30242 30246 +4
- Misses 6861 6865 +4
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
I've left the PR as a draft since I'm not sure if I can replicate the behaviour of In [1]: i = Int(2)
In [2]: i
Out[2]: <Int: uuid: 3ef91127-7fb4-47b4-9a7f-57b5a1ede86a (unstored) value: 2>
In [3]: i.value = 4
In [4]: i
Out[4]: <Int: uuid: 3ef91127-7fb4-47b4-9a7f-57b5a1ede86a (unstored) value: 4>
In [5]: i.store()
Out[5]: <Int: uuid: 3ef91127-7fb4-47b4-9a7f-57b5a1ede86a (pk: 18) value: 4>
In [6]: i.value = 6
[...] Removed for brevity
ModificationNotAllowed: the attributes of a stored entity are immutable Is replicated for the In [1]: l = List([1, 2])
In [2]: l
Out[2]: <List: uuid: 77e96841-285f-4d19-8802-09b7d0110f13 (unstored) value: [1, 2]>
In [3]: l.value = [3, 4]
In [4]: l
Out[4]: <List: uuid: 77e96841-285f-4d19-8802-09b7d0110f13 (unstored) value: [3, 4]>
In [5]: l.store()
Out[5]: <List: uuid: 77e96841-285f-4d19-8802-09b7d0110f13 (pk: 19) value: [3, 4]>
In [6]: l.value = [5, 6]
[...] Removed for brevity
ModificationNotAllowed: the attributes of a stored entity are immutable However, the behaviour of modifying the content through the In [1]: l = List([1, 2])
In [2]: l
Out[2]: <List: uuid: b39a4626-d1d0-43f0-8c41-857677dc8ce9 (unstored) value: [1, 2]>
In [3]: l.value.append(3)
In [4]: l
Out[4]: <List: uuid: b39a4626-d1d0-43f0-8c41-857677dc8ce9 (unstored) value: [1, 2, 3]>
In [5]: l.store()
Out[5]: <List: uuid: b39a4626-d1d0-43f0-8c41-857677dc8ce9 (pk: 21) value: [1, 2, 3]>
In [6]: l.value.append(4)
In [7]: l
Out[7]: <List: uuid: b39a4626-d1d0-43f0-8c41-857677dc8ce9 (pk: 21) value: [1, 2, 3]> I.e. if the node is unstored, the I think this behaviour isn't really inconsistent with the other base types in a sense. As far as I know, the other base types don't have methods that modify the content, so there isn't really any behaviour to replicate. Still, the behaviour is inconsistent with how the content is set using the The current behaviour when modifying through the |
Indeed, I don't think there is an easy workaround for this. Lists and dicts are pointers, so they have a different behaviour, sometimes unexpected (see e.g. when setting an empty list as the default value of a function!). Also other languages like JS have similar problems (see e.g. frameworks like vue.js or react, where they have to resort to just documenting the behaviour that changing dicts or lists should not be directly accessed to change them, but you should use special setter/getter methods, otherwise the library does not know that a change happened. The only other option (that I wouldn't do) would be to return a subclass of list when the node is stored, that overrides certain methods. But I feel this is way too complex and error-prone to be worth it. So, there are two options:
At this point, I would probably vote for 1 - we've been living without .value for Dict and List and this is I think OK. So I would close this, and instead fix the original issue by adding a paragraph about this:
|
I doubt this is possible because what you return is a I guess that ultimately users will always have to learn about the concept of mutability in Python. So no matter whether you get the value through a property
seems more natural and like it would work, compared to
although I bet that there will be many people that are not intimately familiar with Python that will expect the second to also update the content of the node. So I personally think it is still fine to add the properties as there will always be some particular behavior that is not immediately obvious to 100% of users. But then again, I don't think it is crucial either, so fine to not put it in. |
Will close this PR for now, we can continue the discussion in #5313. I'm still in favour of not adding |
Fixes #4495Fixes #5313
Add the
value
property to theList
andDict
classes, to make themmore consistent with other base type classes.