Skip to content

Fix nested namedtuple crash in incremental mode #12803

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 12 commits into from
May 17, 2022
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
23 changes: 13 additions & 10 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ def analyze_namedtuple_classdef(self, defn: ClassDef) -> bool:
is_named_tuple, info = True, defn.info # type: bool, Optional[TypeInfo]
else:
is_named_tuple, info = self.named_tuple_analyzer.analyze_namedtuple_classdef(
defn, self.is_stub_file)
defn, self.is_stub_file, self.is_func_scope())
if is_named_tuple:
if info is None:
self.mark_incomplete(defn.name, defn)
Expand Down Expand Up @@ -1462,7 +1462,10 @@ def prepare_class_def(self, defn: ClassDef, info: Optional[TypeInfo] = None) ->
info._fullname = self.qualified_name(defn.name)
else:
info._fullname = info.name
self.add_symbol(defn.name, defn.info, defn)
local_name = defn.name
if '@' in local_name:
local_name = local_name.split('@')[0]
self.add_symbol(local_name, defn.info, defn)
if self.is_nested_within_func_scope():
# We need to preserve local classes, let's store them
# in globals under mangled unique names
Expand All @@ -1471,17 +1474,17 @@ def prepare_class_def(self, defn: ClassDef, info: Optional[TypeInfo] = None) ->
# incremental mode and we should avoid it. In general, this logic is too
# ad-hoc and needs to be removed/refactored.
if '@' not in defn.info._fullname:
local_name = defn.info.name + '@' + str(defn.line)
if defn.info.is_named_tuple:
# Module is already correctly set in _fullname for named tuples.
defn.info._fullname += '@' + str(defn.line)
else:
defn.info._fullname = self.cur_mod_id + '.' + local_name
global_name = defn.info.name + '@' + str(defn.line)
defn.info._fullname = self.cur_mod_id + '.' + global_name
else:
# Preserve name from previous fine-grained incremental run.
local_name = defn.info.name
global_name = defn.info.name
defn.fullname = defn.info._fullname
self.globals[local_name] = SymbolTableNode(GDEF, defn.info)
if defn.info.is_named_tuple:
# Named tuple nested within a class is stored in the class symbol table.
self.add_symbol_skip_local(global_name, defn.info)
else:
self.globals[global_name] = SymbolTableNode(GDEF, defn.info)

def make_empty_type_info(self, defn: ClassDef) -> TypeInfo:
if (self.is_module_scope()
Expand Down
5 changes: 4 additions & 1 deletion mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def __init__(self, options: Options, api: SemanticAnalyzerInterface) -> None:
self.options = options
self.api = api

def analyze_namedtuple_classdef(self, defn: ClassDef, is_stub_file: bool
def analyze_namedtuple_classdef(self, defn: ClassDef, is_stub_file: bool,
is_func_scope: bool
) -> Tuple[bool, Optional[TypeInfo]]:
"""Analyze if given class definition can be a named tuple definition.

Expand All @@ -70,6 +71,8 @@ def analyze_namedtuple_classdef(self, defn: ClassDef, is_stub_file: bool
# This is a valid named tuple, but some types are incomplete.
return True, None
items, types, default_items = result
if is_func_scope and '@' not in defn.name:
defn.name += '@' + str(defn.line)
info = self.build_namedtuple_typeinfo(
defn.name, items, types, default_items, defn.line)
defn.info = info
Expand Down
53 changes: 53 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -5658,3 +5658,56 @@ class D(C):
[out]
[out2]
tmp/a.py:9: error: Trying to assign name "z" that is not in "__slots__" of type "a.D"

[case testIncrementalWithDifferentKindsOfNestedTypesWithinMethod]
# flags: --python-version 3.7

import a

[file a.py]
import b

[file a.py.2]
import b
b.xyz

[file b.py]
from typing import NamedTuple, NewType
from typing_extensions import TypedDict, TypeAlias
from enum import Enum
from dataclasses import dataclass

class C:
def f(self) -> None:
class C:
c: int
class NT1(NamedTuple):
c: int
NT2 = NamedTuple("NT2", [("c", int)])
class NT3(NT1):
pass
class TD(TypedDict):
c: int
TD2 = TypedDict("TD2", {"c": int})
class E(Enum):
X = 1
@dataclass
class DC:
c: int
Alias: TypeAlias = NT1
N = NewType("N", NT1)

c: C = C()
nt1: NT1 = NT1(c=1)
nt2: NT2 = NT2(c=1)
nt3: NT3 = NT3(c=1)
td: TD = TD(c=1)
td2: TD2 = TD2(c=1)
e: E = E.X
dc: DC = DC(c=1)
al: Alias = Alias(c=1)
n: N = N(NT1(c=1))

[builtins fixtures/dict.pyi]
[out2]
tmp/a.py:2: error: "object" has no attribute "xyz"