Skip to content

Commit 56cb90a

Browse files
authored
fix: 🐛 do not remove self parameter annotation when types do not match (#195)
1 parent 02f6c8f commit 56cb90a

File tree

20 files changed

+101
-13
lines changed

20 files changed

+101
-13
lines changed

pybind11_stubgen/parser/mixins/fix.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,46 @@
4040

4141

4242
class RemoveSelfAnnotation(IParser):
43+
44+
__any_t_name = QualifiedName.from_str("Any")
45+
__typing_any_t_name = QualifiedName.from_str("typing.Any")
46+
4347
def handle_method(self, path: QualifiedName, method: Any) -> list[Method]:
4448
methods = super().handle_method(path, method)
4549
for method in methods:
46-
self._remove_self_arg_annotation(method.function)
50+
self._remove_self_arg_annotation(path, method.function)
4751
return methods
4852

4953
def handle_property(self, path: QualifiedName, prop: Any) -> Property | None:
5054
prop = super().handle_property(path, prop)
5155
if prop is not None:
5256
if prop.getter is not None:
53-
self._remove_self_arg_annotation(prop.getter)
57+
self._remove_self_arg_annotation(path, prop.getter)
5458
if prop.setter is not None:
55-
self._remove_self_arg_annotation(prop.setter)
59+
self._remove_self_arg_annotation(path, prop.setter)
5660

5761
return prop
5862

59-
def _remove_self_arg_annotation(self, func: Function):
60-
if len(func.args) > 0 and func.args[0].name == "self":
61-
func.args[0].annotation = None
63+
def _remove_self_arg_annotation(self, path: QualifiedName, func: Function) -> None:
64+
if len(func.args) == 0:
65+
return
66+
fully_qualified_class_name = QualifiedName(path[:-1]) # remove the method name
67+
first_arg = func.args[0]
68+
if (
69+
first_arg.name == "self"
70+
and isinstance(first_arg.annotation, ResolvedType)
71+
and not first_arg.annotation.parameters
72+
and (
73+
first_arg.annotation.name
74+
in {
75+
self.__any_t_name,
76+
self.__typing_any_t_name,
77+
fully_qualified_class_name,
78+
fully_qualified_class_name[-len(first_arg.annotation.name) :],
79+
}
80+
)
81+
):
82+
first_arg.annotation = None
6283

6384

6485
class FixMissingImports(IParser):

tests/py-demo/bindings/src/modules/aliases.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace {
1616
};
1717
struct Bar3 {
1818
};
19+
struct Bar4 {
20+
};
1921
} // namespace
2022

2123
void bind_aliases_module(py::module &&m) {
@@ -57,13 +59,18 @@ void bind_aliases_module(py::module &&m) {
5759
{
5860
auto &&sub = m.def_submodule("foreign_method_arg");
5961
auto &&pyBar = py::class_<Bar2>(sub, "Bar2");
60-
pyBar.def("set_foo", [](const demo::Foo &) { return 13; });
62+
pyBar.def("set_foo", [](const Bar2 &, const demo::Foo &) { return 13; });
6163
}
6264
{
6365
auto &&sub = m.def_submodule("foreign_method_return");
6466
auto &&pyBar = py::class_<Bar3>(sub, "Bar3");
6567
pyBar.def("get_foo", []() { return demo::Foo(); });
6668
}
69+
{
70+
auto &&sub = m.def_submodule("missing_self_arg");
71+
auto &&pyBar = py::class_<Bar4>(sub, "Bar4");
72+
pyBar.def("set_foo", [](const demo::Foo &) { return 13; });
73+
}
6774

6875
{
6976
m.attr("foreign_type_alias") = m.attr("foreign_method_arg").attr("Bar2");

tests/stubs/python-3.12/pybind11-master/numpy-array-use-type-var/demo/_bindings/aliases/__init__.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ from . import (
1616
foreign_method_arg,
1717
foreign_method_return,
1818
foreign_return,
19+
missing_self_arg,
1920
)
2021

2122
__all__ = [
@@ -33,6 +34,7 @@ __all__ = [
3334
"func",
3435
"local_func_alias",
3536
"local_type_alias",
37+
"missing_self_arg",
3638
"random",
3739
]
3840

tests/stubs/python-3.12/pybind11-master/numpy-array-use-type-var/demo/_bindings/aliases/foreign_method_arg.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import demo._bindings.classes
55
__all__ = ["Bar2"]
66

77
class Bar2:
8-
def set_foo(self) -> int: ...
8+
def set_foo(self, arg0: demo._bindings.classes.Foo) -> int: ...
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import annotations
2+
3+
import demo._bindings.classes
4+
5+
__all__ = ["Bar4"]
6+
7+
class Bar4:
8+
def set_foo(self: demo._bindings.classes.Foo) -> int: ...

tests/stubs/python-3.12/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/aliases/__init__.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ from . import (
1616
foreign_method_arg,
1717
foreign_method_return,
1818
foreign_return,
19+
missing_self_arg,
1920
)
2021

2122
__all__ = [
@@ -33,6 +34,7 @@ __all__ = [
3334
"func",
3435
"local_func_alias",
3536
"local_type_alias",
37+
"missing_self_arg",
3638
"random",
3739
]
3840

tests/stubs/python-3.12/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/aliases/foreign_method_arg.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import demo._bindings.classes
55
__all__ = ["Bar2"]
66

77
class Bar2:
8-
def set_foo(self) -> int: ...
8+
def set_foo(self, arg0: demo._bindings.classes.Foo) -> int: ...
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import annotations
2+
3+
import demo._bindings.classes
4+
5+
__all__ = ["Bar4"]
6+
7+
class Bar4:
8+
def set_foo(self: demo._bindings.classes.Foo) -> int: ...

tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/aliases/__init__.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ from . import (
1616
foreign_method_arg,
1717
foreign_method_return,
1818
foreign_return,
19+
missing_self_arg,
1920
)
2021

2122
__all__ = [
@@ -33,6 +34,7 @@ __all__ = [
3334
"func",
3435
"local_func_alias",
3536
"local_type_alias",
37+
"missing_self_arg",
3638
"random",
3739
]
3840

tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/aliases/foreign_method_arg.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import demo._bindings.classes
55
__all__ = ["Bar2"]
66

77
class Bar2:
8-
def set_foo(self) -> int: ...
8+
def set_foo(self, arg0: demo._bindings.classes.Foo) -> int: ...

0 commit comments

Comments
 (0)