Skip to content

Commit

Permalink
✨ Accept nanobind module for stubgen
Browse files Browse the repository at this point in the history
  • Loading branch information
yosh-matsuda committed Oct 13, 2023
1 parent ac2d56f commit a41780b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
15 changes: 8 additions & 7 deletions mypy/stubdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ def add_token(self, token: tokenize.TokenInfo) -> None:
self.arg_type = self.accumulator
self.state.pop()
elif self.state[-1] == STATE_ARGUMENT_LIST:
self.arg_name = self.accumulator
if not (
token.string == ")" and self.accumulator.strip() == ""
) and not _ARG_NAME_RE.match(self.arg_name):
# Invalid argument name.
self.reset()
return
if not self.accumulator == "/":
self.arg_name = self.accumulator
if not (
token.string == ")" and self.accumulator.strip() == ""
) and not _ARG_NAME_RE.match(self.arg_name):
# Invalid argument name.
self.reset()
return

if token.string == ")":
self.state.pop()
Expand Down
11 changes: 10 additions & 1 deletion mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"Optional",
"Tuple",
"Union",
"Sequence",
)


Expand Down Expand Up @@ -194,7 +195,7 @@ def generate_stub_for_c_module(
done = set()
items = sorted(get_members(module), key=lambda x: x[0])
for name, obj in items:
if is_c_function(obj):
if is_c_function(obj) or is_nanobind_function(obj):
generate_c_function_stub(
module,
name,
Expand Down Expand Up @@ -307,6 +308,14 @@ def is_c_type(obj: object) -> bool:
return inspect.isclass(obj) or type(obj) is type(int)


def is_nanobind_function(obj: object) -> bool:
return (
hasattr(type(obj), "__module__")
and type(obj).__module__ == "nanobind"
and type(obj).__name__ == "nb_func"
)


def is_pybind11_overloaded_function_docstring(docstr: str, name: str) -> bool:
return docstr.startswith(f"{name}(*args, **kwargs)\n" + "Overloaded function.\n\n")

Expand Down

0 comments on commit a41780b

Please sign in to comment.