Skip to content

Turn off Hypothesis's explain phase for the owners' draw simulation properties - #10823

Merged
MaxGhenis merged 1 commit into
mainfrom
owners-draw-hypothesis-phases
Nov 13, 2026
Merged

MaxGhenis merged 1 commit into
mainfrom
owners-draw-hypothesis-phases

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

This PR gives SIMULATION_SETTINGS in policyengine_us/tests/reforms/test_owners_draw.py every Hypothesis phase except Phase.explain. Three properties share those settings and build full simulations inside @given: the raised-cap property, the disabled-reform property and the benefit-credit property. We keep every assertion, strategy and explicit example.

The settings previously named no phases, so Hypothesis kept all six phases enabled. Under ordinary test settings, Hypothesis starts tracing after a property fails. When a change breaks one of these properties, Hypothesis keeps calling it before pytest reports the failing example:

  • With shrink and explain enabled, Hypothesis runs each call after the first failure under a line tracer, apart from the final replay (_should_trace). Python calls that tracer on each line event. The tracer records pairs of consecutive lines outside Hypothesis's files (trace_line).
  • After Hypothesis shrinks the example, it varies each part of the result in its explain phase and calls the property again (explain experiments).
  • Hypothesis checks its 300-second shrinking deadline after each call (MAX_SHRINKING_SECONDS, deadline check).

One-machine timings

Every timing below comes from one macOS machine that also ran other work. For #10548, we made copies of two properties fail after their simulation work, on Python 3.14.7 with policyengine-core 3.34.1:

  • Hypothesis called the raised-cap copy 151 times: the explicit example, 7 generated examples, 71 shrinking calls, 71 explaining calls and the final replay. It spent 103 seconds shrinking and 198 seconds explaining, then stopped at its five-minute limit. Pytest reported the failure after 389 seconds.
  • The disabled-reform copies stopped before pytest reported an example. They made 15 calls over 829 seconds without Speed up the owners' draw reform tests #10548's changes, and 18 calls over 588 seconds with them.

On Python 3.13.9 with policyengine-core 3.33.2 and Hypothesis 6.168.5, we called the disabled-reform property's body at 3f6e604b without the tracer and then inside it. The traced call took 25.6 seconds against 10.4. We then ran the same body as a Hypothesis property that fails after its simulation work:

Phases Seconds per call up to the first failure Seconds per call after it
The file's previous settings 7.2, 7.0, 7.7 26.5, 46.8 (traced)
Without Phase.explain 15.2, 10.3, 12.4 13.5, 11.4 (untraced)

Other work on the machine changed between the rows, so compare the columns within each row. These historical timings do not measure a passing-run speedup.

Reproduction

This script needs Hypothesis and Python 3.12 or later. It copies the disabled-reform property's previous settings, strategy and explicit example. Its body fails when a batch holds an age other than 20, 45, 62 or 85, so the explicit example and Hypothesis's simplest batch pass.

import sys

from hypothesis import HealthCheck, Phase, example, given, settings
from hypothesis import strategies as st

# Settings, strategy and explicit example copied from test_owners_draw.py.
SIMULATION_SETTINGS = dict(
    max_examples=2,
    deadline=None,
    derandomize=True,
    suppress_health_check=[
        HealthCheck.too_slow,
        HealthCheck.data_too_large,
        HealthCheck.function_scoped_fixture,
    ],
)
amount = st.integers(0, 600_000)
person = st.tuples(
    st.integers(20, 85),
    amount,
    st.integers(-40_000, 250_000),
    st.integers(-100_000, 600_000),
    st.integers(-100_000, 300_000),
    st.integers(0, 80_000),
    st.integers(0, 80_000),
)
EXPLICIT = [
    (62, 0, 0, 150_000, 0, 0, 0),
    (45, 300_000, 50_000, -50_000, 50_000, 0, 0),
    (85, 100_000, -20_000, 0, 100_000, 40_000, 20_000),
]


def count(label, **extra):
    calls = {"failed": 0, "later": 0, "traced": 0}

    @settings(**SIMULATION_SETTINGS, **extra)
    @given(batch=st.lists(person, min_size=3, max_size=8))
    @example(batch=EXPLICIT)
    def prop(batch):
        if calls["failed"]:
            calls["later"] += 1
            # Hypothesis registers its line tracer as sys.monitoring tool 3.
            calls["traced"] += sys.monitoring.get_tool(3) is not None
        # A stand-in failure: the explicit example and the simplest batch pass.
        if not all(age in (20, 45, 62, 85) for age, *_ in batch):
            calls["failed"] += 1
            raise AssertionError

    try:
        prop()
    except AssertionError:
        pass
    print(f"{label}: {calls['later']} calls after the first failure, {calls['traced']} traced")


count("the file's settings")
count("without Phase.explain", phases=[p for p in Phase if p is not Phase.explain])
count("explicit and generate only", phases=[Phase.explicit, Phase.generate])

Python 3.13.9 with Hypothesis 6.168.5 prints:

the file's settings: 2007 calls after the first failure, 2006 traced
without Phase.explain: 76 calls after the first failure, 0 traced
explicit and generate only: 1 calls after the first failure, 0 traced

Each call here returns at once, so Hypothesis finishes its explain phase. A call of the real properties runs one or two simulations. The raised-cap copy reached the five-minute limit after 142 shrinking and explaining calls.

Failure behavior

Without Phase.explain, Hypothesis runs ordinary failing calls untraced and makes 76 calls after the first failure in the script, against 2,007 with the previous settings. A failure report then loses two diagnostics: the "Explanation" lines, which Hypothesis builds from the traced calls (explanatory_lines), and its comments on arguments it could vary without losing the failure (explain gate). Hypothesis still shrinks a failure under the same 300-second limit. A property that takes ten seconds per call can still run to that limit.

For example, test_state_income_tax_accounting_invariants.py names only explicit and generate. Hypothesis then reports a failing example unshrunk. We retain shrinking for the owners' draw properties.

Invariants and validation

Pull request run 43480011691 at 003ab5b7f1 passed all 36 jobs on GitHub's merge with main a93cc58d7e.

  • The diff keeps every assertion, strategy, explicit example and setting apart from SIMULATION_SETTINGS.phases. An AST comparison with fresh main matches after removing the new Phase import and phases entry.
  • At the head, pytest reported 38 passed in 294.51 seconds. The timed command took 455.68 seconds including startup.
  • We restored the original phases on the decorated functions at collection and reran the three affected properties, including both raised-cap routes. Pytest reported 4 passed and 34 deselected in 264.30 seconds; the timed command took 427.61 seconds.
  • An audit plugin recorded the same 17 situation payloads in the same order across both runs: 3 per raised-cap route, 3 for disabled reform and 8 for benefit credit. All five retained phases match; shrinking remains enabled.
  • We reran the reproduction on Python 3.13.9 with Hypothesis 6.168.5: 2,007 later calls with the previous settings, 76 without explain and 1 with explicit and generate only. The last two configurations produced no traced calls.
  • Expect no population effect because the diff changes test settings and a changelog fragment only.

Methodology choices

  • Keep every phase except Phase.explain, including shrinking and its 300-second limit.
  • Keep the change within SIMULATION_SETTINGS; formula-level properties retain their settings.
  • Add no YAML or permanent property tests because this change affects test settings only. Existing properties exercise the unchanged assertions, strategies and examples.
  • Use the available Python 3.13.9 environment for local validation. The lock pins Hypothesis 6.168.3 and policyengine-core 3.34.2; the local environment holds Hypothesis 6.168.5 and core 3.33.2.

A follow-up can decide whether to load a suite-wide Hypothesis profile without Phase.explain before test modules import, so that Hypothesis applies it to properties that name no phases.

Microsimulation impact

This PR edits Hypothesis settings in one test file and adds a changelog fragment, so it changes no formula, parameter or dataset and leaves nothing to measure.

impact: none: the change touches one test file's Hypothesis settings and a changelog fragment

axiom: n/a: Hypothesis test settings and a changelog fragment; no policy change

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MaxGhenis MaxGhenis changed the title Turn off Hypothesis's explain phase for owners' draw simulation tests Turn off Hypothesis's explain phase for the owners' draw simulation properties Nov 12, 2026
@MaxGhenis
MaxGhenis marked this pull request as ready for review November 13, 2026 18:10
@MaxGhenis
MaxGhenis merged commit 24dd00e into main Nov 13, 2026
43 checks passed
@MaxGhenis

Copy link
Copy Markdown
Contributor Author

US + core hub merge audit, us#10823 at 003ab5b7f10a406428d56900c2c363d9a6c94f63 (squash): Turn off Hypothesis's explain phase for the owners' draw simulation properties.

  • Reviews:
    • The PR has no GitHub reviews. One independent review ran outside GitHub and approved 003ab5b7f1 with no P1 or P2 finding.
    • It read the diff against the merge base 935b24ff4f. The diff adds Phase to the Hypothesis import, gives SIMULATION_SETTINGS every phase except Phase.explain and adds a changelog fragment. It touches no formula, parameter, YAML test or source capture.
    • It loaded test_owners_draw.py from the base and from the head in one process and compared the settings of all 13 @given tests. Three changed: the raised-cap, disabled-reform and benefit-credit properties each dropped explain and kept the other five phases and max_examples 2. The other 10 match.
    • It read Hypothesis at 44b82b24cb, the commit for version 6.168.3, which uv.lock pins. _should_trace traces a failing call only when the phases hold both shrink and explain, and shrinking keeps its 300-second limit. The repository registers or loads no Hypothesis profile, so the three properties ran all six phases before this PR, locally and in CI.
    • It ran a cheap failing stand-in property under both settings. Under the base settings Hypothesis made 2,008 calls and traced 2,004 of them in 39.7 seconds. Under the head settings it made 80 calls, traced none, took 2.0 seconds and still reported the failure. The description's reproduction script printed the counts the description gives.
    • It ran test_owners_draw.py at the head on Hypothesis 6.168.5 and policyengine-core 3.33.2, where the lock pins 6.168.3 and 3.34.2, and all 38 tests passed. It didn't run the file at the base: Hypothesis traces and explains only after a failure, so passing runs take the same path under both settings.
    • It didn't reproduce the description's one-machine timings or its comparison of two passing runs. No claim about the code rests on either.
    • I applied its description corrections before merge. The description now says the PR leaves nothing to measure and names the two diagnostics a failure report loses without explain. It drops the warning counts the review's run didn't reproduce, and it turns its open question into a follow-up.
  • Gates checked live at merge:
    • gh pr checks exits 0;
    • the latest Pull request run at the head, 43480011691, passed and tested this head merged into main at a93cc58d7e;
    • MERGEABLE;
    • not a draft;
    • no reviewer's latest review requests changes;
    • the semantic-overlap guard: since a93cc58d7e, main has changed no area this PR touches. Between the merge base 935b24ff4f and main at 5959a14359, main changed neither of the PR's two files, and the head merges into that main without conflict;
    • the merge pins the head.
  • Impact: none. The PR edits Hypothesis settings in one test file and adds a changelog fragment, so it changes no formula, parameter or dataset and leaves nothing to measure.
  • Next:
    • No test guards the setting. A revert, or an edit that also drops Phase.shrink, passes every test, since Hypothesis runs explain only after a failure. A follow-up can add a module-level assertion that SIMULATION_SETTINGS["phases"] holds Phase.shrink and omits Phase.explain.
    • A follow-up can decide whether to load a suite-wide Hypothesis profile without Phase.explain, so that properties that name no phases skip explain too. The other 10 properties in this file still run all six phases.
  • Axiom: n/a, since the PR changes test settings and no provision.

🤖 Generated with Claude Code

@MaxGhenis
MaxGhenis deleted the owners-draw-hypothesis-phases branch November 13, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant