Skip to content

stubgenc.py does not generate correct lines for staticmethods #13574

Closed
@EzioHelios

Description

@EzioHelios

Bug Report

When I use stubgen to generate stub file for a pybind11 module

klass
.def_static("some_static_method", &A::some_static_method,
    R"#(None)#", py::arg("a"), py::arg("b")
    );

where pybind11::class_ klass is bound to C++ class A

the result line is like:

class A:
    def some_static_method(self, *args, **kwargs): ...

which should be like:

class A:
    @static_method
    def some_static_method(a: T, b: T): ...

The issue here is related to a pybind11 discussion: [https://github.com/pybind/pybind11/issues/2403].
which says "a static method of class Foo, Foo.dict['f_static'] points to the static method decorator around the function object instead, not the function object itself"

So, I temporarily fixed this issue for my own project by appending a staticmethod checker right after the is_c_classmethod in file stubgenc.py:

def is_c_staticmethod(obj: object) -> bool:
    return type(obj).__name__ in ('staticmethod')
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:
                ...
                if is_c_classmethod(value):
                    methods.append('@classmethod')
                    self_var = 'cls'
                elif is_c_staticmethod(value):
                    methods.append('@staticmethod')
                    value = value.__func__
                    self_var = None
                else:
                    self_var = 'self'
                ...

The result turns out correct with my pybind11 code. But I guess it may not work for other conditions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions