Skip to content

Combining Generic and NamedTuple #653

@DrPyser

Description

@DrPyser

Currently, in python 3.7, it seems impossible to properly compose typing.NamedTuple and typing.Generic. That is, I want to do this:

import typing
T = typing.TypeVar("T")

class Test(typing.NamedTuple, typing.Generic[T]):
    a: T

So this works, the class can be used as a NamedTuple.
However:

x: Test[int] = Test(a=1) # TypeError: 'type' object is not subscriptable

Right, subclassing NamedTuple generates a class that inherits only from tuple.

Maybe this was discussed before, I've looked a bit in this repo and haven't found much, except #588, which doesn't really address this.

Is there any plan or possibility for this to be resolved? Maybe more generally, allowing NamedTuple to be composable with other types(I know this might be difficult because of metaclass conflicts).

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions