# Buggy code
def save_namedtuple_body(self, named_tuple_info: TypeInfo) -> Iterator[None]:
    """Preserve the generated body of class-based named tuple and then restore it.

    Temporarily clear the names dict so we don't get errors about duplicate names
    that were already set in build_namedtuple_typeinfo (we already added the tuple
    field names while generating the TypeInfo, and actual duplicates are
    already reported).
    """
    nt_names = named_tuple_info.names
    named_tuple_info.names = SymbolTable()

    yield

    # Make sure we didn't use illegal names, then reset the names in the typeinfo.
    for prohibited in NAMEDTUPLE_PROHIBITED_NAMES:
        if prohibited in named_tuple_info.names:
            if nt_names.get(prohibited) is named_tuple_info.names[prohibited]:
                continue
            ctx = named_tuple_info.names[prohibited].node
            assert ctx is not None
            self.fail(
                'Cannot overwrite NamedTuple attribute "{}"'.format(prohibited), ctx
            )

    # Restore the names in the original symbol table. This ensures that the symbol
    # table contains the field objects created by build_namedtuple_typeinfo. Exclude
    # __doc__, which can legally be overwritten by the class.
    named_tuple_info.names.update(
        {
            key: value
            for key, value in nt_names.items()
            if key not in named_tuple_info.names or key != "__doc__"
        }
    )


# Traceback (AssertionError)
mypy . --new-semantic-analyzer --show-traceback
./test.py: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.710+dev.16ade15bcd2a5b6b02ad2c76b6b13efa6c2ddf0a
Traceback (most recent call last):
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/bin/mypy", line 10, in <module>
sys.exit(console_entry())
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/__main__.py", line 8, in console_entry
main(None, sys.stdout, sys.stderr)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/main.py", line 88, in main
res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/build.py", line 168, in build
result = _build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/build.py", line 228, in _build
graph = dispatch(sources, manager, stdout)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/build.py", line 2574, in dispatch
process_graph(graph, manager)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/build.py", line 2887, in process_graph
process_stale_scc(graph, scc, manager)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/build.py", line 2985, in process_stale_scc
semantic_analysis_for_scc(graph, scc, manager.errors)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/newsemanal/semanal_main.py", line 80, in semantic_analysis_for_scc
check_type_arguments(graph, scc, errors)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/newsemanal/semanal_main.py", line 338, in check_type_arguments
state.tree.accept(analyzer)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/build.py", line 1832, in wrap_context
yield
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/newsemanal/semanal_main.py", line 338, in check_type_arguments
state.tree.accept(analyzer)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/nodes.py", line 281, in accept
return visitor.visit_mypy_file(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/newsemanal/semanal_typeargs.py", line 33, in visit_mypy_file
super().visit_mypy_file(o)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/traverser.py", line 33, in visit_mypy_file
d.accept(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/nodes.py", line 913, in accept
return visitor.visit_class_def(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/newsemanal/semanal_typeargs.py", line 44, in visit_class_def
super().visit_class_def(defn)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/mixedtraverser.py", line 28, in visit_class_def
super().visit_class_def(o)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/traverser.py", line 65, in visit_class_def
o.defs.accept(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/nodes.py", line 978, in accept
return visitor.visit_block(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/newsemanal/semanal_typeargs.py", line 48, in visit_block
super().visit_block(o)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/traverser.py", line 37, in visit_block
s.accept(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/nodes.py", line 655, in accept
return visitor.visit_func_def(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/traverser.py", line 52, in visit_func_def
self.visit_func(o)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/newsemanal/semanal_typeargs.py", line 40, in visit_func
super().visit_func(defn)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/mixedtraverser.py", line 23, in visit_func
self.visit_optional_type(o.type)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/mixedtraverser.py", line 91, in visit_optional_type
t.accept(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/types.py", line 975, in accept
return visitor.visit_callable_type(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/typetraverser.py", line 52, in visit_callable_type
t.fallback.accept(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/types.py", line 666, in accept
return visitor.visit_instance(self)
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/newsemanal/semanal_typeargs.py", line 54, in visit_instance
for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
File "/Users/reinoud/Envs/tmp-4caf160a3041c9e/lib/python3.7/site-packages/mypy/nodes.py", line 2600, in __getattribute__
raise AssertionError(object.__getattribute__(self, 'msg'))
AssertionError: fallback can't be filled out until semanal
./test.py: : note: use --pdb to drop into pdb

# Fixed code
def save_namedtuple_body(self, named_tuple_info: TypeInfo) -> Iterator[None]:
    """Preserve the generated body of class-based named tuple and then restore it.

    Temporarily clear the names dict so we don't get errors about duplicate names
    that were already set in build_namedtuple_typeinfo (we already added the tuple
    field names while generating the TypeInfo, and actual duplicates are
    already reported).
    """
    nt_names = named_tuple_info.names
    named_tuple_info.names = SymbolTable()

    yield

    # Make sure we didn't use illegal names, then reset the names in the typeinfo.
    for prohibited in NAMEDTUPLE_PROHIBITED_NAMES:
        if prohibited in named_tuple_info.names:
            if nt_names.get(prohibited) is named_tuple_info.names[prohibited]:
                continue
            ctx = named_tuple_info.names[prohibited].node
            assert ctx is not None
            self.fail(
                'Cannot overwrite NamedTuple attribute "{}"'.format(prohibited), ctx
            )

    # Restore the names in the original symbol table. This ensures that the symbol
    # table contains the field objects created by build_namedtuple_typeinfo. Exclude
    # __doc__, which can legally be overwritten by the class.
    for key, value in nt_names.items():
        if key in named_tuple_info.names:
            if key == "__doc__":
                continue
            # Keep existing (user-provided) definitions under mangled names, so they
            # get semantically analyzed.
            r_key = get_unique_redefinition_name(key, named_tuple_info.names)
            named_tuple_info.names[r_key] = named_tuple_info.names[key]
        named_tuple_info.names[key] = value