Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining Generic and NamedTuple #653

Closed
DrPyser opened this issue Jun 22, 2019 · 6 comments
Closed

Combining Generic and NamedTuple #653

DrPyser opened this issue Jun 22, 2019 · 6 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@DrPyser
Copy link

DrPyser commented Jun 22, 2019

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!

@DrPyser
Copy link
Author

DrPyser commented Jun 22, 2019

Something of a workaround:

import typing
T = typing.TypeVar("T")
class Test(typing.NamedTuple):
    a: T
class Test(Test, typing.Generic[T]): pass

x: Test[int] = Test(a=1) # works fine

But unsurprisingly mypy won't accept this:

$ mypy /tmp/test.py 
/tmp/test.py:6: error: Invalid type "test.T"
/tmp/test.py:8: error: Name 'Test' already defined on line 5
/tmp/test.py:11: error: Generic tuple types not supported
/tmp/test.py:12: error: Generic tuple types not supported

@lemon24
Copy link

lemon24 commented Mar 4, 2020

It seems this is being worked on in python/mypy#685.

@illeatmyhat
Copy link

illeatmyhat commented Apr 29, 2020

It's also impossible to do

from typing import TypedDict, TypeVar, Generic

T = TypeVar('T')
class Test(TypedDict, Generic[T]):
    a: T

with error
TypedDict cannot inherit from a non-TypedDict base class

@srittau
Copy link
Collaborator

srittau commented Nov 4, 2021

If a type checker doesn't support this, this seems to be more of a type checker issue than a general typing issue, so I'm closing this here.

@srittau srittau closed this as completed Nov 4, 2021
@Gobot1234
Copy link
Contributor

Typing currently bans this in mro entries

@srittau srittau reopened this Nov 4, 2021
@srittau srittau added the topic: feature Discussions about new features for Python's type annotations label Nov 4, 2021
@AlexWaygood
Copy link
Member

This is now:

So I think this feature request can be closed as "accepted"!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

7 participants