[issue33474] Support immutability per-field in dataclasses

2018-05-14 Thread Daniel Lindeman

Daniel Lindeman <linde...@gmail.com> added the comment:

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.

--

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33474>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33474] Support immutability per-field in dataclasses

2018-05-12 Thread Daniel Lindeman

New submission from Daniel Lindeman <linde...@gmail.com>:

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.

--
messages: 316438
nosy: Daniel Lindeman, eric.smith
priority: normal
severity: normal
status: open
title: Support immutability per-field in dataclasses
type: enhancement
versions: Python 3.7

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33474>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com