Skip to content

Consider adding doctest.skip_if decorator #117364

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

Open
sobolevn opened this issue Mar 29, 2024 · 4 comments
Open

Consider adding doctest.skip_if decorator #117364

sobolevn opened this issue Mar 29, 2024 · 4 comments
Assignees
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@sobolevn
Copy link
Member

sobolevn commented Mar 29, 2024

Feature or enhancement

Right now there's no way to skip the whole test / class doctest. You can only skip individual statements with # doctest: +SKIP

This causes problems in several use-cases:

  • You have a doctest that should be skipped based on the platform
  • We have doctests that should be skipped based on a resource availability
  • Libraries might want to skip some doctests based on the Python version / any other lib version

Right now we have this hack:

if int.__doc__: # simple check for --without-doc-strings, skip if lacking
def non_Python_modules(): r"""
Finding Doctests in Modules Not Written in Python

if not hasattr(sys, 'gettrace') or not sys.gettrace():
def test_pdb_set_trace():
"""Using pdb.set_trace from a doctest.
You can use pdb.set_trace from a doctest. To do so, you must

if supports_unicode:
def test_unicode(): """
Check doctest with a non-ascii filename:
>>> doc = '''
... >>> raise Exception('clé')

However, this is just test functions. For library functions it would be much harder to do.
You have to jiggle __doc__ attribute around.

Since #117297 we now have correct skipped count for doctest + unittest integration.

So, I propose adding this decorator to doctest.py

def skip_if(condition):
    def decorator(func):
        if condition and HAVE_DOCSTRINGS:
            func.__doc__ = ">>> pass  # doctest: +SKIP"
        return func
    return decorator

It will allow us skipping some tests conditionally with the proper reported results.

Refs #116758
CC @serhiy-storchaka and @furkanonder

Linked PRs

@sobolevn sobolevn added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Mar 29, 2024
@sobolevn sobolevn self-assigned this Mar 29, 2024
@serhiy-storchaka
Copy link
Member

There is a problem with applying this to non-test functions. We do not want to replace docstrings of library functions if doctests are skipped.

Also, what is the solution for conditional skipping of module-level doctests?

@serhiy-storchaka serhiy-storchaka added the 3.13 bugs and security fixes label Mar 29, 2024
@sobolevn
Copy link
Member Author

sobolevn commented Mar 29, 2024

There is a problem with applying this to non-test functions. We do not want to replace docstrings of library functions if doctests are skipped.

Hm, indeed. This might be a bit more complex: we can add # doctest: +SKIP to all >>> lines dynamically.

Does not seem too hard, but it will be harder for sure.

Also, what is the solution for conditional skipping of module-level doctests?

This can be adjusted to include doctest.skip_if(condition, module=True).

@sobolevn
Copy link
Member Author

Or instead of patching the string, we can go with .__doctest_skip__ attribute for a function.

@serhiy-storchaka
Copy link
Member

This was my idea too. I thought about making it more general and also setting other options for the whole test, but I don't see a use case for other options for now.

Alternative ideas:

  • Introduce a special exception doctest.SkipTest, so that raising it will skip all following examples.
  • Introduce a syntax for conditional skipping, e.g. +SKIP if varname. It is enough to support only simple variable names, not complex expressions. It would be nice to also add syntax to apply options to all subsequent examples instead of just one example.

They are more complex changes of doctest machinery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants