Title: Make abstract/dunder methods positional-only. · Issue #135312 · python/cpython · GitHub
Open Graph Title: Make abstract/dunder methods positional-only. · Issue #135312 · python/cpython
X Title: Make abstract/dunder methods positional-only. · Issue #135312 · python/cpython
Description: Feature or enhancement It would be nice if certain abstract methods / dunder methods would be defined with positional-only arguments, so that when one subclasses them an appropriate name can be chosen, or they can be defined positional-o...
Open Graph Description: Feature or enhancement It would be nice if certain abstract methods / dunder methods would be defined with positional-only arguments, so that when one subclasses them an appropriate name can be cho...
X Description: Feature or enhancement It would be nice if certain abstract methods / dunder methods would be defined with positional-only arguments, so that when one subclasses them an appropriate name can be cho...
Opengraph URL: https://github.com/python/cpython/issues/135312
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Make abstract/dunder methods positional-only.","articleBody":"# Feature or enhancement\n\nIt would be nice if certain abstract methods / dunder methods would be defined with positional-only arguments, so that when one subclasses them an appropriate name can be chosen, or they can be defined positional-only in the subclass.[^1]\n\nCode sample in [pyright playground](https://pyright-play.net/?strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMpgBuApiADZgCGAJgFCiRQDGYZZxTMSYKAzgHSUARk0zZc%2BAMrEAjgFdiKJsVq0mZSr15QAsnGnzFygNoAVALoAKAwqXEz5gJQAuWlHdRqxYFAD6v9hR-S15iMmAXKBBiGDkQFCgABjcPAAEiUgoaFPcvH380GKQYYghg0PCAGkxqAA9nTBQYaoB6RygAWgA%2BKFMG-gGcqHSScio6D09vP19CrhKy3xCw4GqkOobeMiRlVvbuqBsjewt%2Bwcm8mbni0vKVtY3G-AAfKC2d4j3OntMoV6O7A4GlAAMRQQAy5ARRiB1ipJpNorF4rp9LJbCYLJZHLQgA)[^1]\n\n```python\nfrom typing import overload\nfrom collections.abc import Sequence\n\nclass MySequence[T](Sequence[T]):\n def __len__(self): return 0\n @overload\n def __getitem__(self, idx: int, /) -> T: ...\n @overload\n def __getitem__(self, idx: slice, /) -> Sequence[T]: ...\n def __getitem__(self, idx: int | slice, /) -> T | Sequence[T]: # ❌ override\n return MySequence[T]()\n```\n\nIn some cases, the stubs already fake that the argument is positional-only, such as for `Container.__contains__`:\n\n- [`def __contains__(self, x: object, /) -> bool: ...`](https://github.com/python/typeshed/blob/8f5a80e7b741226e525bd90e45496b8f68dc69b6/stdlib/typing.pyi#L623) in `typeshed/stdlib/typing.pyi`\n- [`def __contains__(self, x):`](https://github.com/python/cpython/blob/a58026a5e3da9ca2d09ef51aa90fe217f9a975ec/Lib/_collections_abc.py#L423) in `cpython/Lib/_collections_abc.py`\n\nPersonally, I think only changing the stubs would be a pragmatic choice, but I was told by the maintainers that they believe this change should be done in Cpython first.\n\n## Backward Compatibility\n\n1. Changing non-abstract methods can potentially break user code. For example, this is currently legal code at runtime\n\n ```python\n from collections.abc import Container.\n Container.__subclasshook__(C=list) # True\n ```\n\n2. Changing the signature of abstract methods can introduce type-checker errors Code sample in [pyright playground](https://pyright-play.net/?strict=true&code=GYJw9gtgBAxmA28CmMAuBLMA7AzgOgEMAjGKdCABzBFSgGUkBHAVySxiQCgATJYKAOZJUACgAeALnpNW7JAG1g8MAVQBdADRksvSdtQBKKAFoAfFCUrUEzlDtQQw5iCxQxeAPoehGVEgheIug6SGIAvMG6Brb2QA)[^1]\n\n ```python\n from collections.abc import Sequence\n def get(x: Sequence[float], index: int) -> float:\n return x.__getitem__(index=index)\n ```\n\nWith this in mind, I believe changing abstrac dunder methods to use positional only arguments should be relatively safe, as calling them directly with keyword is very uncommon, and at worst it will introduce type errors.\n\nPersonally, I'd also like so see methods like `Container.__subclasshook__` use positional-only arguments, but I also understand if this is not feasible due to bc-conerns.\n\n[^1]: Note that `mypy` special cases certain dunder methods, and yields different results than `pyright` on these examples\n\n\n### Has this already been discussed elsewhere?\n\nhttps://github.com/python/typeshed/issues/14071\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-136324\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/randolf-scholz","@type":"Person","name":"randolf-scholz"},"datePublished":"2025-06-09T20:50:54Z","url":"https://github.com/python/cpython/issues/135312"}
| 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:8ac35643-bbbf-8532-965c-b580aa1cef8c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D1D4:30263F:2156470:2D5755A:6B002454 |
| html-safe-nonce | badb766211dbfe1f278f93578337449127f28e45f539284276d9ad2d802db30c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMUQ0OjMwMjYzRjoyMTU2NDcwOjJENTc1NUE6NkIwMDI0NTQiLCJ2aXNpdG9yX2lkIjoiMTY2MTIwNjMzMTU1NTU4NzE1NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b513eb1dfa7c51b0f564b67ab5b52226dc58d379a5738ba1d67c967be4dfa69a |
| hovercard-subject-tag | issue:3131461191 |
| 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/cpython/135312/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9f4dd0ae4e2c4408850e8abaafb44548deab9bc3cd5f9e85fd8e3f22bddd69ac/python/cpython/issues/135312 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9f4dd0ae4e2c4408850e8abaafb44548deab9bc3cd5f9e85fd8e3f22bddd69ac/python/cpython/issues/135312 |
| og:image:alt | Feature or enhancement It would be nice if certain abstract methods / dunder methods would be defined with positional-only arguments, so that when one subclasses them an appropriate name can be cho... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | randolf-scholz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width