-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Support generic NamedTuples #685
Comments
I think you're seeing the same error as Jukka in python/typing#279.
|
The code should work, but I think that the I don't think it would be a lot of work to implement, but anything involving generics is at least a little tricky. |
Yeah, still reproducible. Here's a simpler repro:
Output:
|
But it's no longer an exception in mypy, it just prints an error. |
Here is a rough idea how it might be possible to implement generic named tuples (note that I haven't spend a lot of time on this so this might all make no sense):
|
I have a partially working implementation of generic namedtuples on top of #3081; I can work more on it once that one is merged. My general approach is just to make namedtuples more and more like normal classes. |
#3134 too has some support for generic namedtuple falling out of the implementation. @JelleZijlstra #3081 is indeed conflicting. The general idea is similar, except I do not make the namedtuple look like a class, but rather change it to manipulate the (I did not mean to overlap with your work; it happened through the course of refactoring. I think I should try to find a way for these PRs to live together. But I guess #3134 will have hard time to merge anyway due to code churn). |
#1682 provides an additional repro that should be added to tests when this is fixed. |
Any progress on this? |
Raising priority to normal, since this appeared several times (still not high priority, since the fix is non-trivial). |
Another spelling that would be nice if it worked is from typing import Generic, NamedTuple, TypeVar
T = TypeVar('T')
class Result(Generic[T], NamedTuple):
result: T
other_data: str
result: Result[int] = Result(result=1, other_data='a') As of Mypy version 0.711, this gives the following error:
|
As a result of this bug, the type annotation for |
I'd like to see this working. Presently I want this code:
But need to use a dataclass instead, which unfortunately is mutable and can't be deconstructed like a tuple.
|
this seems to also be an issue when attempting to extend from typing import TypeVar
T = TypeVar("T")
class Foo(tuple[T, T]): pass
a: Foo[int] # error: Generic tuple types not supported [misc] is this intended behavior? i don't see why this shouldn't be allowed |
Have the same Problem like @DetachHead is it as compilcated to fix as for NamedTuples? Is it the same error? |
To update on this issue: generic NamedTuples are now supported by the runtime in 3.11, and python/typing_extensions#7 tracks the progress of a backport via |
Fixes #685 This builds on top of some infra I added for recursive types (Ref #13297). Implementation is based on the idea in #13297 (comment). Generally it works well, but there are actually some problems for named tuples that are recursive. Special-casing them in `maptype.py` is a bit ugly, but I think this is best we can get at the moment.
I tried this for a generic NamedTuple:
Mypy raised an exception:
RuntimeError: TypeVarType is already analysed
.Related question: is this the right way to define a generic NamedTuple?
Thanks!
The text was updated successfully, but these errors were encountered: