python / python/cpython

Consider adding `doctest.skip_if` decorator

未关闭
#117,364 4 条评论 1 个 reaction 已指派 1 人 在 GitHub 查看

@sobolevn 已经在做这个了。

开始于 2024年3月29日。

3.13 stdlib type-feature
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

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:

https://github.com/python/cpython/blob/2e9be80c99f635c2f7761e8356b0260922d6e7a6/Lib/test/test_doctest/test_doctest.py#L727-L730

https://github.com/python/cpython/blob/2e9be80c99f635c2f7761e8356b0260922d6e7a6/Lib/test/test_doctest/test_doctest.py#L1999-L2003

https://github.com/python/cpython/blob/2e9be80c99f635c2f7761e8356b0260922d6e7a6/Lib/test/test_doctest/test_doctest.py#L2938-L2943

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

Since https://github.com/python/cpython/pull/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 https://github.com/python/cpython/pull/116758
CC @serhiy-storchaka and @furkanonder

Linked PRs
  • gh-122935

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。