Commit b6d93f5
authored
Fix regression in dataclass narrowing for Python >= 3.13 (#21675)
Fixes #21635
On Python >= 3.13, `@dataclass` synthesizes a `__replace__(self, ...) ->
Self`
method to support `copy.replace()`. When mypy tries to narrow a
`type[A]`
expression via `issubclass(cls, M)` (or `isinstance`) against a second,
unrelated dataclass `M`, it builds an ad-hoc `<subclass of A and M>`
type to
check whether that narrowing is sound (`intersect_instances` in
`checker.py`). Building that ad-hoc type runs
`check_multiple_inheritance`,
which sees `A`'s synthesized `__replace__` returning `A` and `M`'s
returning `M`, and flags them as incompatible.
That's a false positive: a real subclass of both (e.g. `class C(M, A)`,
itself decorated with `@dataclass`) gets its *own* freshly synthesized,
mutually compatible `__replace__`.1 parent e986558 commit b6d93f5
2 files changed
Lines changed: 22 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3133 | 3133 | | |
3134 | 3134 | | |
3135 | 3135 | | |
3136 | | - | |
| 3136 | + | |
3137 | 3137 | | |
3138 | 3138 | | |
3139 | 3139 | | |
| |||
| 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 | + | |
2612 | 2633 | | |
2613 | 2634 | | |
2614 | 2635 | | |
| |||
0 commit comments