python / cpython

The Python programming language
https://www.python.org
Other
63.36k stars 30.34k forks source link

Support immutability per-field in dataclasses #77655

Open d32e3dae-0148-4235-b30d-834c407f030f opened 6 years ago

d32e3dae-0148-4235-b30d-834c407f030f commented 6 years ago
BPO 33474
Nosy @ericvsmith

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = 'https://github.com/ericvsmith' closed_at = None created_at = labels = ['3.8', 'type-feature', 'library'] title = 'Support immutability per-field in dataclasses' updated_at = user = 'https://bugs.python.org/DanielLindeman' ``` bugs.python.org fields: ```python activity = actor = 'Daniel Lindeman' assignee = 'eric.smith' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'Daniel Lindeman' dependencies = [] files = [] hgrepos = [] issue_num = 33474 keywords = [] message_count = 3.0 messages = ['316438', '316458', '316619'] nosy_count = 2.0 nosy_names = ['eric.smith', 'Daniel Lindeman'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue33474' versions = ['Python 3.8'] ```

d32e3dae-0148-4235-b30d-834c407f030f commented 6 years ago

Currently, all fields in dataclasses can be frozen by passing frozen=True to the decorator. There are cases where it would be useful to have some fields be mutable, and others immutable. By using a strategy similar to how the decorator frozen works, using __setattr and __delattr, it would be possible to support immutability per-field. This could perhaps be done by adding a frozen argument to the field constructor.

ericvsmith commented 6 years ago

Can you explain your use case for this?

Also, how do you envision this working with the existing frozen, hashing, and equality behavior? There are a lot of interactions there, and we'd want to do something that makes sense holistically, and isn't likely to cause trouble for casual users.

I haven't checked, but does attrs do anything like this? It might make sense to try out the idea there, first.

d32e3dae-0148-4235-b30d-834c407f030f commented 6 years ago

A possible use case would be a field that represents an auto-incrementing database ID. Since the field is auto-incrementing in the database, it would be desirable to keep this field frozen, and others mutable once an instance of a data class is created.

This is also a feature of data classes in other languages, for example Kotlin: https://kotlinlang.org/docs/reference/data-classes.html . The main difference here being that we don't have val and var, and thus mutability and immutability are harder to represent.

Hashing could be implemented such that frozen fields are allowed to be part of the hash, but unfrozen fields would remain only part of the hash when unsafe_hash is set. I believe equality follows from hashing, so the same rules should apply.

The conflict between setting the class to frozen, and a field to not frozen could be solved by raising an exception.

Upon investigation, attrs does not support this behavior. I can try this feature with attrs, but their implementation may differ enough to make it infeasible.