Skip to content

Fix and store fallbacks for incomplete named tuples #5429

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
Aug 8, 2018
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
14 changes: 12 additions & 2 deletions mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ def check_namedtuple(self,
items, types, defaults, ok = self.parse_namedtuple_args(call, fullname)
if not ok:
# Error. Construct dummy return value.
return self.build_namedtuple_typeinfo('namedtuple', [], [], {})
if var_name:
name = var_name
else:
name = 'namedtuple@' + str(call.line)
info = self.build_namedtuple_typeinfo(name, [], [], {})
self.store_namedtuple_info(info, name, call, is_typed)
return info
name = cast(StrExpr, call.args[0]).value
if name != var_name or is_func_scope:
# Give it a unique name derived from the line number.
Expand All @@ -166,11 +172,15 @@ def check_namedtuple(self,
info = self.build_namedtuple_typeinfo(name, items, types, default_items)
# Store it as a global just in case it would remain anonymous.
# (Or in the nearest class if there is one.)
self.store_namedtuple_info(info, name, call, is_typed)
return info

def store_namedtuple_info(self, info: TypeInfo, name: str,
call: CallExpr, is_typed: bool) -> None:
stnode = SymbolTableNode(GDEF, info)
self.api.add_symbol_table_node(name, stnode)
call.analyzed = NamedTupleExpr(info, is_typed=is_typed)
call.analyzed.set_line(call.line, call.column)
return info

def parse_namedtuple_args(self, call: CallExpr, fullname: str
) -> Tuple[List[str], List[Type], List[Expression], bool]:
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -7260,6 +7260,26 @@ x = [] # type: List
[out]
==

[case testNamedTupleFallbackModule]
import b
[file b.py]
from a import A
def f(a: A):
pass
[file b.py.2]
from a import A
def f(a: A):
reveal_type(a)
[file a.py]
from typing import NamedTuple

F = [('x', int)]
A = NamedTuple('A', F) # type: ignore
[builtins fixtures/list.pyi]
[out]
==
b.py:3: error: Revealed type is 'Tuple[, fallback=a.A]'

[case testImportOnTopOfAlias1]
from a import A
x: A
Expand Down