# Buggy code
def add_method(
    ctx: ClassDefContext,
    name: str,
    args: List[Argument],
    return_type: Type,
    self_type: Optional[Type] = None,
    tvar_def: Optional[TypeVarDef] = None,
) -> None:
    """Adds a new method to a class."""
    info = ctx.cls.info

    # First remove any previously generated methods with the same name
    # to avoid clashes and problems in new semantic analyzer.
    if name in info.names:
        sym = info.names[name]
        if sym.plugin_generated and isinstance(sym.node, FuncDef):
            ctx.cls.defs.body.remove(sym.node)

    self_type = self_type or fill_typevars(info)
    function_type = ctx.api.named_type("__builtins__.function")

    args = [Argument(Var("self"), self_type, None, ARG_POS)] + args
    arg_types, arg_names, arg_kinds = [], [], []
    for arg in args:
        assert arg.type_annotation, "All arguments must be fully typed."
        arg_types.append(arg.type_annotation)
        arg_names.append(arg.variable.name())
        arg_kinds.append(arg.kind)

    signature = CallableType(
        arg_types, arg_kinds, arg_names, return_type, function_type
    )
    if tvar_def:
        signature.variables = [tvar_def]

    func = FuncDef(name, args, Block([PassStmt()]))
    func.info = info
    func.type = set_callable_name(signature, func)
    func._fullname = info.fullname() + "." + name
    func.line = info.line

    info.names[name] = SymbolTableNode(MDEF, func, plugin_generated=True)
    info.defn.defs.body.append(func)


# 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 add_method(
    ctx: ClassDefContext,
    name: str,
    args: List[Argument],
    return_type: Type,
    self_type: Optional[Type] = None,
    tvar_def: Optional[TypeVarDef] = None,
) -> None:
    """Adds a new method to a class."""
    info = ctx.cls.info

    # First remove any previously generated methods with the same name
    # to avoid clashes and problems in new semantic analyzer.
    if name in info.names:
        sym = info.names[name]
        if sym.plugin_generated and isinstance(sym.node, FuncDef):
            ctx.cls.defs.body.remove(sym.node)

    self_type = self_type or fill_typevars(info)
    function_type = ctx.api.named_type("__builtins__.function")

    args = [Argument(Var("self"), self_type, None, ARG_POS)] + args
    arg_types, arg_names, arg_kinds = [], [], []
    for arg in args:
        assert arg.type_annotation, "All arguments must be fully typed."
        arg_types.append(arg.type_annotation)
        arg_names.append(arg.variable.name())
        arg_kinds.append(arg.kind)

    signature = CallableType(
        arg_types, arg_kinds, arg_names, return_type, function_type
    )
    if tvar_def:
        signature.variables = [tvar_def]

    func = FuncDef(name, args, Block([PassStmt()]))
    func.info = info
    func.type = set_callable_name(signature, func)
    func._fullname = info.fullname() + "." + name
    func.line = info.line

    # NOTE: we would like the plugin generated node to dominate, but we still
    # need to keep any existing definitions so they get semantically analyzed.
    if name in info.names:
        # Get a nice unique name instead.
        r_name = get_unique_redefinition_name(name, info.names)
        info.names[r_name] = info.names[name]

    info.names[name] = SymbolTableNode(MDEF, func, plugin_generated=True)
    info.defn.defs.body.append(func)