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

support using a variable (or expression) for conditional skipping, not just a dependency #193

Open
jab opened this issue Jan 7, 2023 · 7 comments

Comments

@jab
Copy link

jab commented Jan 7, 2023

I went through the issue tracker and I think this exact issue has not been discussed yet:

It looks like the current support for conditional skipping is limited to checking for whether a dependency is available.[1]

But some blocks need to be skipped in other situations, e.g. when running with a too-old Python version, or under an alternative Python implementation such as PyPy.

As prior art, sphinx.ext.doctest has a :skipif: option that allows conditionally skipping doctest blocks using an arbitrary Python expression (e.g. :skipif: sys.version_info < (3,). Something like that would be perfect here.

Or if it's easier, perhaps the existing doctest-requires directive could be expanded to accept a variable rather than a dependency name, e.g.

# docs/foo.rst

.. doctest-requires:: cpython

    >>> float('nan') is float('nan')  # True for PyPy
    False

and a conftest.py could define the variable in the doctest_namespace:

# docs/conftest.py

@pytest.fixture(autouse=True)
def add_doctest_globals(doctest_namespace):
    doctest_namespace['cpython'] = sys.implementation.name == 'cpython'

Is there any interest in considering this?

Thanks!

[1] Both based on my testing as well as the README:

Similarly, in Sphinx .rst documentation, whole code example blocks can be conditionally skipped if a dependency is not available.

.. doctest-requires:: asdf

   >>> import asdf
   >>> asdf.open('file.asdf')
@bsipocz
Copy link
Member

bsipocz commented Jan 10, 2023

Having a conditional feature would be nice, I suppose we just need to converge on whether to have it as an option for the currently existing doctest-requires or add a new skip-if (or preferably calling it doctest-skip?).

Do you have any time to work on a PR for this @jab?

@pllim
Copy link
Contributor

pllim commented Jan 17, 2023

Why is doctestplus not using the built-in skipif from doctest? 🤔

@bsipocz
Copy link
Member

bsipocz commented Jan 18, 2023

I don't have access to code atm, but is the sphinx doctest extension the same as the built-in doctest? I would have guessed no.

OTOH, we certainly support a few things from it already so it's worth comparing whether e.g. testsetup works automatically or not.

@jab
Copy link
Author

jab commented Jan 18, 2023

Right, sphinx.ext.doctest is not the same as the standard library's doctest module. It's the former that provides skip-if, not the latter.

Wonderful to hear there's interest in adding support for skip-if here. I believe that would make pytest-doctestplus the only pytest plugin to offer support for this, which would make it the only solution for projects like mine that need this (example). (pytest-sphinx started adding support for this years ago, but the implementation has not been completed yet. See also sphinx-doc/sphinx#1487.)

I took a quick look at https://github.com/astropy/pytest-doctestplus/blob/main/pytest_doctestplus/sphinx/doctestplus.py with an eye toward contributing this, but couldn't immediately see how to do it, and am not sure when I could find more time for it (recently started a new job, and have a 6-month-old at home:). Would this be pretty straightforward to add though for a core maintainer or a more experienced outside contributor to this project?

Thanks for your consideration!

@bsipocz
Copy link
Member

bsipocz commented Jan 18, 2023

I have some offline flight hours later this week to fill with a suitably standalone project, will try to have a look at this then.

@pllim
Copy link
Contributor

pllim commented Jan 18, 2023

Would be nice if we can take advantage of sphinx.ext.doctest somehow without reinventing the wheel but I am also not familiar with the code here. Hopefully @bsipocz can come up with something. Thanks! 🤞 🙇‍♀️

@mhvk
Copy link
Contributor

mhvk commented Mar 13, 2023

General 👍 to this idea -- will be helpful for people needing to deal with the changes in how units are represented (thanks @pllim for pointing to this!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants