Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Matlab style doctests #94

Closed
skycaptain opened this issue Oct 10, 2019 · 10 comments
Closed

Add support for Matlab style doctests #94

skycaptain opened this issue Oct 10, 2019 · 10 comments

Comments

@skycaptain
Copy link

We're using Sphinx for generating HTML docs out of help texts. We would like to use catch22/octave-doctest for running tests on the examples in docstrings. Sphinx recognizes doctests starting with >>> and formats them as accordingly. octave-doctest however requires doctests starting with >> . See discussion here gnu-octave/octave-doctest#236.

This would allow us to use:

function number = getMyNumber()
% Returns the number
%
%   Returns:
%       number (numeric): The number
%
%   Example:
%       >> getMyNumber()
%       5
%
    number = 5;
end

Instead of:

function number = getMyNumber()
% Returns the number
%
%   Returns:
%       number (numeric): The number
%
%   Example:
%       ::
%
%           >> getMyNumber()
%           5
%
    number = 5;
end

Not an elegant solution, but one could Monkey-patch this line.

@joeced
Copy link
Collaborator

joeced commented Oct 10, 2019

Thanks for the suggestion. I think it's a nice idea and I tried out your monkey-patching idea, which works as expected (and is easy to implement).

I think I will add a new option, matlab_enable_doctest, which will do the monkey-patching. Does that sound OK?

Edit:

I implemented the feature (and it works), but I have to find a way to test it reliably.

@cbm755
Copy link

cbm755 commented Oct 10, 2019

Julia uses a prompt julia>: how do they deal with this in their doctests?

Should we try to get it customizable in Sphinx itself?

@joeced
Copy link
Collaborator

joeced commented Oct 10, 2019

@cbm755 That might difficult. The problem is that the solution is to monkey-patch docutils, which is not part of Sphinx directly.

@cbm755
Copy link

cbm755 commented Oct 10, 2019

Thanks, I see.

This is mentioned under http://docutils.sourceforge.net/docs/dev/todo.html "Generalize the doctest block construct (which is overly Python-centric) to other...". But at least one person "spoke out against this idea".

So I guess a local fix here is certainly quicker.

@skycaptain
Copy link
Author

Nice one! Just one more thing, that came to my mind: How does this affect mixed environments, where we have Python and Matlab code? The patch is global I guess and not per-file?

@skycaptain
Copy link
Author

Another thing that came to my mind: Does Sphinx break the highlighting after one empty line. See the discussion we had over at gnu-octave/octave-doctest#239

@cbm755
Copy link

cbm755 commented Oct 11, 2019

The to-be-written manual for octave-pythonic might want to mix Python and Octave doctests in the same document. CC @mtmiller.

I'm not a Sphinx/Docutils user yet so maybe this is not useful... Can we write some kind of thing like https://www.sympy.org/sphinx-math-dollar/ but that munges >>-style doctests into whatever format Docutils wants? (i.e., with <BLANKLINE> and whatever conventions).

I'd be happy to collaborate on the parsing of octave-doctest Doctests if that were helpful---i.e., we could factor that out a bit.

joeced added a commit that referenced this issue Oct 12, 2019
First solution is to monkey-patch docutils directly, which will make
doctest accept >> instead of >>>.

The solution interferes with Python doctest rendering, but matlabdomain
and python autodoc does not play well anyway.
@joeced
Copy link
Collaborator

joeced commented Oct 12, 2019

I'll try to answer the comments.

I pushed the first hacked version to https://github.com/sphinx-contrib/matlabdomain/tree/doctests so you can try it out.

You can install it locally (preferably in a virtual environment) with

pip install git+https://github.com/sphinx-contrib/matlabdomain.git/@doctests

@joeced
Copy link
Collaborator

joeced commented Oct 23, 2019

I just released a new version, 0.10.0, where Python and MATLAB code can be documented together (closed #63), which probably makes it a lot more difficult to use the proposed hack.

Another solution could be to connect to the autodoc-process-docstring as Napoleon extension does and scan for >> or whatever doctests symbol should be used.

@joeced
Copy link
Collaborator

joeced commented Mar 14, 2023

I've decided not to support this feature.

@joeced joeced closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants