raises_group.py

Overview

The [raises_group.py](/projects/286/67439) file is a comprehensive test suite and utility module designed to extend and enhance the capabilities of Pytest’s exception assertion mechanisms, specifically for handling **exception groups** introduced in Python 3.11 (`ExceptionGroup` and `BaseExceptionGroup`). It builds upon Pytest's existing `RaisesExc` and `RaisesGroup` classes to support more complex scenarios involving nested exception groups, multiple expected exceptions, and flexible matching criteria.

This file primarily contains:

The file serves both as documentation through tests and as a validation tool for the correctness and robustness of exception group handling in Pytest.


Key Classes and Functions

Imported Classes and Functions (from other modules)

These are imported and tested extensively in this file but are implemented elsewhere (likely in `pytest.raises` module).


Function: wrap_escape(s: str) -> str

**Purpose:** Wraps a string with `^` and `$` anchors and escapes all regex meta-characters in the string to create a regex that matches the exact input string.

**Parameters:**

**Returns:**

**Usage Example:**

pattern = wrap_escape("foo.bar")
# pattern == "^foo\\.bar$"

Function: fails_raises_group(msg: str, add_prefix: bool = True) -> RaisesExc[Failed]

**Purpose:** Helper function to create a Pytest raises context expecting a failure (`Failed` exception) with a specific error message matching `msg`. It is used to test negative scenarios where `RaisesGroup` is expected to fail.

**Parameters:**

**Returns:**

**Usage Example:**

with fails_raises_group("some error message"):
    # code that triggers a failure with 'some error message'

Testing RaisesGroup and Related Features

The majority of this file consists of **test functions** prefixed by `test_`, which validate the behavior and robustness of `RaisesGroup` and `RaisesExc` under various scenarios.

General Test Categories

Example: test_raises_group

This test validates:


Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class RaisesGroup {
        +__init__(*expected_exceptions, allow_unwrapped=False, flatten_subgroups=False, match=None, check=None)
        +__enter__()
        +__exit__(exc_type, exc_value, traceback)
        +matches(exception) bool
        +__repr__() str
        +__str__() str
    }

    class RaisesExc {
        +__init__(exception_type=None, match=None, check=None)
        +__enter__()
        +__exit__(exc_type, exc_value, traceback)
        +matches(exception) bool
        +__repr__() str
        +__str__() str
    }

    class Pytester {
        +makepyfile(source:str)
        +runpytest()
        +assert_outcomes(xfailed:int)
    }

    class ExceptionGroup {
        +__init__(message:str, exceptions:tuple[BaseException])
        +add_note(note:str)
    }

    RaisesGroup ..> RaisesExc : uses/contains
    RaisesGroup --> ExceptionGroup : matches against
    RaisesExc --> BaseException : matches against
    test_xfail_raisesgroup ..> Pytester : uses
    test_xfail_RaisesExc ..> Pytester : uses

Summary

The [raises_group.py](/projects/286/67439) file is a detailed and exhaustive test suite that verifies and documents the behavior of Pytest's enhanced exception assertion tools (`RaisesGroup` and `RaisesExc`) for handling Python 3.11 style exception groups. It ensures that users can write precise, flexible, and helpful tests involving complex nested exceptions with clear error reporting and useful suggestions for common issues.

This file is crucial for maintaining correctness and usability of Pytest’s exception group support, interfacing tightly with core Pytest components and Python’s exception group mechanism.


Usage Highlights


If you are extending or debugging Pytest exception assertions, this file serves as an invaluable reference and validation tool.

End of Documentation for raises_group.py