[New-bugs-announce] [issue32953] Dataclasses: frozen should not be inherited
Eric V. Smith
report at bugs.python.org
Sun Feb 25 17:23:51 EST 2018
New submission from Eric V. Smith <eric at trueblade.com>:
Reported by Raymond Hettinger:
When working on the docs for dataclasses, something unexpected came up. If a dataclass is specified to be frozen, that characteristic is inherited by subclasses which prevents them from assigning additional attributes:
>>> @dataclass(frozen=True)
class D:
x: int = 10
>>> class S(D):
pass
>>> s = S()
>>> s.cached = True
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
s.cached = True
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/dataclasses.py", line 448, in _frozen_setattr
raise FrozenInstanceError(f'cannot assign to field {name!r}')
dataclasses.FrozenInstanceError: cannot assign to field 'cached'
Other immutable classes in Python don't behave the same way:
>>> class T(tuple):
pass
>>> t = T([10, 20, 30])
>>> t.cached = True
>>> class F(frozenset):
pass
>>> f = F([10, 20, 30])
>>> f.cached = True
>>> class B(bytes):
pass
>>> b = B()
>>> b.cached = True
Raymond
----------
assignee: eric.smith
components: Library (Lib)
messages: 312866
nosy: eric.smith, rhettinger
priority: normal
severity: normal
status: open
title: Dataclasses: frozen should not be inherited
type: behavior
versions: Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32953>
_______________________________________
More information about the New-bugs-announce
mailing list