Commit a6b5b1e
authored
[dataclass_transform] fix frozen behavior for base classes with direct metaclasses (#14878)
Fixes #14857. The initial implementation overlooked this statement in
[PEP 681](https://peps.python.org/pep-0681/#dataclass-semantics):
> Similarly, a class that directly specifies a metaclass that is
decorated with dataclass_transform is considered neither frozen nor
non-frozen.
As far as I can tell, this is a special case that _only_ applies to
classes that directly specify a `dataclass_transform` metaclass. This is
import for projects like Pydantic, which requires clients to use a base
class but still supports a `frozen` parameter.
Note that this allows mixed frozen and non-frozen behavior if the base
class _does_ have fields:
```
@dataclass_transform()
class Meta(type): ...
class Base(metaclass=Meta, frozen=False):
base: int = 0
class Foo(Base, frozen=True):
foo: int = 0
foo = Foo()
foo.foo = 1 # an error because Foo is frozen
foo.base = 1 # NOT an error — Base is neither frozen nor non-frozen
```
While this is probably surprising behavior, it seems to match the text
of the PEP as well as the behavior of Pyright.1 parent 8d59d31 commit a6b5b1e
File tree
2 files changed
+56
-2
lines changed- mypy/plugins
- test-data/unit
2 files changed
+56
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| |||
100 | 101 | | |
101 | 102 | | |
102 | 103 | | |
| 104 | + | |
103 | 105 | | |
104 | 106 | | |
105 | 107 | | |
| |||
140 | 142 | | |
141 | 143 | | |
142 | 144 | | |
| 145 | + | |
143 | 146 | | |
144 | 147 | | |
145 | 148 | | |
| |||
292 | 295 | | |
293 | 296 | | |
294 | 297 | | |
295 | | - | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
296 | 303 | | |
297 | 304 | | |
298 | 305 | | |
| |||
582 | 589 | | |
583 | 590 | | |
584 | 591 | | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
585 | 595 | | |
586 | 596 | | |
587 | 597 | | |
| |||
624 | 634 | | |
625 | 635 | | |
626 | 636 | | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
627 | 644 | | |
628 | 645 | | |
629 | 646 | | |
| |||
787 | 804 | | |
788 | 805 | | |
789 | 806 | | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
415 | 415 | | |
416 | 416 | | |
417 | 417 | | |
418 | | - | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
419 | 423 | | |
420 | 424 | | |
421 | 425 | | |
| |||
777 | 781 | | |
778 | 782 | | |
779 | 783 | | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
0 commit comments