13445.bugfix.rst
Overview
This file documents a specific bugfix related to the `pytest.skip` function and related skipping utilities in the `pytest` testing framework. The primary purpose of this fix is to improve the **type annotations** of `pytest.skip` and its related skip functions so they are **more specification-compliant**. This enhancement aims to ensure compatibility and correctness across a wider range of type checkers, improving developer experience and code reliability when using these functions in typed Python codebases.
Type annotations are metadata that describe the expected types of function parameters and return values. Making them spec-compliant means adhering strictly to Python typing standards and conventions, which helps static analysis tools (like `mypy`, `pyright`, etc.) verify code correctness more effectively.
Detailed Explanation
Context
Function(s) affected:
pytest.skipand related skip utilities.Nature of fix: Adjusted or corrected type annotations.
Goal: Improve compatibility with type checking tools and ensure that usage of these functions aligns with typing specifications.
pytest.skip Function
`pytest.skip` is used to programmatically skip a test during execution, typically when certain conditions are met (e.g., missing dependencies, unsupported platforms).
**Before this fix:**
The type annotations for
pytest.skipmight have been incomplete, overly generic, or not fully compliant with typing standards.This could cause warnings or errors in some type checkers or hamper their ability to understand the function’s interface.
**After this fix:**
The type annotations have been refined to reflect the exact parameter types and return types.
This includes properly typing the function's parameters (such as messages or reasons for skipping) and its return behavior (generally no return, or
NoReturnto indicate it exits the test).The fix extends to other related skip functions or methods (
pytest.skipif,pytest.mark.skip, etc.) if applicable.
Parameters and Return Values
Parameters:
Usually a single parameter: a string message or reason for skipping.
May also accept optional parameters depending on the specific skip function variant.
Return Value:
Typically does not return (raises a special internal exception to skip tests).
Proper typing uses
NoReturnfromtypingmodule to indicate this behavior.
Usage Example
import pytest
def test_feature():
if not feature_is_available():
pytest.skip("Feature not available on this platform")
# Test code continues only if not skipped
assert do_something() == expected_result
This fix ensures that when running static type checks on code like above, the type checker correctly understands the behavior of `pytest.skip` and does not raise spurious errors.
Implementation Details
The fix involved modifying type annotations within the source code of
pytest.skipand related functions.Likely utilized typing constructs such as
NoReturnto indicate non-returning functions.May have used
Callable[..., NoReturn]or similar patterns to describe the callable nature and behavior.Ensured that annotations are compatible with various type checkers by adhering to Python's PEP 484 standards and PEP 561 for distributing type information.
Interaction with Other System Components
User Tests: This file affects user test code that calls
pytest.skipor related skip mechanisms.Type Checking Tools: Enhances static analysis when these tools inspect test codebases using
pytest.pytestCore: Integrates with the corepytestsystem where skipping logic is implemented.Test Reporting: While the fix is about type annotations, it indirectly supports accurate test reporting by ensuring skip-related calls are correctly understood and handled.
Visual Diagram: Function and Type Annotation Relationship
flowchart TD
A[pytest.skip(message: str) : NoReturn]
B[pytest.skipif(condition: bool, reason: str) : Callable]
C[pytest.mark.skip(reason: str) : Marker]
A -->|raises skip exception| D[Control flow interruption]
B -->|returns decorator| E[Test function]
C -->|marks test| E
style A fill:#f9f,stroke:#333,stroke-width:2px
style B fill:#bbf,stroke:#333,stroke-width:2px
style C fill:#bfb,stroke:#333,stroke-width:2px
This diagram illustrates three main skip-related functions/markers impacted by the type annotation fix:
pytest.skip: Directly skips a test by raising an exception (NoReturn).pytest.skipif: Returns a decorator that conditionally skips tests.pytest.mark.skip: Marks a test to be skipped unconditionally.
All play crucial roles in controlling test execution flow, and their type annotations ensure clarity and correctness in their usage.
Summary
This file represents a bugfix improving type annotations for
pytest.skipand related skip constructs.The fix enhances compatibility and precision across various type checkers.
It clarifies function signatures, especially for non-returning behavior (
NoReturn).Though the fix is internal and annotation-focused, it has positive effects on test code quality and static analysis.
The fix aligns with best practices in Python typing and contributes to the robustness of the
pytestframework for typed Python projects.
**End of Documentation**