Skip to content

Fix copy.replace widening a bound TypeVar to its bound#15997

Open
apoorva-01 wants to merge 2 commits into
python:mainfrom
apoorva-01:fix-15973-copy-replace-self-typevar
Open

Fix copy.replace widening a bound TypeVar to its bound#15997
apoorva-01 wants to merge 2 commits into
python:mainfrom
apoorva-01:fix-15973-copy-replace-self-typevar

Conversation

@apoorva-01

Copy link
Copy Markdown

Fixes #15973

copy.replace drops a bound TypeVar when __replace__ returns Self, so replace(config: T, ...) comes back as the bound instead of T.

#14786 made the protocol covariant for the Box[int] -> Box[str] case, which broke this one. Rather than pick between them, I added a Self-returning overload first that keeps T; anything like Box falls through to the covariant overload. Both work now, and the new test cases fail without it.

(#14819 touches the same stub, so one of us will need a rebase.)

apoorva-01 and others added 2 commits July 11, 2026 06:01
The covariant protocol from python#14786 loses a bound TypeVar whose __replace__
returns Self (dataclasses, namedtuples). Add a Self overload that keeps the
argument's type, falling back to the covariant one so Box[int] -> Box[str]
still works.
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

copy.replace: _SupportsReplace[RT] breaks Self-returning __replace__ with bound TypeVar

1 participant