René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:f9d573ef-0125-7a30-8093-45cd36355444
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8C8E:1DF7D7:316185:4503EA:6B005BF6
html-safe-nonced2a1e460bf95edf54eda0a43e8a6b01fc7bd2723b05e90c1536b676ba4bdf40b
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzhFOjFERjdENzozMTYxODU6NDUwM0VBOjZCMDA1QkY2IiwidmlzaXRvcl9pZCI6IjEwMzc0MTk5MDE1NTMyMzY5ODIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac6fd8630d250b958d80d4cec1274de9a978cd7457a160b24aebcdb7db4d467872
hovercard-subject-tagissue:4802620343
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/15973/issue_layout
twitter:imagehttps://opengraph.githubassets.com/d79675b1a69049c6a0441b82a46d7f2fe2b464ed5a00d72d4e053fef9e3b389a/python/typeshed/issues/15973
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/d79675b1a69049c6a0441b82a46d7f2fe2b464ed5a00d72d4e053fef9e3b389a/python/typeshed/issues/15973
og:image:altPR #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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernametrim21
hostnamegithub.com
expected-hostnamegithub.com
None29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73
turbo-cache-controlno-preview
go-importgithub.com/python/typeshed git https://github.com/python/typeshed.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id31696383
octolytics-dimension-repository_nwopython/typeshed
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id31696383
octolytics-dimension-repository_network_root_nwopython/typeshed
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseeacd927ba6112e3b01ba99c762bd699a4a4f5df7
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/typeshed/issues/15973#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftypeshed%2Fissues%2F15973
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftypeshed%2Fissues%2F15973
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Ftypeshed
Reloadhttps://github.com/python/typeshed/issues/15973
Reloadhttps://github.com/python/typeshed/issues/15973
Reloadhttps://github.com/python/typeshed/issues/15973
pythonhttps://github.com/python
typeshedhttps://github.com/python/typeshed
Codehttps://github.com/python/typeshed
Issues196 (196)https://github.com/python/typeshed/issues
Pull requests252 (252)https://github.com/python/typeshed/pulls
Actionshttps://github.com/python/typeshed/actions
Security and qualityhttps://github.com/python/typeshed/security
Insightshttps://github.com/python/typeshed/insights
https://github.com/python/typeshed
New issuehttps://github.com/login?return_to=https://github.com/python/typeshed/issues/15973
New issuehttps://github.com/login?return_to=https://github.com/python/typeshed/issues/15973
#15997https://github.com/python/typeshed/pull/15997
copy.replace: _SupportsReplace[RT] breaks Self-returning __replace__ with bound TypeVarhttps://github.com/python/typeshed/issues/15973#top
#15997https://github.com/python/typeshed/pull/15997
https://github.com/trim21
trim21https://github.com/trim21
on Jul 3, 2026https://github.com/python/typeshed/issues/15973#issue-4802620343
#14786https://github.com/python/typeshed/pull/14786
https://mypy-play.net/?mypy=master&python=3.14&gist=32a94d730b07faeb680192a3fdad1b0fhttps://mypy-play.net/?mypy=master&python=3.14&gist=32a94d730b07faeb680192a3fdad1b0f
copy.replace type inference regression: breaks Self-returning __replace__ with bound TypeVar mypy#21672https://github.com/python/mypy/issues/21672
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typeshed/issues/15973
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typeshed/issues/15973
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.