-
Notifications
You must be signed in to change notification settings - Fork 378
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
Dataclasses - set all DCs as frozen #4018
Comments
FYI: about 3rd point: attrs's recommends using |
Yeah as @andrevmatos said this is recommended even by the data classes docs. I do the exact same thing in Rotkehlchen here in an in-progress branch: https://github.com/LefterisJP/rotkehlchen/blob/af9cffdfa20c391fd3ff7b8c075cd61f86cb5e6c/rotkehlchen/assets/asset.py#L124-L138 For the attributes of the data class that should not be set by init you also gotta assign it to |
@rakanalh I think most dataclasses are frozen right now, no? |
Not really, most of them are the non-frozen. |
Indeed, was looking at only a subset of files. I started taking a look at this issue and doing some initial work. So one question here, do we really want to just blindly set all dataclasses to frozen even if the instances of the dataclasses are mutated often? Like with the pathfinding IOUs? raiden/raiden/network/pathfinding.py Line 67 in 8821730
|
I also started working on this a while ago and stopped because |
It's not hard to change that. You just return a mutated object, for example at the IOU's The question I have is basically does it make sense? Why do this? If a particular dataclass is used as immutable then should we force the And if it makes no sense let's just modify the issue to be to set |
The general idea we want to achive here is have immutable objects especially in the state machine where any mutations would be performed on a copy of the object and this copy replaces the old value after the mutation happens. The gain would be:
|
So how will the new approach work though? At the moment we make a copy of the current state, name it next state and modify it during the state transition. If we don't do that and all state classes end up being immutable we would need to be making multiple copies of each state object instance for each time any attribute is modified. So in a really deep state transition call from a state change that touches a lot of parts of the state we would make many many copies each time any state class attribute needs to be modified. Is that a good idea? What is the benefit of that?
I think that in this sentence you are actually answering my question from above but I can't understand it. Can you maybe explain the logic here a bit in more detail?
So if it's not the state machine and the dataclass does not need to be immutable should we still enforce
|
We use plenty of frozen dataclasses now:
There's certainly room for more optimization, but we should do that after spotting bottlenecks in a benchmark. |
This is a follow up issue to #3869, #3253 and PR #3969.
TODO
frozen
on all dataclassesdataclasses.replace
to set create a new dataclasses with a mutated value.__post_init__
will reject setting values, so find a way to overcome this.The text was updated successfully, but these errors were encountered: