diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2018-02-27 01:38:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-27 01:38:33 (GMT) |
commit | 2fa6b9eae07e2385e2acbf2e40093a21fb3a10c4 (patch) | |
tree | 8f5dfd3eb970c93199bd916e3b7d3dbd02dcf262 /Lib/dataclasses.py | |
parent | 72d9b2be36f091793ae7ffc5ad751f040c6e6ad3 (diff) | |
download | cpython-2fa6b9eae07e2385e2acbf2e40093a21fb3a10c4.zip cpython-2fa6b9eae07e2385e2acbf2e40093a21fb3a10c4.tar.gz cpython-2fa6b9eae07e2385e2acbf2e40093a21fb3a10c4.tar.bz2 |
bpo-32960: For dataclasses, disallow inheriting frozen from non-frozen classes and vice-versa, (GH-5919)
This restriction will be relaxed at a future date.
Diffstat (limited to 'Lib/dataclasses.py')
-rw-r--r-- | Lib/dataclasses.py | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index db92b10..54478fe 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -623,14 +623,21 @@ def _process_class(cls, repr, eq, order, unsafe_hash, init, frozen): else: setattr(cls, f.name, f.default) + # We're inheriting from a frozen dataclass, but we're not frozen. + if cls.__setattr__ is _frozen_setattr and not frozen: + raise TypeError('cannot inherit non-frozen dataclass from a ' + 'frozen one') + + # We're inheriting from a non-frozen dataclass, but we're frozen. + if (hasattr(cls, _MARKER) and cls.__setattr__ is not _frozen_setattr + and frozen): + raise TypeError('cannot inherit frozen dataclass from a ' + 'non-frozen one') + # Remember all of the fields on our class (including bases). This # marks this class as being a dataclass. setattr(cls, _MARKER, fields) - # We also need to check if a parent class is frozen: frozen has to - # be inherited down. - is_frozen = frozen or cls.__setattr__ is _frozen_setattr - # Was this class defined with an explicit __hash__? Note that if # __eq__ is defined in this class, then python will automatically # set __hash__ to None. This is a heuristic, as it's possible @@ -654,7 +661,7 @@ def _process_class(cls, repr, eq, order, unsafe_hash, init, frozen): if f._field_type in (_FIELD, _FIELD_INITVAR)] _set_new_attribute(cls, '__init__', _init_fn(flds, - is_frozen, + frozen, has_post_init, # The name to use for the "self" param # in __init__. Use "self" if possible. @@ -696,7 +703,7 @@ def _process_class(cls, repr, eq, order, unsafe_hash, init, frozen): f'in class {cls.__name__}. Consider using ' 'functools.total_ordering') - if is_frozen: + if frozen: for name, fn in [('__setattr__', _frozen_setattr), ('__delattr__', _frozen_delattr)]: if _set_new_attribute(cls, name, fn): |