Skip to content

Commit 871a871

Browse files
authored
New semantic analyzer: fix deserialization of generated classes (#6627)
1 parent a4447c2 commit 871a871

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

mypy/newsemanal/semanal.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2802,7 +2802,14 @@ def process_typevar_parameters(self, args: List[Expression],
28022802

28032803
def basic_new_typeinfo(self, name: str, basetype_or_fallback: Instance) -> TypeInfo:
28042804
class_def = ClassDef(name, Block([]))
2805-
class_def.fullname = self.qualified_name(name)
2805+
if self.is_func_scope() and not self.type:
2806+
# Full names of generated classes should always be prefixed with the module names
2807+
# even if they are nested in a function, since these classes will be (de-)serialized.
2808+
# (Note that the caller should append @line to the name to avoid collisions.)
2809+
# TODO: clean this up, see #6422.
2810+
class_def.fullname = self.cur_mod_id + '.' + self.qualified_name(name)
2811+
else:
2812+
class_def.fullname = self.qualified_name(name)
28062813

28072814
info = TypeInfo(SymbolTable(), class_def, self.cur_mod_id)
28082815
class_def.info = info

test-data/unit/check-incremental.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4949,6 +4949,25 @@ NT = NamedTuple('BadName', [('x', int)])
49494949
[out2]
49504950
tmp/a.py:3: error: Revealed type is 'Tuple[builtins.int, fallback=b.BadName@2]'
49514951

4952+
[case testNewAnalyzerIncrementalBrokenNamedTupleNested]
4953+
# flags: --new-semantic-analyzer
4954+
import a
4955+
[file a.py]
4956+
from b import C
4957+
x: C
4958+
[file a.py.2]
4959+
from b import C
4960+
x: C
4961+
# touch
4962+
[file b.py]
4963+
class C: ...
4964+
from collections import namedtuple
4965+
def test() -> None:
4966+
NT = namedtuple('BadName', ['x', 'y'])
4967+
[builtins fixtures/list.pyi]
4968+
[out]
4969+
[out2]
4970+
49524971
[case testNewAnalyzerIncrementalMethodNamedTuple]
49534972
# flags: --new-semantic-analyzer
49544973
import a

0 commit comments

Comments
 (0)