René's URL Explorer Experiment


Title: Switching to Dataclasses · Issue #5279 · python-telegram-bot/python-telegram-bot · GitHub

Open Graph Title: Switching to Dataclasses · Issue #5279 · python-telegram-bot/python-telegram-bot

X Title: Switching to Dataclasses · Issue #5279 · python-telegram-bot/python-telegram-bot

Description: Python dataclasses has been something this library has always eyed keenly. A lot of the work and discussion was done earlier in #2698, and a number of things have changed since that issue was created, which has once again opened the door...

Open Graph Description: Python dataclasses has been something this library has always eyed keenly. A lot of the work and discussion was done earlier in #2698, and a number of things have changed since that issue was creat...

X Description: Python dataclasses has been something this library has always eyed keenly. A lot of the work and discussion was done earlier in #2698, and a number of things have changed since that issue was creat...

Opengraph URL: https://github.com/python-telegram-bot/python-telegram-bot/issues/5279

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Switching to Dataclasses","articleBody":"Python dataclasses has been something this library has always eyed keenly. A lot of the work and discussion was done earlier in #2698, and a number of things have changed since that issue was created, which has once again opened the door for us to now actually switch to dataclasses.\n\nReiterating the main benefits we get from the switch: \n\n1. No more slots and `__init__` boilerplate\n2. Letting dataclass machinery handle equality comparison for us\n3. Drastically less maintenance overhead, even when writing tests, since we don't need slots and equality tests.\n\n## Implementation\n\nI'll go over things which were discussed in the issue and whether they are still applicable:\n\n### Minor details\n\n1. **Immutability**: The solution we went for is rolling our own version of freezing, via a `_frozen` attribute. Dataclasses have their own version via the `frozen=True` argument. Here are the pros/cons to using that:\n\n    1. ➖ IISC, dataclasses does not have a way to temporarily unfreeze an object, which means using `object.__setattr__` as a workaround, specially in tests.\n    2. ➖/➕ There's been a few minor issues when mixing slots and frozen: https://github.com/python/cpython/issues/105936, though this has been fixed in later versions of python 3.13 and above? If users encounter this somehow below 3.13, we may just have to ask them to upgrade, but the scenario in that issue should not happen in our library. \n    3. ➕ Initializing new classes is at 3-10x faster[^1] if we use the dataclass approach. I had codex write a [script](https://gist.github.com/harshil21/57ca4fee272277c1a44a44fbbb3e9805) for that. The results were surprising since I was expecting the opposite.. \n\nPersonally, I'm slightly in favour of using the dataclass frozen=True approach because of the \"free\" performance gain. \n\n2. **Equality comparison**: Dataclasses also has its own comparison method, using `compare=True`, which removes the need for our manual `_id_attrs` logic. I currently don't see any reason why we shouldn't use their approach. The only annoyance is a little more boilerplate to write if you don't want to include a field for equality comparsion. You have to do a `field(eq=False)`. With `frozen=True`, the `__hash__` method will also be handled for us and will check the attributes we want it to check.\n\n3. **`__repr__`**: Dataclasses can also do this for us, but now that we have our own approach, which customizes the look by dropping None values and empty containers, we can just stick to using that. \n\n4. **`__slots__`**: One of the [showstoppers previously](https://github.com/python-telegram-bot/python-telegram-bot/issues/2698#issuecomment-1237415687), we no longer have that issue since we only support 3.10+. \n\n5. **`__match_args__`**: Since we don't have a use for them, we should disable them globally for all TelegramObjects.\n\n6. Some class constants will need to be annotated with **`ClassVar`**, specially the `Final` ones.\n\n7. We should mention that `dataclasses.asdict` and `replace` is not explicitly supported by our API and users should use `to_dict`.\n\n8. Replacing `__slots__` with **`fields()`** when wanting to get all attributes, this means almost all of our internal TO methods and even tests would need to change. \n\n### Major details\n\n9. **Handling of Init only fields and transform like fields:**\n\nAt the moment, we have a couple of classes which call other functions in the `__init__`, like calling `to_timedelta`, or `parse_sequence_arg`. For these kind of classes, I propose using the `metadata` field in [`Field`](https://docs.python.org/3/library/dataclasses.html#dataclasses.Field). This metadata will contain the function for transformation. \n\n`TO.__post_init__` will be defined on the base class which will automatically run every time after the regular `__init__`. It's at this time we will iterate through any metadata fields and apply the transformations.\n\nAn additional use of the metadata field is to use it for deprecated arguments / attributes, e.g. `duration` can be an InitVar, and then we can use a regular `@property` to raise warnings. The metadata field can be used to store the value in `_duration` for example. \n\n_Side_: We can't type hint the argument and attribute differently in dataclasses, so I propose to make the `Sequence` accepting arguments as `tuple`, since users are more likely to access attributes rather than initializing a class with them.\n\n10. **Inheritance**: This is still a problem as mentioned in https://github.com/python-telegram-bot/python-telegram-bot/issues/2698#issuecomment-1237415687. \n    Possible solutions:\n\n    i. The best solution imo is still making the defaulted inherited field as keyword only. This means that `_BaseMedium`, `_BaseThumbedMedium`, etc would make all of its default fields as kw only, which in turn changes the signature of classes which inherit them. \nThis is of course a breaking change without a deprecation period, but the stability policy does outline an extreme case where we can't provide compatibility. Moreover, most of these classes are not typically instantiated by users.\n\n    ii. Non-breaking: Override the `__init__` generated from the dataclass, define a static list of positional arguments expected, and then check the passed arguments to check if they were passed positionally in the right order. I had GPT 5.5 do this, but I felt it was too black magicky and was over ~100 lines. It could work, but it can also be a maintainence burden if it's buggy.\n\n    iii. Inline inherited arguments: This is basically removing the internal classes and all of its attributes and copying them over to the subclasses. That way we can control what order the class is instantiated with. Not a big fan of this either..\n\n----\n\nThere could be 2nd order effects which I haven't forseen so far, but this should be the bulk of the changes. \n\nWhat does the dev team feel about this? \n\n[^1]: Benched on my uv installed python 3.14.6.  ","author":{"url":"https://github.com/harshil21","@type":"Person","name":"harshil21"},"datePublished":"2026-06-26T12:11:43Z","url":"https://github.com/python-telegram-bot/python-telegram-bot/issues/5279"}

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:171c5de1-a209-04aa-8efc-dd00166c544d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id800C:AC3D5:F9544A:14700E0:6B0287A8
html-safe-noncec1a62131e7be16ccad5b904eae2c5bba3ca5b1369ad457a41fca3bb3d6c534d9
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDBDOkFDM0Q1OkY5NTQ0QToxNDcwMEUwOjZCMDI4N0E4IiwidmlzaXRvcl9pZCI6Ijg4MDYxNDg5MTI1Mzc4MzEzMzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac6ecd97a3cd052d2653fac4b69f2171576717135b0d49816475736ba2df3124f5
hovercard-subject-tagissue:4751881279
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-telegram-bot/python-telegram-bot/5279/issue_layout
twitter:imagehttps://opengraph.githubassets.com/8a7cca1015d3580cdf8d2b066866d0568a23aa50ea1b3c7fec90e06272410b59/python-telegram-bot/python-telegram-bot/issues/5279
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/8a7cca1015d3580cdf8d2b066866d0568a23aa50ea1b3c7fec90e06272410b59/python-telegram-bot/python-telegram-bot/issues/5279
og:image:altPython dataclasses has been something this library has always eyed keenly. A lot of the work and discussion was done earlier in #2698, and a number of things have changed since that issue was creat...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameharshil21
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python-telegram-bot/python-telegram-bot git https://github.com/python-telegram-bot/python-telegram-bot.git
octolytics-dimension-user_id16178365
octolytics-dimension-user_loginpython-telegram-bot
octolytics-dimension-repository_id38696925
octolytics-dimension-repository_nwopython-telegram-bot/python-telegram-bot
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id38696925
octolytics-dimension-repository_network_root_nwopython-telegram-bot/python-telegram-bot
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-telegram-bot/python-telegram-bot/issues/5279#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-telegram-bot%2Fpython-telegram-bot%2Fissues%2F5279
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-telegram-bot%2Fpython-telegram-bot%2Fissues%2F5279
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-telegram-bot%2Fpython-telegram-bot
Reloadhttps://github.com/python-telegram-bot/python-telegram-bot/issues/5279
Reloadhttps://github.com/python-telegram-bot/python-telegram-bot/issues/5279
Reloadhttps://github.com/python-telegram-bot/python-telegram-bot/issues/5279
python-telegram-bothttps://github.com/python-telegram-bot
python-telegram-bothttps://github.com/python-telegram-bot/python-telegram-bot
Codehttps://github.com/python-telegram-bot/python-telegram-bot
Issues23 (23)https://github.com/python-telegram-bot/python-telegram-bot/issues
Pull requests19 (19)https://github.com/python-telegram-bot/python-telegram-bot/pulls
Discussionshttps://github.com/python-telegram-bot/python-telegram-bot/discussions
Actionshttps://github.com/python-telegram-bot/python-telegram-bot/actions
Projectshttps://github.com/python-telegram-bot/python-telegram-bot/projects
Wikihttps://github.com/python-telegram-bot/python-telegram-bot/wiki
Security and qualityhttps://github.com/python-telegram-bot/python-telegram-bot/security
Insightshttps://github.com/python-telegram-bot/python-telegram-bot/insights
https://github.com/python-telegram-bot/python-telegram-bot
New issuehttps://github.com/login?return_to=https://github.com/python-telegram-bot/python-telegram-bot/issues/5279
New issuehttps://github.com/login?return_to=https://github.com/python-telegram-bot/python-telegram-bot/issues/5279
💡 featurehttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=type:"💡 feature"
#5334https://github.com/python-telegram-bot/python-telegram-bot/pull/5334
Switching to Dataclasseshttps://github.com/python-telegram-bot/python-telegram-bot/issues/5279#top
#5334https://github.com/python-telegram-bot/python-telegram-bot/pull/5334
🛠 breakingchange type: breakinghttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=state%3Aopen%20label%3A%22%F0%9F%9B%A0%20breaking%22
🛠 refactorchange type: refactorhttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=state%3Aopen%20label%3A%22%F0%9F%9B%A0%20refactor%22
v23https://github.com/python-telegram-bot/python-telegram-bot/milestone/53
https://github.com/harshil21
harshil21https://github.com/harshil21
on Jun 26, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#issue-4751881279
#2698https://github.com/python-telegram-bot/python-telegram-bot/issues/2698
dataclasses: frozen dataclasses with slots' __setattr__ and __delattr__ does not work. python/cpython#105936https://github.com/python/cpython/issues/105936
1https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#user-content-fn-1-7724009174f9023705cb3e7e5c89a13d
scripthttps://gist.github.com/harshil21/57ca4fee272277c1a44a44fbbb3e9805
showstoppers previouslyhttps://github.com/python-telegram-bot/python-telegram-bot/issues/2698#issuecomment-1237415687
Fieldhttps://docs.python.org/3/library/dataclasses.html#dataclasses.Field
[DISCUSSION] Dataclasses #2698 (comment)https://github.com/python-telegram-bot/python-telegram-bot/issues/2698#issuecomment-1237415687
↩https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#user-content-fnref-1-7724009174f9023705cb3e7e5c89a13d
harshil21https://github.com/harshil21
v23https://github.com/python-telegram-bot/python-telegram-bot/milestone/53
on Jun 26, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#event-27245248726
harshil21https://github.com/harshil21
💡 featurehttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=type:"💡 feature"
on Jun 26, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#event-27245249423
harshil21https://github.com/harshil21
Highhttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=field.Effort%3A%22High%22
Highhttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=field.Priority%3A%22High%22
on Jun 26, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#event-27245249500
harshil21https://github.com/harshil21
🛠 refactorchange type: refactorhttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=state%3Aopen%20label%3A%22%F0%9F%9B%A0%20refactor%22
🛠 breakingchange type: breakinghttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=state%3Aopen%20label%3A%22%F0%9F%9B%A0%20breaking%22
on Jun 26, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#event-27245249730
https://github.com/aelkheir
aelkheirhttps://github.com/aelkheir
on Jul 16, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#issuecomment-4993586744
#5186https://github.com/python-telegram-bot/python-telegram-bot/pull/5186
david-lev/pywahttps://github.com/david-lev/pywa
https://github.com/AAraKKe
AAraKKehttps://github.com/AAraKKe
on Jul 16, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#issuecomment-4997244793
https://github.com/Poolitzer
Poolitzerhttps://github.com/Poolitzer
on Jul 17, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#issuecomment-4997770849
https://github.com/AAraKKe
AAraKKehttps://github.com/AAraKKe
on Jul 17, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#issuecomment-5000029592
most used library in APIshttps://pydantic.dev/docs/validation/latest/get-started/#who-is-using-pydantic
https://github.com/harshil21
harshil21https://github.com/harshil21
on Jul 21, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#issuecomment-5038132954
https://typing.python.org/en/latest/spec/dataclasses.htmlhttps://typing.python.org/en/latest/spec/dataclasses.html
converterhttps://typing.python.org/en/latest/spec/dataclasses.html#converters
#5186https://github.com/python-telegram-bot/python-telegram-bot/pull/5186
@arghttps://github.com/arg
https://github.com/harshil21
harshil21https://github.com/harshil21
on Jul 21, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#issuecomment-5038233051
@AAraKKehttps://github.com/AAraKKe
#2698https://github.com/python-telegram-bot/python-telegram-bot/issues/2698
https://github.com/aelkheir
aelkheirhttps://github.com/aelkheir
on Aug 13, 2026https://github.com/python-telegram-bot/python-telegram-bot/issues/5279#issuecomment-5287447272
https://typing.python.org/en/latest/spec/dataclasses.htmlhttps://typing.python.org/en/latest/spec/dataclasses.html
typing specificationhttps://typing.python.org/en/latest/spec/dataclasses.html#decorator-function-and-class-metaclass-parameters
https://typing.python.org/en/latest/spec/dataclasses.htmlhttps://typing.python.org/en/latest/spec/dataclasses.html
Sign up for freehttps://github.com/signup?return_to=https://github.com/python-telegram-bot/python-telegram-bot/issues/5279
Sign in to commenthttps://github.com/login?return_to=https://github.com/python-telegram-bot/python-telegram-bot/issues/5279
🛠 breakingchange type: breakinghttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=state%3Aopen%20label%3A%22%F0%9F%9B%A0%20breaking%22
🛠 refactorchange type: refactorhttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=state%3Aopen%20label%3A%22%F0%9F%9B%A0%20refactor%22
💡 featurehttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=type:"💡 feature"
v23https://github.com/python-telegram-bot/python-telegram-bot/milestone/53
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.