Title: copy.replace: _SupportsReplace[RT] breaks Self-returning __replace__ with bound TypeVar · Issue #15973 · python/typeshed · GitHub
Open Graph Title: copy.replace: _SupportsReplace[RT] breaks Self-returning __replace__ with bound TypeVar · Issue #15973 · python/typeshed
X Title: copy.replace: _SupportsReplace[RT] breaks Self-returning __replace__ with bound TypeVar · Issue #15973 · python/typeshed
Description: PR #14786 changed copy.replace from a bound-TypeVar signature to a covariant protocol. This introduced a regression for __replace__ methods that return Self (dataclasses, namedtuples, etc.) when the argument is a bounded TypeVar. To Repr...
Open Graph Description: PR #14786 changed copy.replace from a bound-TypeVar signature to a covariant protocol. This introduced a regression for __replace__ methods that return Self (dataclasses, namedtuples, etc.) when th...
X Description: PR #14786 changed copy.replace from a bound-TypeVar signature to a covariant protocol. This introduced a regression for __replace__ methods that return Self (dataclasses, namedtuples, etc.) when th...
Opengraph URL: https://github.com/python/typeshed/issues/15973
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"copy.replace: _SupportsReplace[RT] breaks Self-returning __replace__ with bound TypeVar","articleBody":"PR [#14786](https://github.com/python/typeshed/pull/14786) changed `copy.replace` from a bound-TypeVar signature to a covariant protocol. This introduced a regression for `__replace__` methods that return `Self` (dataclasses, namedtuples, etc.) when the argument is a bounded `TypeVar`.\n\n## To Reproduce\n\n```python\nimport copy\nfrom dataclasses import dataclass\nfrom typing import TypeVar, assert_type\n\n@dataclass(frozen=True)\nclass BaseConfig:\n pg_ssl_key: str | None = None\n\n@dataclass(frozen=True)\nclass SubConfig(BaseConfig):\n pg_host: str = \"localhost\"\n\nT = TypeVar(\"T\", bound=BaseConfig)\n\ndef f(config: T) -> T:\n result = copy.replace(config, pg_ssl_key=\"replaced\")\n assert_type(result, T)\n return result\n```\n\nPlayground: https://mypy-play.net/?mypy=master&python=3.14&gist=32a94d730b07faeb680192a3fdad1b0f\n\n## Expected\n\n`assert_type(result, T)` passes — `copy.replace` returns the same type as the argument.\n\n## Actual\n\n```text\nerror: Expression is of type \"BaseConfig\", not \"T\" [assert-type]\nerror: Incompatible return value type (got \"BaseConfig\", expected \"T\") [return-value]\n```\n\nConcrete subclass works fine — `copy.replace(SubConfig(), ...)` correctly returns `SubConfig`. Only the `TypeVar`-bounded case breaks.\n\n## Root Cause\n\nThe change from:\n\n```python\n_SR = TypeVar(\"_SR\", bound=_SupportsReplace)\nclass _SupportsReplace(Protocol):\n def __replace__(self, ...) -> Self: ...\ndef replace(obj: _SR, /, ...) -> _SR: ...\n```\n\nto:\n\n```python\n_RT_co = TypeVar(\"_RT_co\", covariant=True)\nclass _SupportsReplace(Protocol[_RT_co]):\n def __replace__(self, ...) -> _RT_co: ...\ndef replace(obj: _SupportsReplace[_RT_co], /, ...) -> _RT_co: ...\n```\n\nThe bound-TypeVar approach preserves the argument type through the call. The covariant protocol infers `_RT_co` from the protocol method return type, which for `Self` resolves at the upper bound level — losing the `TypeVar`.\n\nThis is a fundamental limitation of Python's structural subtyping: one signature cannot simultaneously express both \"return same type as argument\" (the common `Self` case) and \"return whatever `__replace__` declares\" (the `Box[int] -> Box[str]` edge case).\n\nThe common case (dataclasses / namedtuples returning `Self`) should take priority, since the edge case can use `typing.cast` as a workaround but the `Self` case has no workaround.\n\n## Related\n\n- mypy issue: https://github.com/python/mypy/issues/21672","author":{"url":"https://github.com/trim21","@type":"Person","name":"trim21"},"datePublished":"2026-07-03T10:17:59Z","url":"https://github.com/python/typeshed/issues/15973"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f9d573ef-0125-7a30-8093-45cd36355444 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C8E:1DF7D7:316185:4503EA:6B005BF6 |
| html-safe-nonce | d2a1e460bf95edf54eda0a43e8a6b01fc7bd2723b05e90c1536b676ba4bdf40b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzhFOjFERjdENzozMTYxODU6NDUwM0VBOjZCMDA1QkY2IiwidmlzaXRvcl9pZCI6IjEwMzc0MTk5MDE1NTMyMzY5ODIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6fd8630d250b958d80d4cec1274de9a978cd7457a160b24aebcdb7db4d467872 |
| hovercard-subject-tag | issue:4802620343 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/15973/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d79675b1a69049c6a0441b82a46d7f2fe2b464ed5a00d72d4e053fef9e3b389a/python/typeshed/issues/15973 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d79675b1a69049c6a0441b82a46d7f2fe2b464ed5a00d72d4e053fef9e3b389a/python/typeshed/issues/15973 |
| og:image:alt | PR #14786 changed copy.replace from a bound-TypeVar signature to a covariant protocol. This introduced a regression for __replace__ methods that return Self (dataclasses, namedtuples, etc.) when th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | trim21 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width