python / python/cpython

Consider adding `doctest.skip_if` decorator

Đang mở
#117,364 4 bình luận 1 reaction 1 người được giao Xem trên GitHub

@sobolevn đang làm issue này rồi.

Từ ngày 29/3/2024.

3.13 stdlib type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.