Skip to content

Refine testcase and variable usage for "produce error when assigning NamedTuple to attribute" #8112

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

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool:
self.is_func_scope())
if not is_named_tuple:
return False
if isinstance(s.lvalues[0], MemberExpr):
if isinstance(lvalue, MemberExpr):
self.fail("NamedTuple type as an attribute is not supported", lvalue)
return False
# Yes, it's a valid namedtuple, but defer if it is not ready.
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,12 @@ reveal_type(u.field_1) # N: Revealed type is 'typing.Mapping[Tuple[builtins.int
reveal_type(u.field_2) # N: Revealed type is 'Tuple[builtins.int, builtins.int, fallback=__main__.MyBaseTuple]'
reveal_type(u[0]) # N: Revealed type is 'typing.Mapping[Tuple[builtins.int, builtins.int, fallback=__main__.MyBaseTuple], builtins.int]'
reveal_type(u[1]) # N: Revealed type is 'Tuple[builtins.int, builtins.int, fallback=__main__.MyBaseTuple]'

[case testAssignNamedTupleAsAttribute]
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

reveal_type(A().b) # N: Revealed type is 'Any'
12 changes: 0 additions & 12 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1479,18 +1479,6 @@ def f_suppresses() -> int:
_testUnreachableWithStdlibContextManagersNoStrictOptional.py:9: error: Statement is unreachable
_testUnreachableWithStdlibContextManagersNoStrictOptional.py:15: error: Statement is unreachable

[case testNamedTupleAtRunTime]
from typing import NamedTuple

class A:
def __init__(self) -> None:
self.b = NamedTuple('x', [('s', str), ('n', int)])

reveal_type(A().b)
[out]
_testNamedTupleAtRunTime.py:5: error: NamedTuple type as an attribute is not supported
_testNamedTupleAtRunTime.py:7: note: Revealed type is 'Any'

[case testAsyncioFutureWait]
# mypy: strict-optional
from asyncio import Future, wait
Expand Down