From 9074f908a0b10b8071adc27d3fd27cf029c8ea63 Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Sat, 7 Nov 2026 13:39:27 +0530 Subject: [PATCH 1/3] [copy] Accept unions of dataclasses in copy.replace via overload Fixes #16582. Since #14819, copy.replace is typed via _SupportsReplace[_P, _RT_co] with a ParamSpec, and a union argument whose members each have a field the others lack (e.g. Circle | Square sharing only name) is rejected: a ParamSpec cannot bind a union of signatures. Add the overload from the issue: a second replace() overload taking _Unreplaceable (@final, empty class) returning Never. No real argument matches it; its presence makes checkers fall back to argument-type expansion (typing spec overload evaluation step 3) and check each union member separately. Regression case in stdlib/@tests/test_cases/check_copy.py: rename() accepted, wrong() still refused. Verified: mypy_test stdlib/copy.pyi success 3.10-3.15, regr_test stdlib success, ruff clean. --- stdlib/@tests/test_cases/check_copy.py | 22 ++++++++++++++++++++++ stdlib/copy.pyi | 20 +++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/stdlib/@tests/test_cases/check_copy.py b/stdlib/@tests/test_cases/check_copy.py index 59896851d737..2b55b2897580 100644 --- a/stdlib/@tests/test_cases/check_copy.py +++ b/stdlib/@tests/test_cases/check_copy.py @@ -37,3 +37,25 @@ def __replace__(self, /, *, value: str) -> Box[str]: box1: Box[int] = Box(42) box2 = copy.replace(box1, value="spam") assert_type(box2, Box[str]) + + +if sys.version_info >= (3, 13): + # Regression for #16582: a union whose members each have a field the + # others lack must not be rejected. + from dataclasses import dataclass + + @dataclass(frozen=True) + class Circle: + name: str + radius: float + + @dataclass(frozen=True) + class Square: + name: str + side: float + + def rename(shape: Circle | Square) -> Circle | Square: + return copy.replace(shape, name="renamed") + + def wrong(shape: Circle | Square) -> Circle | Square: + return copy.replace(shape, name=1) # E: invalid-argument-type diff --git a/stdlib/copy.pyi b/stdlib/copy.pyi index e805af61291b..a349772b6f96 100644 --- a/stdlib/copy.pyi +++ b/stdlib/copy.pyi @@ -1,6 +1,6 @@ import sys -from typing import Any, Protocol, TypeVar, type_check_only -from typing_extensions import ParamSpec +from typing import Any, Protocol, TypeVar, final, overload, type_check_only +from typing_extensions import Never, ParamSpec __all__ = ["Error", "copy", "deepcopy"] @@ -13,6 +13,14 @@ class _SupportsReplace(Protocol[_P, _RT_co]): # In reality doesn't support args, but there's no great way to express this. def __replace__(self, /, *_: _P.args, **changes: _P.kwargs) -> _RT_co: ... +# @final + empty: no argument can be an instance of it. The second replace +# overload below is never matched directly; it exists so checkers fall back +# to argument-type expansion (typing spec overload evaluation step 3) and +# evaluate a union argument member-by-member. See #16582. +@final +@type_check_only +class _Unreplaceable: ... + # None in CPython but non-None in Jython PyStringMap: Any @@ -28,9 +36,15 @@ else: if sys.version_info >= (3, 13): __all__ += ["replace"] + @overload def replace( - obj: _SupportsReplace[_P, _RT_co], /, *_: _P.args, **changes: _P.kwargs # does not accept positional arguments at runtime + obj: _SupportsReplace[_P, _RT_co], + /, + *_: _P.args, + **changes: _P.kwargs, # does not accept positional arguments at runtime ) -> _RT_co: ... + @overload + def replace(obj: _Unreplaceable, /, **changes: object) -> Never: ... class Error(Exception): ... From b0d04c4024f9a7323354d2db3c0ab8c1e911ad42 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 08:11:39 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/copy.pyi | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/stdlib/copy.pyi b/stdlib/copy.pyi index a349772b6f96..ae0238ecb837 100644 --- a/stdlib/copy.pyi +++ b/stdlib/copy.pyi @@ -38,10 +38,7 @@ if sys.version_info >= (3, 13): @overload def replace( - obj: _SupportsReplace[_P, _RT_co], - /, - *_: _P.args, - **changes: _P.kwargs, # does not accept positional arguments at runtime + obj: _SupportsReplace[_P, _RT_co], /, *_: _P.args, **changes: _P.kwargs # does not accept positional arguments at runtime ) -> _RT_co: ... @overload def replace(obj: _Unreplaceable, /, **changes: object) -> Never: ... From f828ce478fe3bfe45b6468b3c957049761553cf6 Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Sat, 7 Nov 2026 13:47:15 +0530 Subject: [PATCH 3/3] test(copy): assert return type of replace with union of dataclasses --- stdlib/@tests/test_cases/check_copy.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/stdlib/@tests/test_cases/check_copy.py b/stdlib/@tests/test_cases/check_copy.py index 2b55b2897580..741ab2635869 100644 --- a/stdlib/@tests/test_cases/check_copy.py +++ b/stdlib/@tests/test_cases/check_copy.py @@ -55,7 +55,6 @@ class Square: side: float def rename(shape: Circle | Square) -> Circle | Square: - return copy.replace(shape, name="renamed") - - def wrong(shape: Circle | Square) -> Circle | Square: - return copy.replace(shape, name=1) # E: invalid-argument-type + res = copy.replace(shape, name="renamed") + assert_type(res, Circle | Square) + return res