Skip to content
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

Support immutability per-field in dataclasses #77655

Open
DanielLindeman mannequin opened this issue May 12, 2018 · 3 comments
Open

Support immutability per-field in dataclasses #77655

DanielLindeman mannequin opened this issue May 12, 2018 · 3 comments
Assignees
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@DanielLindeman
Copy link
Mannequin

DanielLindeman mannequin commented May 12, 2018

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:

assignee = 'https://github.com/ericvsmith'
closed_at = None
created_at = <Date 2018-05-12.21:28:57.583>
labels = ['3.8', 'type-feature', 'library']
title = 'Support immutability per-field in dataclasses'
updated_at = <Date 2018-05-15.04:20:40.665>
user = 'https://bugs.python.org/DanielLindeman'

bugs.python.org fields:

activity = <Date 2018-05-15.04:20:40.665>
actor = 'Daniel Lindeman'
assignee = 'eric.smith'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2018-05-12.21:28:57.583>
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']

@DanielLindeman
Copy link
Mannequin Author

DanielLindeman mannequin commented May 12, 2018

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.

@DanielLindeman DanielLindeman mannequin added 3.7 (EOL) end of life type-feature A feature request or enhancement labels May 12, 2018
@rhettinger rhettinger added stdlib Python modules in the Lib dir 3.8 only security fixes and removed 3.7 (EOL) end of life labels May 13, 2018
@ericvsmith
Copy link
Member

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.

@DanielLindeman
Copy link
Mannequin Author

DanielLindeman mannequin commented May 15, 2018

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants