• 
      

    Hide stack traces within assert_queries() from results.

    Review Request #14387 — Created March 21, 2025 and submitted

    Information

    django-assert-queries
    main

    Reviewers

    When assert_queries() reports mismatches, pytest would show stack
    traces going into assert_queries(), with all the logic and docs shown.
    This made for long and annoying stack traces.

    pytest, it turns out, has a handy __tracebackhide__ = True flag you
    can set in any particular traceback frame context to prevent it from
    showing up in stack traces. This needs to be as high up as possible to
    take effect.

    Unfortunately, this can't merely be set in assert_queries() itself or
    in a decorator applying to assert_queries(), as the code needs to run
    in the context manager's __exit__() function. So instead, we need to
    return an explicit context manager instance that sets it at the right
    time.

    We now dynamically construct and return a context manager that sets this
    at the right time. Right now, all this is constructed within
    assert_queries(). It may be worthwhile breaking this and the other
    functions out into a dedicated and more public class down the road, but
    it's unclear at this time if there's much real-world value in doing
    that.

    All unit tests pass.

    Verified that query assertion results appeared with the assert_queries() call.

    Summary ID
    Hide stack traces within assert_queries() from results.
    When `assert_queries()` reports mismatches, pytest would show stack traces going into `assert_queries()`, with all the logic and docs shown. This made for long and annoying stack traces. pytest, it turns out, has a handy `__tracebackhide__ = True` flag you can set in any particular traceback frame context to prevent it from showing up in stack traces. This needs to be as high up as possible to take effect. Unfortunately, this can't merely be set in `assert_queries()` itself or in a decorator applying to `assert_queries()`, as the code needs to run in the context manager's `__exit__()` function. So instead, we need to return an explicit context manager instance that sets it at the right time. We now dynamically construct and return a context manager that sets this at the right time. Right now, all this is constructed within `assert_queries()`. It may be worthwhile breaking this and the other functions out into a dedicated and more public class down the road, but it's unclear at this time if there's much real-world value in doing that.
    95b235349406a9ed9f906dd46ee19e0f17f97947
    maubin
    1. Ship It!
    2. 
        
    david
    1. Ship It!
    2. 
        
    chipx86
    Review request changed
    Status:
    Completed