typing_raises_group.py


Overview

This file provides extensive type checking and validation utilities focused on the `pytest` framework's exception-raising context managers, specifically `RaisesExc` and `RaisesGroup`. It contains a series of test-like functions that verify the correct typing behavior, constructor usage, type narrowing, and matching logic of these classes. These checks ensure type safety, proper covariance and contravariance in generics, and correct behavior when dealing with Python 3.11's `ExceptionGroup` and its backport `exceptiongroup`.

Key points:

This file is primarily useful for maintainers of `pytest` or typing-related tooling to ensure that the exception-raising utilities behave correctly with static type checkers.


Classes, Functions, and Methods

Imported Entities


Functions

All functions are scoped tests or usage examples demonstrating typing and behavior of `RaisesExc` and `RaisesGroup`.


check_raisesexc_typevar_default(e: RaisesExc) -> None


check_basic_contextmanager() -> None


check_basic_matches() -> None


check_matches_with_different_exception_type() -> None


check_raisesexc_init() -> None


raisesgroup_check_type_narrowing() -> None


raisesgroup_narrow_baseexceptiongroup() -> None


check_raisesexc_transparent() -> None


check_nested_raisesgroups_contextmanager() -> None


check_nested_raisesgroups_matches() -> None


check_multiple_exceptions_1() -> None


check_multiple_exceptions_2() -> None


check_raisesgroup_overloads() -> None


check_triple_nested_raisesgroup() -> None


check_check_typing() -> None


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class RaisesExc {
        +__init__(expected_exception: BaseException | None = ..., match: str | None = ..., check: Callable | None = ...)
        +expected_exceptions: type[BaseException] | tuple[type[BaseException], ...] | None
        +exception_type() -> type[BaseException]
    }

    class RaisesGroup {
        +__init__(*exceptions: RaisesExc | type[BaseException], match: str | None = ..., check: Callable | None = ..., allow_unwrapped: bool = False, flatten_subgroups: bool = False)
        +matches(exc: BaseException | BaseExceptionGroup) -> bool
        +check: Callable[[BaseExceptionGroup], bool] | None
        +value: BaseExceptionGroup
    }

    class ExceptionGroup {
        +exceptions: tuple[BaseException, ...]
        +message: str
    }

    class BaseExceptionGroup {
        +exceptions: tuple[BaseException, ...]
        +message: str
    }

    RaisesExc --> BaseException
    RaisesGroup --> RaisesExc
    RaisesGroup --> BaseExceptionGroup
    RaisesGroup --> BaseException
    RaisesGroup --> ExceptionGroup

Summary

This file serves as a comprehensive set of static typing and usage tests for `pytest`'s advanced exception handling utilities, focusing on the interplay with Python’s `ExceptionGroup` mechanism. It is an essential aid in ensuring that exception-raising assertions in tests are both type-safe and semantically correct, particularly as Python’s exception grouping features evolve.


**End of documentation for `typing_raises_group.py`**