Title: Specialization of namedtuple types · Issue #92107 · python/cpython · GitHub
Open Graph Title: Specialization of namedtuple types · Issue #92107 · python/cpython
X Title: Specialization of namedtuple types · Issue #92107 · python/cpython
Description: collections.namedtuple() creates a tuple subclass, and as a subclass it inherits all tuple methods, including __class_getitem__. The __new__ method has been overridden in a named tuple class. It no longer accept an iterator of arbitrary ...
Open Graph Description: collections.namedtuple() creates a tuple subclass, and as a subclass it inherits all tuple methods, including __class_getitem__. The __new__ method has been overridden in a named tuple class. It no...
X Description: collections.namedtuple() creates a tuple subclass, and as a subclass it inherits all tuple methods, including __class_getitem__. The __new__ method has been overridden in a named tuple class. It no...
Opengraph URL: https://github.com/python/cpython/issues/92107
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Specialization of namedtuple types","articleBody":"`collections.namedtuple()` creates a `tuple` subclass, and as a subclass it inherits all `tuple` methods, including `__class_getitem__`.\r\n\r\nThe `__new__` method has been overridden in a named tuple class. It no longer accept an iterator of arbitrary length. The named tuple has fixed number of elements, and the constructor accept fixed number of arguments (some may be optional).\r\n\r\nBut the `__class_getitem__` is inherited from `tuple`. It allows to specialize the named tuple type with arbitrary number of arguments.\r\n```python\r\nPoint2D = namedtuple('Point2D', 'x y label')\r\nPoint2D[int, int, str]\r\nPoint2D[int, str]\r\nPoint2D[int, ...]\r\n```\r\nIt looks confusing at best. A named tuple is not a general tuple, it cannot have an arbitrary number of items.\r\n\r\nThere are two options:\r\n\r\n1. Forbid subscription of types created by `namedtuple()`. If you want to add type annotations, use `typing.NamedTuple`.\r\n2. Add `__class_getitem__` which checks that the number of types corresponds to the size of the named tuple.\r\n\r\n@rhettinger ","author":{"url":"https://github.com/serhiy-storchaka","@type":"Person","name":"serhiy-storchaka"},"datePublished":"2022-05-01T06:00:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/92107/cpython/issues/92107"}
| 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:9538c8a0-9605-4d38-5a4b-9ad836599c6a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 970E:2B644A:3A0F7D:4BADB0:696B3EB3 |
| html-safe-nonce | 875c104c3b98381bfab3c03418c4b6806d081331355ac28e4650f4a2ff8cd184 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzBFOjJCNjQ0QTozQTBGN0Q6NEJBREIwOjY5NkIzRUIzIiwidmlzaXRvcl9pZCI6IjgxOTYxMDkwNjIyMTA1MzUwOTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0fe85280a2aa7a5f98c6a696e2e5e42c8f0c074ff3d48f0a9de9c8feaeeafa15 |
| hovercard-subject-tag | issue:1222033790 |
| 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/92107/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a3654a193f8acc0291286b29e731c6017430894804df98226d9d0ee2e198610e/python/cpython/issues/92107 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a3654a193f8acc0291286b29e731c6017430894804df98226d9d0ee2e198610e/python/cpython/issues/92107 |
| og:image:alt | collections.namedtuple() creates a tuple subclass, and as a subclass it inherits all tuple methods, including __class_getitem__. The __new__ method has been overridden in a named tuple class. It no... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | serhiy-storchaka |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width