René's URL Explorer Experiment


Title: gh-137855: Lazy import `inspect` module in dataclasses by danielhollas · Pull Request #144387 · python/cpython · GitHub

Open Graph Title: gh-137855: Lazy import `inspect` module in dataclasses by danielhollas · Pull Request #144387 · python/cpython

X Title: gh-137855: Lazy import `inspect` module in dataclasses by danielhollas · Pull Request #144387 · python/cpython

Description: inspect module is slow to import (see #117865) and is dragging down dataclasses with it. There are currently only two uses of inspect in dataclasses, but they are a bit tricky to inline since they are on a direct code path when the @dataclass decorator is executed. inspect.signature is used to autogenerate class docstring (if one is not provided already) inspect.unwrap is used in a rather esoteric code path only for slotted classes, added in #124455) For 1. I have used a descriptor protocol to generate the __doc__ attribute on demand (this is my first time messing with descriptors, apologies if I overlooked something). For 2. can be deferred by calling the unwrap functions only when really necessary (and hopefully this path is not common) Benchmarks ./python -Ximporttime -c "import dataclasses" Before ❯ hyperfine -w 10 './python -c "import dataclasses"' Benchmark 1: ./python -c "import dataclasses" Time (mean ± σ): 15.4 ms ± 1.8 ms [User: 11.6 ms, System: 3.6 ms] Range (min … max): 13.1 ms … 20.2 ms 201 runs After ❯ hyperfine -w 10 './python -c "import dataclasses"' Benchmark 1: ./python -c "import dataclasses" Time (mean ± σ): 11.0 ms ± 1.5 ms [User: 7.7 ms, System: 3.1 ms] Range (min … max): 9.1 ms … 15.0 ms 271 runs Overall seems to be a solid 20-30% improvement. Issue: gh-137855

Open Graph Description: inspect module is slow to import (see #117865) and is dragging down dataclasses with it. There are currently only two uses of inspect in dataclasses, but they are a bit tricky to inline since they ...

X Description: inspect module is slow to import (see #117865) and is dragging down dataclasses with it. There are currently only two uses of inspect in dataclasses, but they are a bit tricky to inline since they ...

Opengraph URL: https://github.com/python/cpython/pull/144387

X: @github

direct link

Domain: github.com

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/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:190783b6-e9fd-19d9-485f-0c3ef583c4d8
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idD526:EA09B:38932C6:4AF58DA:6B0212F9
html-safe-noncea46602ca128bf0ec038ba153794f2612c56120ff9830e4f08eae61d6c7320754
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTI2OkVBMDlCOjM4OTMyQzY6NEFGNThEQTo2QjAyMTJGOSIsInZpc2l0b3JfaWQiOiI2OTQ2ODM3NTIxMTkyMDYzNzM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacad2834feb1e13fe40020be482f6cebf86deee13fd422b276ab4f4609cdc17fa2
hovercard-subject-tagpull_request:3234947531
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/144387/files
twitter:imagehttps://avatars.githubusercontent.com/u/9539441?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/9539441?s=400&v=4
og:image:altinspect module is slow to import (see #117865) and is dragging down dataclasses with it. There are currently only two uses of inspect in dataclasses, but they are a bit tricky to inline since they ...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
diff-viewunified
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 full-width
disable-turbotrue
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

Links:

Skip to contenthttps://github.com/python/cpython/pull/144387/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F144387%2Ffiles
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%2Fpull%2F144387%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/144387/files
Reloadhttps://github.com/python/cpython/pull/144387/files
Reloadhttps://github.com/python/cpython/pull/144387/files
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
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
JelleZijlstrahttps://github.com/JelleZijlstra
python:mainhttps://github.com/python/cpython/tree/main
danielhollas:dataclass-lazy-inspecthttps://github.com/danielhollas/cpython/tree/dataclass-lazy-inspect
Conversation 28 https://github.com/python/cpython/pull/144387
Commits 20 https://github.com/python/cpython/pull/144387/commits
Checks 49 https://github.com/python/cpython/pull/144387/checks
Files changed https://github.com/python/cpython/pull/144387/files
Please reload this pagehttps://github.com/python/cpython/pull/144387/files
gh-137855: Lazy import inspect module in dataclasses https://github.com/python/cpython/pull/144387/files#top
Show all changes 20 commits https://github.com/python/cpython/pull/144387/files
4210688 Lazy import `inspect` in dataclasses module danielhollas Jan 16, 2026 https://github.com/python/cpython/pull/144387/commits/4210688d516aff9286f3fa8735e968be26075377
b1578d5 Simplify vendored unwrap function danielhollas Feb 2, 2026 https://github.com/python/cpython/pull/144387/commits/b1578d5bcb8ecaa02de3e4eb659e48f6093a611a
43e2633 Unvendor unwrap, call it only when needed danielhollas Feb 2, 2026 https://github.com/python/cpython/pull/144387/commits/43e263378b550b79897dcc2c0ee5b39aeb85ce72
6bc6199 Use PEP810! danielhollas Feb 12, 2026 https://github.com/python/cpython/pull/144387/commits/6bc61998db92425f3a7a4e1b14bccabbecbf46df
3901a4c Merge branch 'main' into dataclass-lazy-inspect danielhollas Feb 12, 2026 https://github.com/python/cpython/pull/144387/commits/3901a4c60de59f9aef98dd689f95b14a000178f5
9910cfd Add blurb danielhollas Feb 12, 2026 https://github.com/python/cpython/pull/144387/commits/9910cfd935f4c1aaf23cced3a136a2085c5ea3ff
0d5efb7 Add regression test danielhollas Feb 12, 2026 https://github.com/python/cpython/pull/144387/commits/0d5efb7c75baf4080b5667aa3a551af304cf3ca8
ff59bd3 Remove unrelated change danielhollas Feb 12, 2026 https://github.com/python/cpython/pull/144387/commits/ff59bd3ef63a78cfdc26390c3d6a25d55dccce86
6f0db76 move lazy import danielhollas Feb 13, 2026 https://github.com/python/cpython/pull/144387/commits/6f0db7602bb49b991a633b49a712cd109de15ad2
7c9f75b Lazy import copy module danielhollas Feb 16, 2026 https://github.com/python/cpython/pull/144387/commits/7c9f75b884aeb3c48a6be7455823b1c8154a2383
b577f3b Use the same _AutoDocstring instance danielhollas Feb 16, 2026 https://github.com/python/cpython/pull/144387/commits/b577f3b8cb6286d5a04ee707122eaaa31978a1ea
1703153 Lazy import re module danielhollas Feb 16, 2026 https://github.com/python/cpython/pull/144387/commits/1703153fe6a7f85e1e674b7e26172aec8947c956
b581777 Merge branch 'main' into dataclass-lazy-inspect danielhollas Feb 16, 2026 https://github.com/python/cpython/pull/144387/commits/b581777b7b8db3064a051c35a2b1e32d34bf1b16
820e28c Merge branch 'main' into dataclass-lazy-inspect danielhollas Feb 16, 2026 https://github.com/python/cpython/pull/144387/commits/820e28cdd8cd992b5dcd857fccc577919528369c
f0a4146 Add test for docstring of recursive dataclass danielhollas Feb 17, 2026 https://github.com/python/cpython/pull/144387/commits/f0a4146d21460b015485bcd8a3d45329d427b9e2
80dca62 Merge branch 'main' into dataclass-lazy-inspect danielhollas Feb 17, 2026 https://github.com/python/cpython/pull/144387/commits/80dca621b78ec6e4e697679ada72c5b3fa18a37b
152ae79 Merge branch 'main' into dataclass-lazy-inspect danielhollas Apr 11, 2026 https://github.com/python/cpython/pull/144387/commits/152ae7969be500d3bc6b6590a7b2ad091db59a67
65d92cc Update Lib/dataclasses.py danielhollas Apr 18, 2026 https://github.com/python/cpython/pull/144387/commits/65d92cc12c153e856a72895a7a74a45907e1cef0
0299994 Merge branch 'main' into dataclass-lazy-inspect danielhollas Apr 18, 2026 https://github.com/python/cpython/pull/144387/commits/029999412262d8dfb409d4a498b540ee8600c744
ea805a1 Ensure inspect is not imported in test__colorize.py danielhollas Apr 18, 2026 https://github.com/python/cpython/pull/144387/commits/ea805a199afb4d9d4faad516edbe1069ae7ffa9f
Clear filters https://github.com/python/cpython/pull/144387/files
Please reload this pagehttps://github.com/python/cpython/pull/144387/files
Please reload this pagehttps://github.com/python/cpython/pull/144387/files
dataclasses.py https://github.com/python/cpython/pull/144387/files#diff-44ce2dc1c4922b2f5cf7631d8f86cc569a4c25eb003aaecdc2bc22eb9163d5f5
test__colorize.py https://github.com/python/cpython/pull/144387/files#diff-75225b6c4cf8c610ccbbee08c945f34db965572cbb7c781fcc367e51780978de
__init__.py https://github.com/python/cpython/pull/144387/files#diff-212e368b34eb9b134f87e765787d6d26b747235a358e13da8922f83861c0d676
2026-02-12-18-05-16.gh-issue-137855.2_PTbg.rst https://github.com/python/cpython/pull/144387/files#diff-0f99950667735c0fb6a048cb38b6711d07c9086c1c12e013a6afd89e153c2ff9
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L422
Lib/dataclasses.pyhttps://github.com/python/cpython/pull/144387/files#diff-44ce2dc1c4922b2f5cf7631d8f86cc569a4c25eb003aaecdc2bc22eb9163d5f5
View file https://github.com/danielhollas/cpython/blob/ea805a199afb4d9d4faad516edbe1069ae7ffa9f/Lib/dataclasses.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/144387/{{ revealButtonHref }}
https://github.com/python/cpython/pull/144387/files#diff-44ce2dc1c4922b2f5cf7631d8f86cc569a4c25eb003aaecdc2bc22eb9163d5f5
https://github.com/python/cpython/pull/144387/files#diff-44ce2dc1c4922b2f5cf7631d8f86cc569a4c25eb003aaecdc2bc22eb9163d5f5
https://github.com/python/cpython/pull/144387/files#diff-44ce2dc1c4922b2f5cf7631d8f86cc569a4c25eb003aaecdc2bc22eb9163d5f5
https://github.com/python/cpython/pull/144387/files#diff-44ce2dc1c4922b2f5cf7631d8f86cc569a4c25eb003aaecdc2bc22eb9163d5f5
https://github.com/python/cpython/pull/144387/files#diff-44ce2dc1c4922b2f5cf7631d8f86cc569a4c25eb003aaecdc2bc22eb9163d5f5
https://github.com/python/cpython/pull/144387/files#diff-44ce2dc1c4922b2f5cf7631d8f86cc569a4c25eb003aaecdc2bc22eb9163d5f5
danielhollashttps://github.com/danielhollas
Feb 2, 2026https://github.com/python/cpython/pull/144387/files#r2752620978
Learn morehttps://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment
Please reload this pagehttps://github.com/python/cpython/pull/144387/files
https://github.com/python/cpython/pull/144387/files#diff-44ce2dc1c4922b2f5cf7631d8f86cc569a4c25eb003aaecdc2bc22eb9163d5f5
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L414
Lib/test/test__colorize.pyhttps://github.com/python/cpython/pull/144387/files#diff-75225b6c4cf8c610ccbbee08c945f34db965572cbb7c781fcc367e51780978de
View file https://github.com/danielhollas/cpython/blob/ea805a199afb4d9d4faad516edbe1069ae7ffa9f/Lib/test/test__colorize.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/144387/{{ revealButtonHref }}
https://github.com/python/cpython/pull/144387/files#diff-75225b6c4cf8c610ccbbee08c945f34db965572cbb7c781fcc367e51780978de
https://github.com/python/cpython/pull/144387/files#diff-75225b6c4cf8c610ccbbee08c945f34db965572cbb7c781fcc367e51780978de
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L423
Lib/test/test_dataclasses/__init__.pyhttps://github.com/python/cpython/pull/144387/files#diff-212e368b34eb9b134f87e765787d6d26b747235a358e13da8922f83861c0d676
View file https://github.com/danielhollas/cpython/blob/ea805a199afb4d9d4faad516edbe1069ae7ffa9f/Lib/test/test_dataclasses/__init__.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/144387/{{ revealButtonHref }}
https://github.com/python/cpython/pull/144387/files#diff-212e368b34eb9b134f87e765787d6d26b747235a358e13da8922f83861c0d676
https://github.com/python/cpython/pull/144387/files#diff-212e368b34eb9b134f87e765787d6d26b747235a358e13da8922f83861c0d676
https://github.com/python/cpython/pull/144387/files#diff-212e368b34eb9b134f87e765787d6d26b747235a358e13da8922f83861c0d676
https://github.com/python/cpython/pull/144387/files#diff-212e368b34eb9b134f87e765787d6d26b747235a358e13da8922f83861c0d676
Misc/NEWS.d/next/Library/2026-02-12-18-05-16.gh-issue-137855.2_PTbg.rsthttps://github.com/python/cpython/pull/144387/files#diff-0f99950667735c0fb6a048cb38b6711d07c9086c1c12e013a6afd89e153c2ff9
View file https://github.com/danielhollas/cpython/blob/ea805a199afb4d9d4faad516edbe1069ae7ffa9f/Misc/NEWS.d/next/Library/2026-02-12-18-05-16.gh-issue-137855.2_PTbg.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/144387/{{ revealButtonHref }}
Please reload this pagehttps://github.com/python/cpython/pull/144387/files
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.