Skip to content

Commit 9063be4

Browse files
wojdyrwjakob
authored andcommitted
stubgen.py: handle method aliases similarly to function aliases (#735)
1 parent 06f4bf5 commit 9063be4

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Version TBD (unreleased)
3434
<https://github.com/wjakob/nanobind/pull/939>`__, `#940
3535
<https://github.com/wjakob/nanobind/pull/940>`__).
3636

37+
- The stub generator now detects method aliases and preserves this information
38+
instead of duplicating the definition. (PR `#735
39+
<https://github.com/wjakob/nanobind/pull/735>`__).
40+
3741
- Added support for binding functions that accept a ``std::variant<...>`` that
3842
is not default-constructible (because its first alternative isn't). (PR `#987
3943
<https://github.com/wjakob/nanobind/pull/987>`__).

src/stubgen.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,19 @@ def put_function(self, fn: Callable[..., Any], name: Optional[str] = None, paren
395395
self.write_ln(f"{name} = {fn_name}\n")
396396
return
397397

398+
# Special handling for nanobind functions with overloads
399+
if type(fn).__module__ == "nanobind":
400+
fn = cast(NbFunction, fn)
401+
self.put_nb_func(fn, name)
402+
return
403+
398404
if isinstance(fn, staticmethod):
399405
self.write_ln("@staticmethod")
400406
fn = fn.__func__
401407
elif isinstance(fn, classmethod):
402408
self.write_ln("@classmethod")
403409
fn = fn.__func__
404410

405-
# Special handling for nanobind functions with overloads
406-
if type(fn).__module__ == "nanobind":
407-
fn = cast(NbFunction, fn)
408-
self.put_nb_func(fn, name)
409-
return
410-
411411
if name is None:
412412
name = fn.__name__
413413
assert name
@@ -859,7 +859,7 @@ def put(self, value: object, name: Optional[str] = None, parent: Optional[object
859859
elif tp_mod == "nanobind":
860860
if tp_name == "nb_method":
861861
value = cast(NbFunction, value)
862-
self.put_nb_func(value, name)
862+
self.put_function(value, name)
863863
elif tp_name == "nb_static_property":
864864
value = cast(NbStaticProperty, value)
865865
self.put_nb_static_property(name, value)

tests/test_typing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ NB_MODULE(test_typing_ext, m) {
6060
// Aliases to local functoins and types
6161
m.attr("FooAlias") = m.attr("Foo");
6262
m.attr("f_alias") = m.attr("f");
63+
nb::type<Foo>().attr("lt_alias") = nb::type<Foo>().attr("__lt__");
6364

6465
// Custom signature generation for classes and methods
6566
struct CustomSignature { int value; };

tests/test_typing_ext.pyi.ref

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Foo:
1717

1818
def __ge__(self, arg: Foo, /) -> bool: ...
1919

20+
lt_alias = __lt__
21+
2022
def f() -> None: ...
2123

2224
def makeNestedClass() -> py_stub_test.AClass.NestedClass: ...

0 commit comments

Comments
 (0)