|
52 | 52 | nitpicky = True # ensures all :class:, :obj:, etc. links are valid |
53 | 53 | nitpick_ignore = [] |
54 | 54 |
|
55 | | -doctest_global_setup = "import pyarrow; import vortex" |
| 55 | +doctest_global_setup = "import pyarrow; import vortex; import vortex as vx" |
56 | 56 | doctest_default_flags = ( |
57 | 57 | doctest.ELLIPSIS | doctest.IGNORE_EXCEPTION_DETAIL | doctest.DONT_ACCEPT_TRUE_FOR_1 | doctest.NORMALIZE_WHITESPACE |
58 | 58 | ) |
@@ -196,6 +196,59 @@ def _post_process(app, builder): |
196 | 196 | os.environ["POLARS_TABLE_WIDTH"] = "80" |
197 | 197 |
|
198 | 198 |
|
| 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 | + |
199 | 251 | def setup(app): |
200 | 252 | app.connect("hawkmoth-process-docstring", _replace_rust_references) |
201 | 253 | 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