# Buggy code
def build_typeddict_typeinfo(
    self, name: str, items: List[str], types: List[Type], required_keys: Set[str]
) -> TypeInfo:
    fallback = (
        self.named_type_or_none("typing.Mapping", [self.str_type(), self.object_type()])
        or self.object_type()
    )
    info = self.basic_new_typeinfo(name, fallback)
    info.typeddict_type = TypedDictType(
        OrderedDict(zip(items, types)), required_keys, fallback
    )

    def patch() -> None:
        # Calculate the correct value type for the fallback Mapping.
        assert info.typeddict_type, "TypedDict type deleted before calling the patch"
        fallback.args[1] = join.join_type_list(list(info.typeddict_type.items.values()))

    # We can't calculate the complete fallback type until after semantic
    # analysis, since otherwise MROs might be incomplete. Postpone a callback
    # function that patches the fallback.
    self.patches.append(patch)
    return info


# Traceback (RuntimeError)
forward.py:1: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.540
Traceback (most recent call last):
File "/Users/joshstaiger/anaconda/bin/mypy", line 11, in <module>
sys.exit(console_entry())
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
main(None)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/main.py", line 50, in main
res = type_check_only(sources, bin_dir, options)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/main.py", line 103, in type_check_only
options=options)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 201, in build
graph = dispatch(sources, manager)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 1867, in dispatch
process_graph(graph, manager)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 2117, in process_graph
process_stale_scc(graph, scc, manager)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 2214, in process_stale_scc
graph[id].semantic_analysis_pass_three()
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 1760, in semantic_analysis_pass_three
self.options, patches)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/semanal_pass3.py", line 56, in visit_file
self.accept(file_node)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/semanal_pass3.py", line 73, in accept
node.accept(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/nodes.py", line 228, in accept
return visitor.visit_mypy_file(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/traverser.py", line 30, in visit_mypy_file
d.accept(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/nodes.py", line 794, in accept
return visitor.visit_assignment_stmt(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/semanal_pass3.py", line 187, in visit_assignment_stmt
self.analyze(s.rvalue.analyzed.type, s.rvalue.analyzed, warn=True)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/semanal_pass3.py", line 311, in analyze
type.accept(analyzer)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/types.py", line 476, in accept
return visitor.visit_instance(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/typeanal.py", line 716, in visit_instance
if not is_subtype(arg, bound):
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/subtypes.py", line 83, in is_subtype
ignore_declared_variance=ignore_declared_variance))
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/types.py", line 1227, in accept
return visitor.visit_union_type(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/subtypes.py", line 312, in visit_union_type
for item in left.items)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/subtypes.py", line 312, in <genexpr>
for item in left.items)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/subtypes.py", line 83, in is_subtype
ignore_declared_variance=ignore_declared_variance))
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/types.py", line 1412, in accept
return visitor.visit_forwardref_type(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/types.py", line 1497, in visit_forwardref_type
raise RuntimeError('Internal error: unresolved forward reference')
RuntimeError: Internal error: unresolved forward reference
forward.py:1: : note: use --pdb to drop into pdb

# Fixed code
def build_typeddict_typeinfo(
    self, name: str, items: List[str], types: List[Type], required_keys: Set[str]
) -> TypeInfo:
    fallback = (
        self.named_type_or_none("typing.Mapping", [self.str_type(), self.object_type()])
        or self.object_type()
    )
    info = self.basic_new_typeinfo(name, fallback)
    info.typeddict_type = TypedDictType(
        OrderedDict(zip(items, types)), required_keys, fallback
    )

    def patch() -> None:
        # Calculate the correct value type for the fallback Mapping.
        assert info.typeddict_type, "TypedDict type deleted before calling the patch"
        fallback.args[1] = join.join_type_list(list(info.typeddict_type.items.values()))

    # We can't calculate the complete fallback type until after semantic
    # analysis, since otherwise MROs might be incomplete. Postpone a callback
    # function that patches the fallback.
    self.patches.append((PRIORITY_FALLBACKS, patch))
    return info