René's URL Explorer Experiment


Title: Allow Self or Self-like type arguments for generics · Issue #2276 · python/typing · GitHub

Open Graph Title: Allow Self or Self-like type arguments for generics · Issue #2276 · python/typing

X Title: Allow Self or Self-like type arguments for generics · Issue #2276 · python/typing

Description: from contextlib import AbstractContextManager class ManagerA(AbstractContextManager): def __init__(self, x: int) -> None: self._x = x def __enter__(self) -> int: return self._x This can raise type check errors because AbstractContextMana...

Open Graph Description: from contextlib import AbstractContextManager class ManagerA(AbstractContextManager): def __init__(self, x: int) -> None: self._x = x def __enter__(self) -> int: return self._x This can raise type ...

X Description: from contextlib import AbstractContextManager class ManagerA(AbstractContextManager): def __init__(self, x: int) -> None: self._x = x def __enter__(self) -> int: return self._x This can raise...

Opengraph URL: https://github.com/python/typing/issues/2276

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow Self or Self-like type arguments for generics","articleBody":"```python\nfrom contextlib import AbstractContextManager\n\nclass ManagerA(AbstractContextManager):\n    def __init__(self, x: int) -> None:\n        self._x = x\n    def __enter__(self) -> int:\n        return self._x\n```\n\nThis can raise type check errors because `AbstractContextManager` is a generic type, and this code omits the type argument. Fair enough, and easy to fix:\n\n```python\nfrom contextlib import AbstractContextManager\n\nclass ManagerA(AbstractContextManager[int]):\n    def __init__(self, x: int) -> None:\n        self._x = x\n    def __enter__(self) -> int:\n        return self._x\n```\n\nBut what if we are not overriding the `__enter__` method? Consider this example:\n\n```python\nfrom contextlib import AbstractContextManager\n\nclass ManagerB(AbstractContextManager):\n    def __init__(self, x: int) -> None:\n        self._x = x\n    # Other methods are defined, but __enter__ is not overridden\n```\n\nIn this case, there is no great way to accurate type annotate it in order to reflect the fact that the `__enter__` method always returns the object that it was called on (which is the default implementation of the `__enter__` method in `AbstractContextManager`.\n\nIf we were willing to mark this class as `final`, then we could annotate it as:\n```python\nclass ManagerB(AbstractContextManager[\"ManagerB\"]):\n```\n\nAnd that would work. But if we want to be able to subclass it, then this would no longer be accurate. Consider:\n\n```python\nfrom contextlib import AbstractContextManager\nfrom types import TracebackType\nfrom typing import Type\n\nclass ManagerB(AbstractContextManager[\"ManagerB\"]):\n    def __init__(self, x: int) -> None:\n        self._x = x\n\n    # Other methods are defined, but __enter__ is not overridden\n\n    def __exit__(\n            self, exc_type: Type[BaseException] | None,\n            exc_val: BaseException | None,\n            exc_tb: TracebackType | None) -> bool | None:\n        return None\n\nclass SubManager(ManagerB):\n    def harf(self) -> None:\n        print(\"harf\")\n\nwith SubManager(1) as subman:\n    subman.harf()\n```\n\nThis runs fine, but the type checker will rightly complain. The annotations indicate that `subman` should be of type `ManagerB`, but `ManagerB` has no `harf` attribute.\n\nIt *feels* like it would be appropriate to use:\n\n```python\nclass ManagerB(AbstractContextManager[Self]):\n```\n\nBut `Self` is not allowed to be used in this context.\n\nThe feature I would like to see is some way to accurately handle non-final subclasses of `AbstractContextManager` that do not overwrite the `__enter__` method (or that overwrite it but still return `self`).\n\nObviously this issue applies generally to generics -- this is just an obvious example since the Python implementation of that class makes it impossible to type annotate many of its subclasses. But ideally any solution would apply generally to generics -- essentially giving a way to use `Self` (or something `Self`-like) as the type argument for a generic.","author":{"url":"https://github.com/mharding-hpe","@type":"Person","name":"mharding-hpe"},"datePublished":"2026-04-29T18:46:03Z","url":"https://github.com/python/typing/issues/2276"}

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:2d3e72f2-868c-7e5d-4d69-507d7e0ddf71
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idCEDE:12885A:1F88BE:28E11C:6AFF82CC
html-safe-nonce2882597f37f105ecaef38073e3d114bf05f98c1486578071f50f45f57aff6b2b
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRURFOjEyODg1QToxRjg4QkU6MjhFMTFDOjZBRkY4MkNDIiwidmlzaXRvcl9pZCI6IjQ4Nzg0ODg3NjE1ODQ4MTI3NDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac347e9c71f7b5bd887c3aebe6c32c0bd0fd76d2495b70c59c1895e21002d756c0
hovercard-subject-tagissue:4352908979
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/typing/2276/issue_layout
twitter:imagehttps://opengraph.githubassets.com/42ea5716e81ef7fae1b1cb89b65a54fe762d37f1061ed7dbf2a1138f81bd09ce/python/typing/issues/2276
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/42ea5716e81ef7fae1b1cb89b65a54fe762d37f1061ed7dbf2a1138f81bd09ce/python/typing/issues/2276
og:image:altfrom contextlib import AbstractContextManager class ManagerA(AbstractContextManager): def __init__(self, x: int) -> None: self._x = x def __enter__(self) -> int: return self._x This can raise type ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemharding-hpe
hostnamegithub.com
expected-hostnamegithub.com
None19488db4031f00b04ea79b4c70b5dd32cfa873a8d20b3877f3896b8e2d195946
turbo-cache-controlno-preview
go-importgithub.com/python/typing git https://github.com/python/typing.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id24604008
octolytics-dimension-repository_nwopython/typing
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24604008
octolytics-dimension-repository_network_root_nwopython/typing
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
release7952f5285f4ba685d70c4c7540ad3cc9dc9746b8
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/typing/issues/2276#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fissues%2F2276
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%2Ftyping%2Fissues%2F2276
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%2Ftyping
Reloadhttps://github.com/python/typing/issues/2276
Reloadhttps://github.com/python/typing/issues/2276
Reloadhttps://github.com/python/typing/issues/2276
pythonhttps://github.com/python
typinghttps://github.com/python/typing
Codehttps://github.com/python/typing
Issues206 (206)https://github.com/python/typing/issues
Pull requests27 (27)https://github.com/python/typing/pulls
Discussionshttps://github.com/python/typing/discussions
Actionshttps://github.com/python/typing/actions
Security and qualityhttps://github.com/python/typing/security
Insightshttps://github.com/python/typing/insights
https://github.com/python/typing
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/2276
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/2276
Allow Self or Self-like type arguments for genericshttps://github.com/python/typing/issues/2276#top
topic: featureDiscussions about new features for Python's type annotationshttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22topic%3A%20feature%22
https://github.com/mharding-hpe
mharding-hpehttps://github.com/mharding-hpe
on Apr 29, 2026https://github.com/python/typing/issues/2276#issue-4352908979
mharding-hpehttps://github.com/mharding-hpe
topic: featureDiscussions about new features for Python's type annotationshttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22topic%3A%20feature%22
on Apr 29, 2026https://github.com/python/typing/issues/2276#event-25004201875
https://github.com/mharding-hpe
mharding-hpehttps://github.com/mharding-hpe
on May 6, 2026https://github.com/python/typing/issues/2276#issuecomment-4389771746
python/typeshed#13950https://github.com/python/typeshed/issues/13950
python/mypy#17758https://github.com/python/mypy/issues/17758
https://github.com/mitcharf
mitcharfhttps://github.com/mitcharf
on May 12, 2026https://github.com/python/typing/issues/2276#issuecomment-4432609209
https://discuss.python.org/t/self-as-typevar-default/90939https://discuss.python.org/t/self-as-typevar-default/90939
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typing/issues/2276
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typing/issues/2276
topic: featureDiscussions about new features for Python's type annotationshttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22topic%3A%20feature%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.