New submission from Raymond Hettinger <[email protected]>:
The unsafe_hash option is unsafe only because it doesn't afford mutability
protections. This can be mitigated with selective immutability.
@dataclass
class Person:
ssn: int = field(immutable=True)
birth_city: int = field(immutable=True)
name: str # A person can change their name
address: str # A person can move
age: int # An age can change
This would generate something like this:
def __setattr__(self, attr, value):
if attr in {'ssn', 'birth_city'} and hasattr(self, attr):
raise TypeError(
f'{attr!r} is not settable after initialization')
return object.__setattr__(self, name, attr)
A number of APIs are possible -- the important thing to be able to selectively
block updates to particular fields (particularly those used in hashing and
ordering).
----------
assignee: eric.smith
components: Library (Lib)
messages: 332080
nosy: eric.smith, rhettinger
priority: normal
severity: normal
status: open
title: Make fields selectively immutable in dataclasses
type: enhancement
versions: Python 3.8
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue35527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com