Skip to content

Commit

Permalink
Moved namespace fix to autodoc event callbacks to fix imports in stubs.
Browse files Browse the repository at this point in the history
  • Loading branch information
timohl committed Dec 18, 2024
1 parent 0ae7b1a commit 22f89e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
12 changes: 2 additions & 10 deletions cpp/pybind/docstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,9 @@ std::string FunctionDoc::ToGoogleDocString() const {
return rc.str();
}

std::string FunctionDoc::NamespaceFix(const std::string& s) {
std::string rc = std::regex_replace(s, std::regex("::(\\S)"), ".$1");
rc = std::regex_replace(rc, std::regex("open3d\\.(cpu|cuda)\\.pybind\\."),
"open3d.");
return rc;
}

std::string FunctionDoc::StringCleanAll(std::string& s,
const std::string& white_space) {
std::string rc = utility::StripString(s, white_space);
rc = NamespaceFix(rc);
return rc;
}

Expand All @@ -313,7 +305,7 @@ ArgumentDoc FunctionDoc::ParseArgumentToken(const std::string& argument_token) {
std::smatch matches;
if (std::regex_search(argument_token, matches, rgx_with_default)) {
argument_doc.name_ = matches[1].str();
argument_doc.type_ = NamespaceFix(matches[2].str());
argument_doc.type_ = matches[2].str();
argument_doc.default_ = matches[3].str();

// Handle long default value. Long default has multiple lines and thus
Expand All @@ -335,7 +327,7 @@ ArgumentDoc FunctionDoc::ParseArgumentToken(const std::string& argument_token) {
"([A-Za-z_][A-Za-z\\d_:\\.\\[\\]\\(\\) ,]*)");
if (std::regex_search(argument_token, matches, rgx_without_default)) {
argument_doc.name_ = matches[1].str();
argument_doc.type_ = NamespaceFix(matches[2].str());
argument_doc.type_ = matches[2].str();
}
}

Expand Down
23 changes: 23 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,31 @@ def skip(app, what, name, obj, would_skip, options):
return would_skip


def fix_namespace(text):
"""Fixes namespace in a string by removing .[cpu|cuda].pybind."""
return (text.replace("open3d.cpu.pybind.", "open3d.").replace(
"open3d.cuda.pybind.cuda.", "open3d.") if text is not None else None)


def process_signature(app, what, name, obj, options, signature,
return_annotation):
"""Fixes namespace in signature by removing .[cpu|cuda].pybind."""
return (
fix_namespace(signature),
fix_namespace(return_annotation),
)


def process_docstring(app, what, name, obj, options, lines):
"""Fixes namespace in docstring by removing .[cpu|cuda].pybind."""
for i, line in enumerate(lines):
lines[i] = fix_namespace(line)


def setup(app):
app.connect("autodoc-skip-member", skip)
app.connect("autodoc-process-signature", process_signature)
app.connect("autodoc-process-docstring", process_docstring)
# Add Google analytics
app.add_js_file("https://www.googletagmanager.com/gtag/js?id=G-3TQPKGV6Z3",
**{'async': 'async'})
Expand Down

0 comments on commit 22f89e0

Please sign in to comment.