# Buggy code
def copy_method_to_another_class(
    ctx: ClassDefContext,
    self_type: Instance,
    new_method_name: str,
    method_node: FuncDef,
) -> None:
    semanal_api = get_semanal_api(ctx)
    if method_node.type is None:
        if not semanal_api.final_iteration:
            semanal_api.defer()
            return

        arguments, return_type = build_unannotated_method_args(method_node)
        add_method(
            ctx,
            new_method_name,
            args=arguments,
            return_type=return_type,
            self_type=self_type,
        )
        return

    method_type = method_node.type
    if not isinstance(method_type, CallableType):
        if not semanal_api.final_iteration:
            semanal_api.defer()
        return

    arguments = []
    bound_return_type = semanal_api.anal_type(
        method_type.ret_type, allow_placeholder=True
    )

    assert bound_return_type is not None

    if isinstance(bound_return_type, PlaceholderNode):
        return

    for arg_name, arg_type, original_argument in zip(
        method_type.arg_names[1:], method_type.arg_types[1:], method_node.arguments[1:]
    ):
        bound_arg_type = semanal_api.anal_type(arg_type, allow_placeholder=True)
        if bound_arg_type is None and not semanal_api.final_iteration:
            semanal_api.defer()
            return

        assert bound_arg_type is not None

        if isinstance(bound_arg_type, PlaceholderNode):
            return

        var = Var(name=original_argument.variable.name, type=arg_type)
        var.line = original_argument.variable.line
        var.column = original_argument.variable.column
        argument = Argument(
            variable=var,
            type_annotation=bound_arg_type,
            initializer=original_argument.initializer,
            kind=original_argument.kind,
        )
        argument.set_line(original_argument)
        arguments.append(argument)

    add_method(
        ctx,
        new_method_name,
        args=arguments,
        return_type=bound_return_type,
        self_type=self_type,
    )


# Traceback (AttributeError)
./test_app/models.py:6: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.770
Traceback (most recent call last):
File "mypy/semanal.py", line 4679, in accept
File "mypy/nodes.py", line 939, in accept
File "mypy/semanal.py", line 1001, in visit_class_def
File "mypy/semanal.py", line 1078, in analyze_class
File "mypy/semanal.py", line 1087, in analyze_class_body_common
File "mypy/semanal.py", line 1147, in apply_class_plugin_hooks
File "<venv>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/main.py", line 37, in transform_model_class
process_model_class(ctx, django_context)
File "<venv>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/transformers/models.py", line 357, in process_model_class
initializer_cls(ctx, django_context).run()
File "<venv>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/transformers/models.py", line 71, in run
self.run_with_model_cls(model_cls)
File "<venv>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/transformers/models.py", line 231, in run_with_model_cls
base_manager_info=manager_info)
File "<venv>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/transformers/models.py", line 186, in create_new_model_parametrized_manager
method_node=sym.node)
File "<venv>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/lib/helpers.py", line 329, in copy_method_to_another_class
arguments, return_type = build_unannotated_method_args(method_node)
File "<venv>/mypytest/lib/python3.7/site-packages/mypy_django_plugin/lib/helpers.py", line 314, in build_unannotated_method_args
for argument in method_node.arguments[1:]:
AttributeError: attribute 'arguments' of 'FuncItem' undefined
./test_app/models.py:6: : note: use --pdb to drop into pdb

# Fixed code
def copy_method_to_another_class(
    ctx: ClassDefContext,
    self_type: Instance,
    new_method_name: str,
    method_node: FuncDef,
) -> None:
    semanal_api = get_semanal_api(ctx)
    if method_node.type is None:
        if not semanal_api.final_iteration:
            semanal_api.defer()
            return

        arguments, return_type = build_unannotated_method_args(method_node)
        add_method(
            ctx,
            new_method_name,
            args=arguments,
            return_type=return_type,
            self_type=self_type,
        )
        return

    method_type = method_node.type
    if not isinstance(method_type, CallableType):
        if not semanal_api.final_iteration:
            semanal_api.defer()
        return

    arguments = []
    bound_return_type = semanal_api.anal_type(
        method_type.ret_type, allow_placeholder=True
    )

    assert bound_return_type is not None

    if isinstance(bound_return_type, PlaceholderNode):
        return

    try:
        original_arguments = method_node.arguments[1:]
    except AttributeError:
        original_arguments = []

    for arg_name, arg_type, original_argument in zip(
        method_type.arg_names[1:], method_type.arg_types[1:], original_arguments
    ):
        bound_arg_type = semanal_api.anal_type(arg_type, allow_placeholder=True)
        if bound_arg_type is None and not semanal_api.final_iteration:
            semanal_api.defer()
            return

        assert bound_arg_type is not None

        if isinstance(bound_arg_type, PlaceholderNode):
            return

        var = Var(name=original_argument.variable.name, type=arg_type)
        var.line = original_argument.variable.line
        var.column = original_argument.variable.column
        argument = Argument(
            variable=var,
            type_annotation=bound_arg_type,
            initializer=original_argument.initializer,
            kind=original_argument.kind,
        )
        argument.set_line(original_argument)
        arguments.append(argument)

    add_method(
        ctx,
        new_method_name,
        args=arguments,
        return_type=bound_return_type,
        self_type=self_type,
    )