René's URL Explorer Experiment


Title: Context manager support for `contextvars.Context` · Issue #99633 · python/cpython · GitHub

Open Graph Title: Context manager support for `contextvars.Context` · Issue #99633 · python/cpython

X Title: Context manager support for `contextvars.Context` · Issue #99633 · python/cpython

Description: Feature or enhancement I would like to add __enter__ and __exit__ methods to contextvars.Context such that these two are mostly equivalent: contextvars.copy_context().run(do_something_interesting) and: with contextvars.copy_context(): do...

Open Graph Description: Feature or enhancement I would like to add __enter__ and __exit__ methods to contextvars.Context such that these two are mostly equivalent: contextvars.copy_context().run(do_something_interesting) ...

X Description: Feature or enhancement I would like to add __enter__ and __exit__ methods to contextvars.Context such that these two are mostly equivalent: contextvars.copy_context().run(do_something_interesting) ...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Context manager support for `contextvars.Context`","articleBody":"# Feature or enhancement\r\n\r\nI would like to add `__enter__` and `__exit__` methods to [`contextvars.Context`](https://docs.python.org/3/library/contextvars.html#contextvars.Context) such that these two are mostly equivalent:\r\n\r\n```python\r\ncontextvars.copy_context().run(do_something_interesting)\r\n```\r\n\r\nand:\r\n\r\n```python\r\nwith contextvars.copy_context():\r\n    do_something_interesting()\r\n```\r\n\r\n# Pitch\r\n\r\nThis makes it possible to combine Context enter/exit with other context managers:\r\n\r\n```python\r\nimport contextlib\r\nimport contextvars\r\n\r\n@contextlib.contextmanager\r\ndef foo():\r\n    with contextvars.copy_context():\r\n        set_some_context_vars()\r\n        yield\r\n```\r\n\r\nMy personal motivating interest is controlling ContextVar values in pytest fixtures.  For example:\r\n\r\n```python\r\nimport contextvars\r\nimport pytest\r\nimport module_under_test\r\n\r\n@pytest.fixture\r\ndef clean_slate():\r\n    with contextvars.copy_context():\r\n        module_under_test._internal_state_context_var.set(None)\r\n        yield\r\n\r\ndef test_foo(clean_slate):\r\n    assert not module_under_test.is_initialized()\r\n```\r\n\r\n# Previous discussion\r\n\r\nhttps://stackoverflow.com/q/59264158\r\n\r\n\u003c!-- gh-linked-prs -->\r\n### Linked PRs\r\n* gh-124773\r\n* gh-124776\r\n* gh-99634\r\n\u003c!-- /gh-linked-prs -->\r\n","author":{"url":"https://github.com/rhansen","@type":"Person","name":"rhansen"},"datePublished":"2022-11-21T06:02:27Z","url":"https://github.com/python/cpython/issues/99633"}

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:9bcc5163-8c74-1038-885a-59163e9f3c08
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id86B0:4BB79:4300527:5567E60:6B023A94
html-safe-nonce4dd031412b1bfdc0cf22dbc1292632c57e26cdae349e3b9fec60db89a1234500
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkIwOjRCQjc5OjQzMDA1Mjc6NTU2N0U2MDo2QjAyM0E5NCIsInZpc2l0b3JfaWQiOiIyMTE5MTYxNzIwNjE3NDUwMTMyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmaccdf0a9f24c4947141c96da37737b56f08ffefafa846e0235fc3ac38047c7581a
hovercard-subject-tagissue:1457353795
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/99633/issue_layout
twitter:imagehttps://opengraph.githubassets.com/d71c47aca60fd7c2845bc13f7193340a59eea945773298e93e7ab15cdf14375f/python/cpython/issues/99633
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/d71c47aca60fd7c2845bc13f7193340a59eea945773298e93e7ab15cdf14375f/python/cpython/issues/99633
og:image:altFeature or enhancement I would like to add __enter__ and __exit__ methods to contextvars.Context such that these two are mostly equivalent: contextvars.copy_context().run(do_something_interesting) ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamerhansen
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/99633#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F99633
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%2F99633
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/99633
Reloadhttps://github.com/python/cpython/issues/99633
Reloadhttps://github.com/python/cpython/issues/99633
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/99633
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/99633
Context manager support for contextvars.Contexthttps://github.com/python/cpython/issues/99633#top
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/rhansen
rhansenhttps://github.com/rhansen
on Nov 21, 2022https://github.com/python/cpython/issues/99633#issue-1457353795
contextvars.Contexthttps://docs.python.org/3/library/contextvars.html#contextvars.Context
https://stackoverflow.com/q/59264158https://stackoverflow.com/q/59264158
gh-124872: Refine contextvars documentation #124773https://github.com/python/cpython/pull/124773
gh-124872: Replace enter/exit events with "switched" #124776https://github.com/python/cpython/pull/124776
gh-99633: Add context manager support to contextvars.Context #99634https://github.com/python/cpython/pull/99634
Nexthttps://github.com/python/cpython/issues/99633?timeline_page=1
rhansenhttps://github.com/rhansen
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
on Nov 21, 2022https://github.com/python/cpython/issues/99633#event-7854080001
rhansenhttps://github.com/rhansen
on Nov 21, 2022https://github.com/python/cpython/issues/99633#event-7854217827
pythongh-99633: Add context manager support to `contextvars.Context`https://github.com/rhansen/cpython/commit/45a83271d7101888622405a7d13a0a6d8394c1ad
pythongh-99633https://github.com/python/cpython/issues/99633
45a8327https://github.com/rhansen/cpython/commit/45a83271d7101888622405a7d13a0a6d8394c1ad
sobolevnhttps://github.com/sobolevn
topic-asynciohttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-asyncio%22
on Nov 21, 2022https://github.com/python/cpython/issues/99633#event-7856177585
ezio-melottihttps://github.com/ezio-melotti
asynciohttps://github.com/orgs/python/projects/29
on Nov 21, 2022https://github.com/python/cpython/issues/99633
ezio-melottihttps://github.com/ezio-melotti
asynciohttps://github.com/orgs/python/projects/29
on Nov 21, 2022https://github.com/python/cpython/issues/99633#event-15231021126
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Nov 21, 2022https://github.com/python/cpython/issues/99633#issuecomment-1322658777
@1st1https://github.com/1st1
rhansenhttps://github.com/rhansen
on Nov 21, 2022https://github.com/python/cpython/issues/99633#event-7861536702
pythongh-99633: Add context manager support to `contextvars.Context`https://github.com/rhansen/cpython/commit/eb110b6895b575dd54976fbe5801342e672004d3
pythongh-99633https://github.com/python/cpython/issues/99633
eb110b6https://github.com/rhansen/cpython/commit/eb110b6895b575dd54976fbe5801342e672004d3
rhansenhttps://github.com/rhansen
on Feb 2, 2023https://github.com/python/cpython/issues/99633#event-8417733834
pythongh-99633: Add context manager support to `contextvars.Context`https://github.com/rhansen/cpython/commit/f5ef259390df69d1a6c993b3694c1773aafcca0a
pythongh-99633https://github.com/python/cpython/issues/99633
f5ef259https://github.com/rhansen/cpython/commit/f5ef259390df69d1a6c993b3694c1773aafcca0a
https://github.com/rhansen
rhansenhttps://github.com/rhansen
on Feb 2, 2023https://github.com/python/cpython/issues/99633#issuecomment-1413243460
@1st1https://github.com/1st1
kumaraditya303https://github.com/kumaraditya303
topic-asynciohttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-asyncio%22
on Feb 3, 2023https://github.com/python/cpython/issues/99633#event-8434307427
kumaraditya303https://github.com/kumaraditya303
asynciohttps://github.com/orgs/python/projects/29
on Feb 3, 2023https://github.com/python/cpython/issues/99633
iritkatrielhttps://github.com/iritkatriel
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
on Nov 27, 2023https://github.com/python/cpython/issues/99633#event-11074632966
https://github.com/apostolos-geyer
apostolos-geyerhttps://github.com/apostolos-geyer
on May 26, 2024https://github.com/python/cpython/issues/99633#issuecomment-2131894699
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on May 26, 2024https://github.com/python/cpython/issues/99633#issuecomment-2131977723
rhansenhttps://github.com/rhansen
on Jul 10, 2024https://github.com/python/cpython/issues/99633#event-13463267649
pythongh-99633: Add context manager support to `contextvars.Context`https://github.com/rhansen/cpython/commit/b21b76f3b137105f9e9d559040e8e15b71b5df79
pythongh-99633https://github.com/python/cpython/issues/99633
b21b76fhttps://github.com/rhansen/cpython/commit/b21b76f3b137105f9e9d559040e8e15b71b5df79
rhansenhttps://github.com/rhansen
on Oct 7, 2024https://github.com/python/cpython/issues/99633#event-14547952477
pythongh-99633: Inline independent coroutine checkhttps://github.com/rhansen/cpython/commit/cc607c121b932e5381bf29f48a97bd4d6520fdd7
pythongh-99633https://github.com/python/cpython/issues/99633
cc607c1https://github.com/rhansen/cpython/commit/cc607c121b932e5381bf29f48a97bd4d6520fdd7
pythongh-99633: Add context manager support to `contextvars.Context`https://github.com/rhansen/cpython/commit/f3a5f93ec2df0a31def5eba3b4dc6971c54406af
pythongh-99633https://github.com/python/cpython/issues/99633
f3a5f93https://github.com/rhansen/cpython/commit/f3a5f93ec2df0a31def5eba3b4dc6971c54406af
pythongh-99633: _contextvars: Change private types to reduce castinghttps://github.com/rhansen/cpython/commit/1e02793f0c265a1f1cc4b96ca36d141c7c78fe06
pythongh-99633https://github.com/python/cpython/issues/99633
1e02793https://github.com/rhansen/cpython/commit/1e02793f0c265a1f1cc4b96ca36d141c7c78fe06
pythongh-99633: _contextvars: Clarify that the ref is stolen, not bor…https://github.com/rhansen/cpython/commit/11c500b21097bb23b109e2453fb81038af721b3d
pythongh-99633https://github.com/python/cpython/issues/99633
11c500bhttps://github.com/rhansen/cpython/commit/11c500b21097bb23b109e2453fb81038af721b3d
pythongh-99633: _contextvars: Enter the thread's initial contexthttps://github.com/rhansen/cpython/commit/543df99a2566fccd63e63c4dd68a722f4f0181f2
pythongh-99633https://github.com/python/cpython/issues/99633
543df99https://github.com/rhansen/cpython/commit/543df99a2566fccd63e63c4dd68a722f4f0181f2
pythongh-99633: _contextvars: Call non-tstate variantshttps://github.com/rhansen/cpython/commit/f6ae9d7a3da04067e3eaf3cd48810d9fcc1b83fe
pythongh-99633https://github.com/python/cpython/issues/99633
f6ae9d7https://github.com/rhansen/cpython/commit/f6ae9d7a3da04067e3eaf3cd48810d9fcc1b83fe
pythongh-99633: New generator/coroutine `_context` propertyhttps://github.com/rhansen/cpython/commit/d08b88d941bc13b9ffab771a1566d6fba93a497a
pythongh-99633https://github.com/python/cpython/issues/99633
d08b88dhttps://github.com/rhansen/cpython/commit/d08b88d941bc13b9ffab771a1566d6fba93a497a
pythongh-99633: Inline independent coroutine checkhttps://github.com/rhansen/cpython/commit/37c3544c8723f34d6f14ea81a817c7303f9708a7
pythongh-99633https://github.com/python/cpython/issues/99633
37c3544https://github.com/rhansen/cpython/commit/37c3544c8723f34d6f14ea81a817c7303f9708a7
pythongh-99633: Add context manager support to `contextvars.Context`https://github.com/rhansen/cpython/commit/01afd3c227de36afa69404ff35f0558d14ce28a5
pythongh-99633https://github.com/python/cpython/issues/99633
01afd3chttps://github.com/rhansen/cpython/commit/01afd3c227de36afa69404ff35f0558d14ce28a5
pythongh-124872: Replace enter/exit events with "switched"https://github.com/rhansen/cpython/commit/b4cff7700cbc47aa3cfcb6400dea2456b8825ca5
pythongh-124872https://github.com/python/cpython/issues/124872
b4cff77https://github.com/rhansen/cpython/commit/b4cff7700cbc47aa3cfcb6400dea2456b8825ca5
pythongh-124872: Replace enter/exit events with "switched"https://github.com/rhansen/cpython/commit/d8985251e73b4c69001285b7ea04abde26c463e8
pythongh-124872https://github.com/python/cpython/issues/124872
d898525https://github.com/rhansen/cpython/commit/d8985251e73b4c69001285b7ea04abde26c463e8
1st1https://github.com/1st1
on Oct 14, 2024https://github.com/python/cpython/issues/99633#event-14645020629
gh-124872: Replace enter/exit events with "switched" (#124776)https://github.com/python/cpython/commit/843d28f59d2616d052d9d45f31823976da07f0f3
gh-124872https://github.com/python/cpython/issues/124872
#124776https://github.com/python/cpython/pull/124776
843d28fhttps://github.com/python/cpython/commit/843d28f59d2616d052d9d45f31823976da07f0f3
Eclips4https://github.com/Eclips4
on Oct 16, 2024https://github.com/python/cpython/issues/99633#event-14673307467
pythongh-124872: Replace enter/exit events with "switched" (python#12…https://github.com/Eclips4/cpython/commit/f0b3aa862b629690496d0336904a9bb1ada6dd47
pythongh-124872https://github.com/python/cpython/issues/124872
python#12…https://github.com/python/cpython/pull/124776
f0b3aa8https://github.com/Eclips4/cpython/commit/f0b3aa862b629690496d0336904a9bb1ada6dd47
ambvhttps://github.com/ambv
on Oct 16, 2024https://github.com/python/cpython/issues/99633#event-14676558847
gh-124872: Replace enter/exit events with "switched" (#125532)https://github.com/python/cpython/commit/bee112a94d688c8048ddeddaa7bbd5150aecad11
gh-124872https://github.com/python/cpython/issues/124872
#125532https://github.com/python/cpython/pull/125532
bee112ahttps://github.com/python/cpython/commit/bee112a94d688c8048ddeddaa7bbd5150aecad11
ebonnalhttps://github.com/ebonnal
on Jan 12, 2025https://github.com/python/cpython/issues/99633#event-15894557733
pythongh-124872: Replace enter/exit events with "switched" (python#12…https://github.com/ebonnal/cpython/commit/077ed71bd23529bc64f12d5f4437f7b4a416e2b3
pythongh-124872https://github.com/python/cpython/issues/124872
python#12…https://github.com/python/cpython/pull/125532
077ed71https://github.com/ebonnal/cpython/commit/077ed71bd23529bc64f12d5f4437f7b4a416e2b3
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/99633
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/99633
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%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.