Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The stub generator needs to emit aliases *after* the item they're aliasing. #900

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Version TBD (not yet released)
binding abstractions that "feel like" the built-in ones.
(PR `#884 <https://github.com/wjakob/nanobind/pull/884>`__)

- Taught the stub generator to emit aliases late, i.e. *after* the class
member they're supposed to alias. (PR `#900
<https://github.com/wjakob/nanobind/pull/900>`__)

Version 2.4.0 (Dec 6, 2024)
---------------------------

Expand Down
17 changes: 12 additions & 5 deletions src/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def put_nb_func(self, fn: NbFunction, name: Optional[str] = None) -> None:
self.write_ln(f"@{overload}")
self.put_nb_overload(fn, s, name)

def put_function(self, fn: Callable[..., Any], name: Optional[str] = None, parent: Optional[object] = None):
def put_function(self, fn: Callable[..., Any], name: Optional[str] = None, parent: Optional[object] = None, deferred: Optional[List[str]] = None):
"""Append a function of an arbitrary type to the stub"""
# Don't generate a constructor for nanobind classes that aren't constructible
if name == "__init__" and type(parent).__name__.startswith("nb_type"):
Expand All @@ -392,7 +392,11 @@ def put_function(self, fn: Callable[..., Any], name: Optional[str] = None, paren

# Check if this function is an alias from the *same* module
if name and fn_name and name != fn_name:
self.write_ln(f"{name} = {fn_name}\n")
line = f"{name} = {fn_name}\n"
if deferred is None:
self.write_ln(line)
else:
deferred.append(line)
return

if isinstance(fn, staticmethod):
Expand Down Expand Up @@ -519,8 +523,11 @@ def put_type(self, tp: NbType, name: Optional[str]):
self.put_docstr(docstr)
if len(tp_dict):
self.write("\n")
deferred = []
for k, v in tp_dict.items():
self.put(v, k, tp)
self.put(v, k, tp, deferred)
for line in deferred:
self.write_ln(line)
if output_len == len(self.output):
self.write_ln("pass\n")
self.depth -= 1
Expand Down Expand Up @@ -751,7 +758,7 @@ def apply_pattern(self, query: str, value: object) -> bool:
# Success, pattern was applied
return True

def put(self, value: object, name: Optional[str] = None, parent: Optional[object] = None) -> None:
def put(self, value: object, name: Optional[str] = None, parent: Optional[object] = None, deferred: Optional[List[str]] = None) -> None:
old_prefix = self.prefix

if value in self.stack:
Expand Down Expand Up @@ -841,7 +848,7 @@ def put(self, value: object, name: Optional[str] = None, parent: Optional[object
self.apply_pattern(self.prefix + ".__suffix__", None)
elif self.is_function(tp):
value = cast(NbFunction, value)
self.put_function(value, name, parent)
self.put_function(value, name, parent, deferred)
elif issubclass(tp, type):
value = cast(NbType, value)
self.put_type(value, name)
Expand Down