bpo-32960: For dataclasses, disallow inheriting frozen from non-froze… · python/cpython@a93e3dc (original) (raw)
`@@ -623,14 +623,21 @@ def _process_class(cls, repr, eq, order, unsafe_hash, init, frozen):
`
623
623
`else:
`
624
624
`setattr(cls, f.name, f.default)
`
625
625
``
``
626
`+
We're inheriting from a frozen dataclass, but we're not frozen.
`
``
627
`+
if cls.setattr is _frozen_setattr and not frozen:
`
``
628
`+
raise TypeError('cannot inherit non-frozen dataclass from a '
`
``
629
`+
'frozen one')
`
``
630
+
``
631
`+
We're inheriting from a non-frozen dataclass, but we're frozen.
`
``
632
`+
if (hasattr(cls, _MARKER) and cls.setattr is not _frozen_setattr
`
``
633
`+
and frozen):
`
``
634
`+
raise TypeError('cannot inherit frozen dataclass from a '
`
``
635
`+
'non-frozen one')
`
``
636
+
626
637
`# Remember all of the fields on our class (including bases). This
`
627
638
`# marks this class as being a dataclass.
`
628
639
`setattr(cls, _MARKER, fields)
`
629
640
``
630
``
`-
We also need to check if a parent class is frozen: frozen has to
`
631
``
`-
be inherited down.
`
632
``
`-
is_frozen = frozen or cls.setattr is _frozen_setattr
`
633
``
-
634
641
`# Was this class defined with an explicit hash? Note that if
`
635
642
`# eq is defined in this class, then python will automatically
`
636
643
`# 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):
`
654
661
`if f._field_type in (_FIELD, _FIELD_INITVAR)]
`
655
662
`_set_new_attribute(cls, 'init',
`
656
663
`_init_fn(flds,
`
657
``
`-
is_frozen,
`
``
664
`+
frozen,
`
658
665
`has_post_init,
`
659
666
`# The name to use for the "self" param
`
660
667
`# in init. Use "self" if possible.
`
`@@ -696,7 +703,7 @@ def _process_class(cls, repr, eq, order, unsafe_hash, init, frozen):
`
696
703
`f'in class {cls.name}. Consider using '
`
697
704
`'functools.total_ordering')
`
698
705
``
699
``
`-
if is_frozen:
`
``
706
`+
if frozen:
`
700
707
`for name, fn in [('setattr', _frozen_setattr),
`
701
708
` ('delattr', _frozen_delattr)]:
`
702
709
`if _set_new_attribute(cls, name, fn):
`