Title: PEP 695 syntax doesn't work for generic NamedTuples · Issue #104616 · python/cpython · GitHub
Open Graph Title: PEP 695 syntax doesn't work for generic NamedTuples · Issue #104616 · python/cpython
X Title: PEP 695 syntax doesn't work for generic NamedTuples · Issue #104616 · python/cpython
Description: Since Python 3.11, defining generic NamedTuples has been legal, like so: >>> from typing import * >>> T = TypeVar("T") >>> class Foo(NamedTuple, Generic[T]): ... x: T >>> However, using the new PEP-695 syntax for generic NamedTuples fail...
Open Graph Description: Since Python 3.11, defining generic NamedTuples has been legal, like so: >>> from typing import * >>> T = TypeVar("T") >>> class Foo(NamedTuple, Generic[T]): ... x: T >>> However, using the new PEP...
X Description: Since Python 3.11, defining generic NamedTuples has been legal, like so: >>> from typing import * >>> T = TypeVar("T") >>> class Foo(NamedTuple, Generic[T]): .....
Opengraph URL: https://github.com/python/cpython/issues/104616
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PEP 695 syntax doesn't work for generic NamedTuples","articleBody":"Since Python 3.11, defining generic `NamedTuple`s has been legal, like so:\r\n\r\n```pycon\r\n\u003e\u003e\u003e from typing import *\r\n\u003e\u003e\u003e T = TypeVar(\"T\")\r\n\u003e\u003e\u003e class Foo(NamedTuple, Generic[T]):\r\n... x: T\r\n\u003e\u003e\u003e\r\n```\r\n\r\nHowever, using the new PEP-695 syntax for generic `NamedTuple`s fails with a slightly inexplicable error message:\r\n\r\n```pycon\r\n\u003e\u003e\u003e from typing import *\r\n\u003e\u003e\u003e class Foo[T](NamedTuple):\r\n... x: T\r\n...\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"\u003cstdin\u003e\", line 1, in \u003cgeneric parameters of Foo\u003e\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\typing.py\", line 2707, in __new__\r\n setattr(nm_tpl, key, ns[key])\r\nAttributeError: attribute '__type_params__' of 'type' objects is not writable\r\n```\r\n\r\nUsing PEP-695 syntax for generic `TypedDict`s seems to work fine, but isn't tested anywhere in `test_typing.py`, as far as I can tell:\r\n\r\n```pycon\r\n\u003e\u003e\u003e class Bar[T](TypedDict):\r\n... x: T\r\n...\r\n\u003e\u003e\u003e Bar.__type_params__\r\n(T,)\r\n\u003e\u003e\u003e Bar.__orig_bases__\r\n(\u003cfunction TypedDict at 0x0000017E9A17D9D0\u003e, typing.Generic[T])\r\n\u003e\u003e\u003e Bar.__mro__\r\n(\u003cclass '__main__.Bar'\u003e, \u003cclass 'typing.Generic'\u003e, \u003cclass 'dict'\u003e, \u003cclass 'object'\u003e)\r\n```\r\n\r\nCc. @JelleZijlstra","author":{"url":"https://github.com/AlexWaygood","@type":"Person","name":"AlexWaygood"},"datePublished":"2023-05-18T16:20:14.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/104616/cpython/issues/104616"}
| 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:91ebcfe1-2b94-d854-e4fc-2de7106c125d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C48:84862:242F6:3336F:696A0B86 |
| html-safe-nonce | 9d109b8bd52212018624ab9ad5e49af40539737397d3cfbf2deeca3e3c1476be |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzQ4Ojg0ODYyOjI0MkY2OjMzMzZGOjY5NkEwQjg2IiwidmlzaXRvcl9pZCI6IjI5OTAxMjM1MjcwNjc3OTg0MDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 86ec829b5be85d009e9f8a4e474a1a1622d73f493806a7bb95a90d48abeb934a |
| hovercard-subject-tag | issue:1715908302 |
| 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/104616/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b99af22a0806d676628bf9efcb29bc91c19969bf75b3e6702534565de695279a/python/cpython/issues/104616 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b99af22a0806d676628bf9efcb29bc91c19969bf75b3e6702534565de695279a/python/cpython/issues/104616 |
| og:image:alt | Since Python 3.11, defining generic NamedTuples has been legal, like so: >>> from typing import * >>> T = TypeVar("T") >>> class Foo(NamedTuple, Generic[T]): ... x: T >>> However, using the new PEP... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AlexWaygood |
| hostname | github.com |
| expected-hostname | github.com |
| None | 699227a00bbb7fe1eec276d2ae1c3a93068bc5ba483bd9dc4b2a27a8f4f2f595 |
| 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 | 7266b2d935baa1c6474b16dd9feaa5ca30607261 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width