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
Domain: github.com
{"@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-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2d3e72f2-868c-7e5d-4d69-507d7e0ddf71 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CEDE:12885A:1F88BE:28E11C:6AFF82CC |
| html-safe-nonce | 2882597f37f105ecaef38073e3d114bf05f98c1486578071f50f45f57aff6b2b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRURFOjEyODg1QToxRjg4QkU6MjhFMTFDOjZBRkY4MkNDIiwidmlzaXRvcl9pZCI6IjQ4Nzg0ODg3NjE1ODQ4MTI3NDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 347e9c71f7b5bd887c3aebe6c32c0bd0fd76d2495b70c59c1895e21002d756c0 |
| hovercard-subject-tag | issue:4352908979 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typing/2276/issue_layout |
| twitter:image | https://opengraph.githubassets.com/42ea5716e81ef7fae1b1cb89b65a54fe762d37f1061ed7dbf2a1138f81bd09ce/python/typing/issues/2276 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/42ea5716e81ef7fae1b1cb89b65a54fe762d37f1061ed7dbf2a1138f81bd09ce/python/typing/issues/2276 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mharding-hpe |
| hostname | github.com |
| expected-hostname | github.com |
| None | 19488db4031f00b04ea79b4c70b5dd32cfa873a8d20b3877f3896b8e2d195946 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7952f5285f4ba685d70c4c7540ad3cc9dc9746b8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width