Skip to content

PEP 695 syntax doesn't work for generic NamedTuples#104616

@AlexWaygood

Description

@AlexWaygood

Since Python 3.11, defining generic NamedTuples has been legal, like so:

>>> from typing import* >>> T = TypeVar("T") >>> classFoo(NamedTuple, Generic[T]): ... x: T >>>

However, using the new PEP-695 syntax for generic NamedTuples 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 TypedDicts 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

Metadata

Metadata

Assignees

Labels

3.12only security fixestopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions