-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Unsupported type Type[...] error when instantiating NamedTuple from class variable #4300
Comments
The really odd thing is that removing the |
FWIW I have seen a very similar problem when working on self types in generic classes. Hopefully, my fix will also fix this one. |
One more example: import typing
T = typing.TypeVar('T')
L = typing.TypeVar('L', bound='List')
class List(typing.List[T]):
def __init__(self, iterable: typing.Iterable = ()) -> None:
super().__init__(iterable)
def copy(self: L) -> L:
return type(self)(self) |
But this works: import typing
L = typing.TypeVar('L', bound='List')
class List(typing.List):
def __init__(self, iterable: typing.Iterable = ()) -> None:
super().__init__(iterable)
def copy(self: L) -> L:
return type(self)(self) So if I do not use |
It looks like instantiating (calling)
Maybe there are some more details. |
And |
Is there any way to silence this error? |
Hm... this is the problematic part. It looks like it is impossible to silence the error if the |
The issue is also that this is not available: #626 |
There's a test (by @gvanrossum) describing that you can't take the type of a Tuple from when mypy/test-data/unit/check-classes.test Lines 2432 to 2437 in d8892c4
Since the above is basically taking The behavior is basically caused by there being no case for Line 649 in d8892c4
The error with generics is related to the same function and also seems like a deliberate choice by @gvanrossum (https://github.com/python/mypy/blob/master/mypy/checkexpr.p#L672-L673). |
Note the difference between IMO the only real issue is that
doesn't give an error unless you try to call |
|
That's a good point. Maybe PEP 484 should be clearer on this (especially if
we actually get a PR that fixes this for NamedTuple).
|
Because Oddly (to me), this behavior does work when simply subclassing
This happens because |
There's nothing "normal" about NamedTuple. :-( Note that when you subclass tuple, it ends up being a variable-length tuple that's homogeneously typed (all items have the same type). Both NamedTuple and Tuple are fixed-length tuples with heterogeneous types (each item has its own type). That's the reason they are special. |
Sorry, thanks for explaining that. |
One minor comment to future self: the formatting for this error is also wrong it looks like
instead of
|
It also doesn't feature a line number, so though I see:
I'm having a hard time tracking down the cause... this particular file doesn't even import edit: In case it's of use to someone else, in my case the cause was an assignment like |
We should at the very least add the line number to the error. This is already a high priority issue. |
All the original examples now pass correctly, the only remaining problem is that now reasonably invalid (from mypy's point of view) things like
|
A similar example just came in #7086. |
Hello ! Any news on this ? |
This seems to work now using the latest git master. |
Run mypy 0.550 on the following code
I see
error: Unsupported type Type["A"]
on the lines forA2
anddef j
, but not in other places, as indicated.I'm probably hitting an edge case for
NamedTuple
here.The text was updated successfully, but these errors were encountered: