Closed
Description
Since Python 3.11, defining generic NamedTuple
s 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 NamedTuple
s fails with a slightly inexplicable error message:
>>> from typing import *
>>> class Foo[T](NamedTuple):
... x: T
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <generic parameters of Foo>
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 2707, in __new__
setattr(nm_tpl, key, ns[key])
AttributeError: attribute '__type_params__' of 'type' objects is not writable
Using 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:
>>> class Bar[T](TypedDict):
... x: T
...
>>> Bar.__type_params__
(T,)
>>> Bar.__orig_bases__
(<function TypedDict at 0x0000017E9A17D9D0>, typing.Generic[T])
>>> Bar.__mro__
(<class '__main__.Bar'>, <class 'typing.Generic'>, <class 'dict'>, <class 'object'>)
Cc. @JelleZijlstra