René's URL Explorer Experiment


Title: Add public functions to dataclasses to correctly identify methods and features · Issue #157314 · python/cpython · GitHub

Open Graph Title: Add public functions to dataclasses to correctly identify methods and features · Issue #157314 · python/cpython

X Title: Add public functions to dataclasses to correctly identify methods and features · Issue #157314 · python/cpython

Description: Feature or enhancement Proposal: There are a few places both in the stdlib and in third party packages where tools attempt to discover specific features of how dataclasses are constructed. Currently these either rely on leaky internals o...

Open Graph Description: Feature or enhancement Proposal: There are a few places both in the stdlib and in third party packages where tools attempt to discover specific features of how dataclasses are constructed. Currentl...

X Description: Feature or enhancement Proposal: There are a few places both in the stdlib and in third party packages where tools attempt to discover specific features of how dataclasses are constructed. Currentl...

Opengraph URL: https://github.com/python/cpython/issues/157314

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add public functions to dataclasses to correctly identify methods and features","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nThere are a few places both in the stdlib and in third party packages where tools attempt to discover specific features of how dataclasses are constructed. Currently these either rely on leaky internals or will behave incorrectly in certain cases, or both.\n\nI'd like to both make it possible to stop leaking those internal details while also making the discovery more accurate.\n\nProposal:\n\n1) **Add a `__generated_for_dataclass__` attribute to methods generated by dataclasses** \n   * This would hold the class the method was generated for\n   * Note that this would not include shared functions like `__replace__` which are attached to dataclasses but are not generated for a specific class\n   * Adding a `True/False` flag was proposed back in [the issue that added pprint repr replacement support](https://github.com/python/cpython/issues/87246#issuecomment-1093900950), this extends that idea to also identify which class a method was generated for which gives more useful information[^1]\n2) **Add a public `generated_for_dataclass`[^2] function to retrieve this attribute** \n    * This would return `None` if the method is not a generated dataclass method\n3) **Add an `is_frozen` public function** to detect if a class is actually a frozen dataclass\n   * The current implementation would check that `generated_for_dataclass(cls.method) is cls` for both `__setattr__` and `__delattr__`\n   * This is both a convenience function and future-proofing if the implementation of frozen-ness changes\n\nIn the linked discuss thread I also mentioned adding a `requires_decorator` to `is_dataclass` to identify if a class has been decorated and doesn't just inherit from a dataclass along with making `_is_dataclass_instance` public and adding a matching `is_dataclass_type`. These additions could be used to simplify some existing checks but aren't necessary to resolve the logic issues. As such I'm leaving them out unless it's considered worthwhile to include them.\n\n---\n\nExamples of issues this intends to resolve:\n\n1) Attempts to detect and replace a dataclass `__repr__`:\n\n- [pprint](https://github.com/python/cpython/blob/36c7440c480da622735788af656c728af28cd9eb/Lib/pprint.py#L208-L215)\n   * This relies on dataclasses not cleanly renaming the `__repr__` function and leaking the name of the internal `__create_fn__` function through the recursive_repr wrapper\n   * This prevented dataclasses from renaming the internal function in [a previous PR](https://github.com/python/cpython/pull/110851#issuecomment-1768502273)\n   * A change that would no longer leak the internal name would also break this (I discovered this while testing changes for lazy method generation)\n- [enum](https://github.com/python/cpython/blob/36c7440c480da622735788af656c728af28cd9eb/Lib/enum.py#L993-L999)\n   * This only checks if `repr=True` is in the params and doesn't check if the `__repr__` method is actually generated by dataclasses and will replace any `__repr__` method on a dataclass\n   * As a dataclass, defining the `__repr__` by hand should be treated the same as setting `repr=False` but this isn't the case for enum \n- [rich](https://github.com/Textualize/rich/blob/9d8f9a372cc5916fd4781fec207ced7ddac2f08f/rich/pretty.py#L70-L87)\n   * This checks for `reprlib.py`'s path in `__code__.co_filename`\n   * **Any** `__repr__` function on a dataclass that uses `recursive_repr` will be replaced\n   * Unlike the 2 stdlib cases, this can't rely on being kept in sync with changes to `dataclasses` due to failing stdlib tests\n\nThese could all use `generated_for_dataclass(cls.__repr__)` to both identify if a `__repr__` is replaceable and get the correct class to use for retrieving the matching `fields`.\n\n---\n\n2) Current attempts to detect frozenness:\n\nThere are a number of tools that look at `cls.__dataclass_params__.frozen` to try and detect if a class is a frozen dataclass, here's an example from  [pytorch](https://github.com/pytorch/pytorch/blob/2b1d72fc614c57d5ebe70ebaf22da2abaf11a3d4/torch/_dynamo/utils.py#L5537-L5548) and [other examples from a search](https://github.com/search?q=__dataclass_params__.frozen+language%3APython&type=code).\n\nThis logic will fail if `__setattr__` or `__delattr__` have been replaced in an undecorated subclass.\n\n```python\nfrom dataclasses import dataclass, is_dataclass\n\n@dataclass(frozen=True)\nclass Frozen:\n    a: int = 42\n\nclass UnFrozen(Frozen):\n    __setattr__ = object.__setattr__\n    __delattr__ = object.__delattr__\n\nex = UnFrozen()\nprint(is_dataclass(ex))  # True\nprint(ex.__dataclass_params__.frozen)  # True\nex.a = 1  # Not actually frozen\n```\n\n[^1]: For example if you want to replace the `__repr__` function, it tells you which class to use for `fields(cls)` to generate the correct method\n[^2]: Open to a different name if there's a better suggestion\n\n\n### Has this already been discussed elsewhere?\n\nI have already discussed this feature proposal on Discourse\n\n### Links to previous discussion of this feature:\n\nhttps://discuss.python.org/t/add-public-functions-to-inspect-dataclass-parameters-and-methods/108613/12","author":{"url":"https://github.com/DavidCEllis","@type":"Person","name":"DavidCEllis"},"datePublished":"2026-09-11T12:55:57Z","url":"https://github.com/python/cpython/issues/157314"}

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:5b7f57ae-52ed-b298-9924-8e9f493f4dfb
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8444:17543D:61323C:7B28DE:6B020991
html-safe-nonce082477cb2cd23dd066d74512442e74893923d1f4cf1f0355715f8ec01d3c6036
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDQ0OjE3NTQzRDo2MTMyM0M6N0IyOERFOjZCMDIwOTkxIiwidmlzaXRvcl9pZCI6IjEzMTcxNTkyODYyNzQ2NTY2NTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac6e07084eb922ba7ec4a4bfa7825fd54ca48a088967508bb8c3b49986bfd966f4
hovercard-subject-tagissue:5424547633
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/cpython/157314/issue_layout
twitter:imagehttps://opengraph.githubassets.com/1952ac72696da34246cb80b9064ab18a0ba400642daf2b82b4755c3bc152b740/python/cpython/issues/157314
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/1952ac72696da34246cb80b9064ab18a0ba400642daf2b82b4755c3bc152b740/python/cpython/issues/157314
og:image:altFeature or enhancement Proposal: There are a few places both in the stdlib and in third party packages where tools attempt to discover specific features of how dataclasses are constructed. Currentl...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameDavidCEllis
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
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
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/157314#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F157314
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%2Fcpython%2Fissues%2F157314
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%2Fcpython
Reloadhttps://github.com/python/cpython/issues/157314
Reloadhttps://github.com/python/cpython/issues/157314
Reloadhttps://github.com/python/cpython/issues/157314
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/157314
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/157314
Add public functions to dataclasses to correctly identify methods and featureshttps://github.com/python/cpython/issues/157314#top
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-dataclasseshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-dataclasses%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/DavidCEllis
DavidCEllishttps://github.com/DavidCEllis
on Sep 11, 2026https://github.com/python/cpython/issues/157314#issue-5424547633
the issue that added pprint repr replacement supporthttps://github.com/python/cpython/issues/87246#issuecomment-1093900950
1https://github.com/python/cpython/issues/157314#user-content-fn-1-f03bb9d9b464117292ff8ca86a9b4376
2https://github.com/python/cpython/issues/157314#user-content-fn-2-f03bb9d9b464117292ff8ca86a9b4376
pprinthttps://github.com/python/cpython/blob/36c7440c480da622735788af656c728af28cd9eb/Lib/pprint.py#L208-L215
a previous PRhttps://github.com/python/cpython/pull/110851#issuecomment-1768502273
enumhttps://github.com/python/cpython/blob/36c7440c480da622735788af656c728af28cd9eb/Lib/enum.py#L993-L999
richhttps://github.com/Textualize/rich/blob/9d8f9a372cc5916fd4781fec207ced7ddac2f08f/rich/pretty.py#L70-L87
pytorchhttps://github.com/pytorch/pytorch/blob/2b1d72fc614c57d5ebe70ebaf22da2abaf11a3d4/torch/_dynamo/utils.py#L5537-L5548
other examples from a searchhttps://github.com/search?q=__dataclass_params__.frozen+language%3APython&type=code
https://discuss.python.org/t/add-public-functions-to-inspect-dataclass-parameters-and-methods/108613/12https://discuss.python.org/t/add-public-functions-to-inspect-dataclass-parameters-and-methods/108613/12
↩https://github.com/python/cpython/issues/157314#user-content-fnref-1-f03bb9d9b464117292ff8ca86a9b4376
↩https://github.com/python/cpython/issues/157314#user-content-fnref-2-f03bb9d9b464117292ff8ca86a9b4376
DavidCEllishttps://github.com/DavidCEllis
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-dataclasseshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-dataclasses%22
on Sep 11, 2026https://github.com/python/cpython/issues/157314#event-30972822225
https://github.com/DavidCEllis
DavidCEllishttps://github.com/DavidCEllis
on Sep 18, 2026https://github.com/python/cpython/issues/157314#issuecomment-5731932133
#156841https://github.com/python/cpython/pull/156841
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/157314
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/157314
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-dataclasseshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-dataclasses%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
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.