Repository navigation
[copy] Accept unions of dataclasses in copy.replace via overload - #16733
Open
hrithiksaini99 wants to merge 3 commits into
Open
hrithiksaini99 wants to merge 3 commits into
hrithiksaini99 wants to merge 3 commits into
Conversation
Fixes python#16582. Since python#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.
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Since #14819,
copy.replaceis typed through_SupportsReplace[_P, _RT_co]with a ParamSpec, and a union argument whose members each have a field the others lack (e.g.Circle | Squaresharing onlyname) is rejected — even though the call is fine at runtime (verified:copy.replace(Square('s', 1.0), name='renamed')returnsSquare(name='renamed', side=1.0)). A ParamSpec cannot bind a union of signatures.Fix
Add the second overload from the issue:
No real argument matches
_Unreplaceable; its presence makes checkers fall back to argument-type expansion (typing spec, overload evaluation step 3) and check each union member on its own.Verification
New regression case:
rename(Circle | Square)accepted,wrong(...)still refused.Fixes #16582.