You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NamedTuple triggered mypy error inside a class #7531
This is expected (although the error message could be better). The NamedTuple constructor isn't meant to be used at runtime in a class constructor; instead you should define your NamedTuple type in the module scope.
@JukkaL
I created a test to fix this issue but the test causes some exceptions:
[case testNamedTupleAtRunTime]
from typing import NamedTuple
class A:
def __init__(self) -> None:
self.b = NamedTuple('x', [('s', str), ('n', int)]) # E: NamedTuple type as an attribute is not supported
Activity
JelleZijlstra commentedon Sep 19, 2019
This is expected (although the error message could be better). The NamedTuple constructor isn't meant to be used at runtime in a class constructor; instead you should define your NamedTuple type in the module scope.
JukkaL commentedon Sep 20, 2019
It would be better if mypy would at least tell that defining a NamedTuple type as an attribute is not supported.
AFanaei commentedon Oct 7, 2019
@JukkaL
I created a test to fix this issue but the test causes some exceptions:
could you give some hint what is wrong?
JukkaL commentedon Oct 8, 2019
@AFanaei Try adding
[builtins fixtures/list.pyi]to the end of the test case.Produce error when NamedTuple is assigned to attribute
Produce error when NamedTuple is assigned to attribute
Produce error when NamedTuple is assigned to attribute (#7662)