<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Floris and Florian,<br><br>Thanks for the feedback. As I mentioned, this is something I was playing with as an experiment. My specific use case would not be satisfied with this change but I thought I'd ask if this were possible since it was an idea I stumbled across while investigating my usecase. I will, however, detail my usecase just so you know what I'm working on.</div><div dir="ltr"><br></div><div dir="ltr">I've been developing a package that repurposes software testing practices for data validation (<a href="https://datatest.rtfd.io/">https://datatest.rtfd.io/</a>).<br><br></div><div dir="ltr">A simple example using the datatest's validate() function:</div><div dir="ltr"><br></div><div dir="ltr">    from datatest import validate<br>    <br>    def test_somedata():<br>        data = {'A', 'B', 'C', 'D'}<br>        requirement = {'A', 'B'}<br>        validate(data, requirement)<br></div><div><br></div><div>The test above will fail with the following exception:<br></div><div><br></div><div>    ValidationError: does not satisfy set membership (2 differences): [<br>        Extra('C'),<br>        Extra('D'),<br>    ]</div><div><br></div><div>The ValidationError is a subclass of AssertionError. It contains one 
item for each element in `data` that fails to satisfy the `requirement`.</div><div><br></div><div>I was playing around with the idea of a marker to implement something like the following (see below) but still generate the ValidationError output via pytest's plugin system:<br></div><div><br></div><div><div><div>    import pytest<br></div><div><br></div><div dir="ltr"><div>    @pytest.mark.validate<br></div><div dir="ltr">    def test_somedata():<br>        data = {'A', 'B', 'C', 'D'}<br>        requirement = {'A', 'B'}</div>        assert data == requirement<br><div><br></div><div>The idea is to use an `assert` statement but still get the ValidationError output (although there are many cases where using "==" is semantically wrong so the validate() function wouldn't go away in all cases). While I could emulate this type of output with pytest_assertrepr_compare(), I need to do more than emulate the error.<br></div><div><br></div><div>Here's where things get more involved: The datatest package also implements a system of acceptances that operate on the items contained within the ValidationError. If a user decides that the "Extra" items are acceptable, they can use the accepted() context manager:</div></div></div></div><div><br></div><div>    from datatest import validate, accepted, Extra</div><div><br></div><div dir="ltr"><div dir="ltr">    def test_somedata():<br>        data = {'A', 'B', 'C', 'D'}<br>        requirement = {'A', 'B'}</div><div>        with accepted(Extra):<br></div><div dir="ltr">            validate(data, requirement)<br></div><div><br></div><div>In this case, the accepted() context manager accepts all of the items in the error (which suppresses the error entirely). But if it contained other items that were not accepted, then an error would be re-raised with the remainder of the items.</div><div><br></div><div>Below, I've added comments showing why the assertrepr comparison can't work with the context manager:<br></div><div><br></div><div><div>    import pytest<br></div><div>    from datatest import validate, accepted, Extra</div><div><br></div><div dir="ltr"><div>    @pytest.mark.validate<br></div><div dir="ltr">    def test_somedata():<br>        data = {'A', 'B', 'C', 'D'}<br>        requirement = {'A', 'B'}</div><div>        with accepted(Extra):  # <- Never receives an inspectible error!<br></div><div dir="ltr">            assert data == requirement  # <- Raises an assertion that can not be inspected or changed!<br></div><div><br></div></div></div><div>To implement my dream "validate marker", I would need to manipulate the assertion inside the scope of the context manager--which is what the validate() function does. But doing this is far from what pytest_assertrepr_compare() has access to.</div><div><br></div><div>One solution I could see is an entirely new hook:</div><div><br></div><div>    def pytest_do_assertion(item, op, left, right=None):<br>        """ perform comparison<br>        <br>        Returns an AssertionError or None """<br></div><div><br></div><div>But from what I can see, implementing a hook like `pytest_do_assertion` could require unreasonably invasive changes to pytest. That said, I would be very interested if something like `pytest_do_assertion()` were possible to add but I understand if it's not. Also, the hook name is just a throwaway idea, it's probably not appropriate.<br></div><div><br></div><div>-Shawn<br></div></div></div></div></div></div></div></div></div></div></div>