Skip to content

Commit

Permalink
Changed namespace fix to use Sphinx autodoc-process-* events (#7103)
Browse files Browse the repository at this point in the history
* Moved namespace fix to autodoc event callbacks to fix imports in stubs.

* Fixed typo for cuda namespace fix
  • Loading branch information
timohl authored Dec 19, 2024
1 parent 2a67419 commit 8dbccb6
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.", "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 8dbccb6

Please sign in to comment.