AnvilSnapshotState

Documentation for eth_defi.provider.anvil.AnvilSnapshotState Python class.

class AnvilSnapshotState

Bases: object

Mutable reset point for a shared Anvil backend.

This helper is designed for pytest suites that keep one Anvil process alive across multiple tests and reset it cheaply using evm_snapshot / evm_revert instead of relaunching the fork each time.

Use create_anvil_snapshot_state() to take the initial snapshot and reset_anvil_snapshot() to restore it between tests.

Note

Only use this pattern in self-contained test modules or conftest files where the web3 fixture is not overridden by sibling modules. Placing an autouse=True restore fixture in a shared conftest.py causes ScopeMismatch errors when other test modules in the same directory override web3 with function scope (e.g. for a different chain). Additionally, module-scoped Anvil forks combined with repeated snapshot/revert cycles can hang on CI runners under pytest-xdist parallel execution, likely due to Anvil process responsiveness degradation after many revert cycles.

Example:

import pytest

from eth_defi.provider.anvil import AnvilSnapshotState, create_anvil_snapshot_state, reset_anvil_snapshot


@pytest.fixture(scope="module")
def deployed_state(web3, deploy_info) -> AnvilSnapshotState:
    # deploy_info is resolved first so the snapshot captures the
    # expensive post-deployment baseline
    return create_anvil_snapshot_state(web3)


@pytest.fixture(autouse=True)
def restore_deployed_state(web3, deployed_state) -> None:
    reset_anvil_snapshot(web3, deployed_state)

Attributes summary

snapshot_id

Methods summary

__init__(snapshot_id)

__init__(snapshot_id)
Parameters

snapshot_id (int) –

Return type

None