Commit 966efe1
committed
Don't flag synthesized __replace__ as incompatible across unrelated bases
Fixes #21635.
On Python 3.13+, @DataClass synthesizes a __replace__(self, ...) -> Self
method to support copy.replace(). When mypy builds an ad-hoc intersection
type to narrow an expression via issubclass()/isinstance() against two
unrelated dataclasses, check_multiple_inheritance sees each dataclass's
synthesized __replace__ returning its own concrete type (e.g. A vs M) and
flags them as incompatible, causing intersect_instances to fail and the
narrowed type to collapse to Never -- even when a real subclass of both
(itself decorated with @DataClass, and so getting its own compatible
synthesized __replace__) already exists in the code.
This exempts __replace__ from the cross-base compatibility check the same
way __init__/__new__/__init_subclass__ already are, but only when the
method was generated by a plugin (plugin_generated=True) on both sides --
hand-written __replace__ overrides with genuine incompatibilities are
still caught.1 parent 1462b4e commit 966efe1
2 files changed
Lines changed: 44 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3138 | 3138 | | |
3139 | 3139 | | |
3140 | 3140 | | |
| 3141 | + | |
| 3142 | + | |
| 3143 | + | |
| 3144 | + | |
| 3145 | + | |
| 3146 | + | |
| 3147 | + | |
| 3148 | + | |
| 3149 | + | |
| 3150 | + | |
3141 | 3151 | | |
3142 | 3152 | | |
3143 | 3153 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2609 | 2609 | | |
2610 | 2610 | | |
2611 | 2611 | | |
| 2612 | + | |
| 2613 | + | |
| 2614 | + | |
| 2615 | + | |
| 2616 | + | |
| 2617 | + | |
| 2618 | + | |
| 2619 | + | |
| 2620 | + | |
| 2621 | + | |
| 2622 | + | |
| 2623 | + | |
| 2624 | + | |
| 2625 | + | |
| 2626 | + | |
| 2627 | + | |
| 2628 | + | |
| 2629 | + | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
| 2635 | + | |
| 2636 | + | |
| 2637 | + | |
| 2638 | + | |
| 2639 | + | |
| 2640 | + | |
| 2641 | + | |
| 2642 | + | |
| 2643 | + | |
| 2644 | + | |
| 2645 | + | |
2612 | 2646 | | |
2613 | 2647 | | |
2614 | 2648 | | |
| |||
0 commit comments