From 90146f8cad292045ae31a0618006f934b2105f04 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Thu, 4 Apr 2019 15:17:43 +0100 Subject: [PATCH 1/2] New semantic analyzer: fix deserealization of generated classes --- mypy/newsemanal/semanal.py | 4 ++++ test-data/unit/check-incremental.test | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/mypy/newsemanal/semanal.py b/mypy/newsemanal/semanal.py index 87d398c6664e..7063ca32323a 100644 --- a/mypy/newsemanal/semanal.py +++ b/mypy/newsemanal/semanal.py @@ -2803,6 +2803,10 @@ def process_typevar_parameters(self, args: List[Expression], def basic_new_typeinfo(self, name: str, basetype_or_fallback: Instance) -> TypeInfo: class_def = ClassDef(name, Block([])) class_def.fullname = self.qualified_name(name) + if self.is_func_scope() and not self.type: + # Full names of generated classes should always be prefixed with the module names + # even if they are nested in a function, since these classes will be (de-)serialized. + class_def.fullname = self.cur_mod_id + '.' + class_def.fullname info = TypeInfo(SymbolTable(), class_def, self.cur_mod_id) class_def.info = info diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index 3da160a59d2a..fe8973d4f9ba 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -4949,6 +4949,25 @@ NT = NamedTuple('BadName', [('x', int)]) [out2] tmp/a.py:3: error: Revealed type is 'Tuple[builtins.int, fallback=b.BadName@2]' +[case testNewAnalyzerIncrementalBrokenNamedTupleNested] +# flags: --new-semantic-analyzer +import a +[file a.py] +from b import C +x: C +[file a.py.2] +from b import C +x: C +# touch +[file b.py] +class C: ... +from collections import namedtuple +def test() -> None: + NT = namedtuple('BadName', ['x', 'y']) +[builtins fixtures/list.pyi] +[out] +[out2] + [case testNewAnalyzerIncrementalMethodNamedTuple] # flags: --new-semantic-analyzer import a From fc316b9cce541b147f82c173dd91c951be807871 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Thu, 4 Apr 2019 15:55:51 +0100 Subject: [PATCH 2/2] Address CR --- mypy/newsemanal/semanal.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mypy/newsemanal/semanal.py b/mypy/newsemanal/semanal.py index 7063ca32323a..519f6709ba10 100644 --- a/mypy/newsemanal/semanal.py +++ b/mypy/newsemanal/semanal.py @@ -2802,11 +2802,14 @@ def process_typevar_parameters(self, args: List[Expression], def basic_new_typeinfo(self, name: str, basetype_or_fallback: Instance) -> TypeInfo: class_def = ClassDef(name, Block([])) - class_def.fullname = self.qualified_name(name) if self.is_func_scope() and not self.type: # Full names of generated classes should always be prefixed with the module names # even if they are nested in a function, since these classes will be (de-)serialized. - class_def.fullname = self.cur_mod_id + '.' + class_def.fullname + # (Note that the caller should append @line to the name to avoid collisions.) + # TODO: clean this up, see #6422. + class_def.fullname = self.cur_mod_id + '.' + self.qualified_name(name) + else: + class_def.fullname = self.qualified_name(name) info = TypeInfo(SymbolTable(), class_def, self.cur_mod_id) class_def.info = info