Skip to content

Commit eaac6c5

Browse files
authored
fix: make cargo test work by hiding Python code from Rust (#4431)
See the comment in docs/conf.py Signed-off-by: Daniel King <dan@spiraldb.com>
1 parent 5a062ff commit eaac6c5

6 files changed

Lines changed: 355 additions & 217 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ jobs:
133133
run: |
134134
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
135135
# nextest doesn't support doc tests, so we run it here
136-
cargo test --doc --workspace --all-features --exclude vortex-cxx --exclude vortex-python --exclude vortex-jni --exclude vortex-ffi --exclude xtask --no-fail-fast
136+
cargo test --doc --workspace --all-features --exclude vortex-cxx --exclude vortex-jni --exclude vortex-ffi --exclude xtask --no-fail-fast
137137
138138
build-rust:
139139
name: "Rust build (${{matrix.config.name}})"

docs/conf.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
nitpicky = True # ensures all :class:, :obj:, etc. links are valid
5353
nitpick_ignore = []
5454

55-
doctest_global_setup = "import pyarrow; import vortex"
55+
doctest_global_setup = "import pyarrow; import vortex; import vortex as vx"
5656
doctest_default_flags = (
5757
doctest.ELLIPSIS | doctest.IGNORE_EXCEPTION_DETAIL | doctest.DONT_ACCEPT_TRUE_FOR_1 | doctest.NORMALIZE_WHITESPACE
5858
)
@@ -196,6 +196,59 @@ def _post_process(app, builder):
196196
os.environ["POLARS_TABLE_WIDTH"] = "80"
197197

198198

199+
def _convert_python_fenced_blocks_from_rust_to_valid_reST_blocks(app, what, name, obj, options, lines):
200+
"""Remove Markdown-style code fences from Python docs written in Rust.
201+
202+
We would like `cargo test` to Just Work (TM). Unfortunately, by default, it executes any
203+
code-block in any docstring even though we intend those docs to be *Python* doc tests.
204+
205+
For example, the following is interpreted by Rust as Rust code (which it will try to doctest):
206+
207+
/// >>> 1 + 1
208+
/// 3
209+
fn foo() {
210+
}
211+
212+
What syntax can we use to communicate to Rust "This is not Rust code" but communicate to Python
213+
"This is Python code"? The following appears as executable code to both, so it does not work:
214+
215+
/// .. code-block:: python
216+
///
217+
/// >>> 1 + 1
218+
/// 3
219+
fn foo() {
220+
}
221+
222+
This does not appear to work unless we wrap all the code in braces or a function, which makes it
223+
not valid Python:
224+
225+
/// #[no_run]
226+
/// >>> 1 + 1
227+
/// 3
228+
229+
The following is executed by neither language and does not render properly (because it is not
230+
valid reStructured Text):
231+
232+
/// ```python
233+
/// >>> 1 + 1
234+
/// 3
235+
/// ```
236+
237+
Okay, so, our solution is to just adopt the last option and explicitly remove the code fences
238+
when we parse docstrings in Sphinx.
239+
240+
"""
241+
in_block = False
242+
for i, line in enumerate(lines):
243+
if line == "```python":
244+
lines[i] = ""
245+
in_block = True
246+
elif in_block and line == "```":
247+
lines[i] = ""
248+
in_block = False
249+
250+
199251
def setup(app):
200252
app.connect("hawkmoth-process-docstring", _replace_rust_references)
201253
app.connect("write-started", _post_process)
254+
app.connect("autodoc-process-docstring", _convert_python_fenced_blocks_from_rust_to_valid_reST_blocks)

0 commit comments

Comments
 (0)