# Buggy code
def generate_c_type_stub(
    module: ModuleType,
    class_name: str,
    obj: type,
    output: List[str],
    imports: List[str],
    sigs: Optional[Dict[str, str]] = None,
    class_sigs: Optional[Dict[str, str]] = None,
) -> None:
    """Generate stub for a single class using runtime introspection.

    The result lines will be appended to 'output'. If necessary, any
    required names will be added to 'imports'.
    """
    # typeshed gives obj.__dict__ the not quite correct type Dict[str, Any]
    # (it could be a mappingproxy!), which makes mypyc mad, so obfuscate it.
    obj_dict = getattr(obj, "__dict__")  # type: Mapping[str, Any]  # noqa
    items = sorted(obj_dict.items(), key=lambda x: method_name_sort_key(x[0]))
    methods = []  # type: List[str]
    types = []  # type: List[str]
    static_properties = []  # type: List[str]
    rw_properties = []  # type: List[str]
    ro_properties = []  # type: List[str]
    done = set()  # type: Set[str]
    for attr, value in items:
        if is_c_method(value) or is_c_classmethod(value):
            done.add(attr)
            if not is_skipped_attribute(attr):
                if attr == "__new__":
                    # TODO: We should support __new__.
                    if "__init__" in obj_dict:
                        # Avoid duplicate functions if both are present.
                        # But is there any case where .__new__() has a
                        # better signature than __init__() ?
                        continue
                    attr = "__init__"
                if is_c_classmethod(value):
                    methods.append("@classmethod")
                    self_var = "cls"
                else:
                    self_var = "self"
                generate_c_function_stub(
                    module,
                    attr,
                    value,
                    methods,
                    imports=imports,
                    self_var=self_var,
                    sigs=sigs,
                    class_name=class_name,
                    class_sigs=class_sigs,
                )
        elif is_c_property(value):
            done.add(attr)
            generate_c_property_stub(
                attr,
                value,
                static_properties,
                rw_properties,
                ro_properties,
                is_c_property_readonly(value),
                module=module,
                imports=imports,
            )
        elif is_c_type(value):
            generate_c_type_stub(
                module,
                attr,
                value,
                types,
                imports=imports,
                sigs=sigs,
                class_sigs=class_sigs,
            )
            done.add(attr)

    for attr, value in items:
        if is_skipped_attribute(attr):
            continue
        if attr not in done:
            static_properties.append(
                "%s: ClassVar[%s] = ..."
                % (
                    attr,
                    strip_or_import(get_type_fullname(type(value)), module, imports),
                )
            )
    all_bases = obj.mro()
    if all_bases[-1] is object:
        # TODO: Is this always object?
        del all_bases[-1]
    # remove pybind11_object. All classes generated by pybind11 have pybind11_object in their MRO,
    # which only overrides a few functions in object type
    if all_bases and all_bases[-1].__name__ == "pybind11_object":
        del all_bases[-1]
    # remove the class itself
    all_bases = all_bases[1:]
    # Remove base classes of other bases as redundant.
    bases = []  # type: List[type]
    for base in all_bases:
        if not any(issubclass(b, base) for b in bases):
            bases.append(base)
    if bases:
        bases_str = "(%s)" % ", ".join(
            strip_or_import(get_type_fullname(base), module, imports) for base in bases
        )
    else:
        bases_str = ""
    if types or static_properties or rw_properties or methods or ro_properties:
        output.append("class %s%s:" % (class_name, bases_str))
        for line in types:
            if (
                output
                and output[-1]
                and not output[-1].startswith("class")
                and line.startswith("class")
            ):
                output.append("")
            output.append("    " + line)
        for line in static_properties:
            output.append("    %s" % line)
        for line in rw_properties:
            output.append("    %s" % line)
        for line in methods:
            output.append("    %s" % line)
        for line in ro_properties:
            output.append("    %s" % line)
    else:
        output.append("class %s%s: ..." % (class_name, bases_str))


# Traceback (TypeError)
PS C:\Users\Gen\Repos> stubgen -m av.enum
Traceback (most recent call last):
File "c:\users\gen\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\gen\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\Gen\AppData\Local\Programs\Python\Python39\Scripts\stubgen.exe\__main__.py", line 7, in <module>
File "mypy\stubgen.py", line 1582, in main
File "mypy\stubgen.py", line 1475, in generate_stubs
File "mypy\stubgenc.py", line 64, in generate_stub_for_c_module
File "mypy\stubgenc.py", line 315, in generate_c_type_stub
TypeError: unbound method type.mro() needs an argument

# Fixed code
def generate_c_type_stub(
    module: ModuleType,
    class_name: str,
    obj: type,
    output: List[str],
    imports: List[str],
    sigs: Optional[Dict[str, str]] = None,
    class_sigs: Optional[Dict[str, str]] = None,
) -> None:
    """Generate stub for a single class using runtime introspection.

    The result lines will be appended to 'output'. If necessary, any
    required names will be added to 'imports'.
    """
    # typeshed gives obj.__dict__ the not quite correct type Dict[str, Any]
    # (it could be a mappingproxy!), which makes mypyc mad, so obfuscate it.
    obj_dict = getattr(obj, "__dict__")  # type: Mapping[str, Any]  # noqa
    items = sorted(obj_dict.items(), key=lambda x: method_name_sort_key(x[0]))
    methods = []  # type: List[str]
    types = []  # type: List[str]
    static_properties = []  # type: List[str]
    rw_properties = []  # type: List[str]
    ro_properties = []  # type: List[str]
    done = set()  # type: Set[str]
    for attr, value in items:
        if is_c_method(value) or is_c_classmethod(value):
            done.add(attr)
            if not is_skipped_attribute(attr):
                if attr == "__new__":
                    # TODO: We should support __new__.
                    if "__init__" in obj_dict:
                        # Avoid duplicate functions if both are present.
                        # But is there any case where .__new__() has a
                        # better signature than __init__() ?
                        continue
                    attr = "__init__"
                if is_c_classmethod(value):
                    methods.append("@classmethod")
                    self_var = "cls"
                else:
                    self_var = "self"
                generate_c_function_stub(
                    module,
                    attr,
                    value,
                    methods,
                    imports=imports,
                    self_var=self_var,
                    sigs=sigs,
                    class_name=class_name,
                    class_sigs=class_sigs,
                )
        elif is_c_property(value):
            done.add(attr)
            generate_c_property_stub(
                attr,
                value,
                static_properties,
                rw_properties,
                ro_properties,
                is_c_property_readonly(value),
                module=module,
                imports=imports,
            )
        elif is_c_type(value):
            generate_c_type_stub(
                module,
                attr,
                value,
                types,
                imports=imports,
                sigs=sigs,
                class_sigs=class_sigs,
            )
            done.add(attr)

    for attr, value in items:
        if is_skipped_attribute(attr):
            continue
        if attr not in done:
            static_properties.append(
                "%s: ClassVar[%s] = ..."
                % (
                    attr,
                    strip_or_import(get_type_fullname(type(value)), module, imports),
                )
            )
    all_bases = type.mro(obj)
    if all_bases[-1] is object:
        # TODO: Is this always object?
        del all_bases[-1]
    # remove pybind11_object. All classes generated by pybind11 have pybind11_object in their MRO,
    # which only overrides a few functions in object type
    if all_bases and all_bases[-1].__name__ == "pybind11_object":
        del all_bases[-1]
    # remove the class itself
    all_bases = all_bases[1:]
    # Remove base classes of other bases as redundant.
    bases = []  # type: List[type]
    for base in all_bases:
        if not any(issubclass(b, base) for b in bases):
            bases.append(base)
    if bases:
        bases_str = "(%s)" % ", ".join(
            strip_or_import(get_type_fullname(base), module, imports) for base in bases
        )
    else:
        bases_str = ""
    if types or static_properties or rw_properties or methods or ro_properties:
        output.append("class %s%s:" % (class_name, bases_str))
        for line in types:
            if (
                output
                and output[-1]
                and not output[-1].startswith("class")
                and line.startswith("class")
            ):
                output.append("")
            output.append("    " + line)
        for line in static_properties:
            output.append("    %s" % line)
        for line in rw_properties:
            output.append("    %s" % line)
        for line in methods:
            output.append("    %s" % line)
        for line in ro_properties:
            output.append("    %s" % line)
    else:
        output.append("class %s%s: ..." % (class_name, bases_str))