sizmailov / pybind11-stubgen

Generate stubs for python modules
Other
218 stars 44 forks source link

fix: 🐛 do not remove self parameter annotation when types do not match #195

Closed ringohoffman closed 7 months ago

ringohoffman commented 7 months ago

Always removing the first parameter's type annotation when its name is "self" can mask underlying issues in the C++ binding, which is more obviously revealed when you only remove the parameter's annotation when it matches the class name it is bound to or it is typing.Any.

I noticed this bug since there was an unused import in demo/_bindings/aliases/foreign_method_arg.pyi, which seemed strange until I noticed that the sole parameter was actually in fact typed as a Foo in the C++ code (the C++ method implementation is missing a self parameter). I think making this change will help to make these bugs in the underlying C++ code more obvious:

Screenshot 2023-11-23 at 5 55 31 PM
sizmailov commented 7 months ago

I thought that it would be nice to produce a warning when we find a suspicious self annotation. If the problem is reliably detectable by static analysis checkers, we don't need to.