pytest-antilru
Bust functools.lru_cache when running pytest to avoid test pollution
What it is and what it does
pytest-antilru is a pytest plugin that automatically clears functools.lru_cache between test runs. When you use lru_cache in your application code and then mock or patch dependencies in tests, the cache can retain stale values from previous test runs, causing tests to fail or pass unexpectedly depending on execution order. This plugin solves that by busting the cache between tests, restoring proper test isolation.
The plugin works automatically once installed—no code changes needed. By default it disables lru_cache everywhere, but you can configure an allowlist of specific module paths where caching should remain active, useful when other dependencies rely on cache behavior to function correctly within a single test run.
Use it for:
- Testing functions that use lru_cache with mocked network calls or external dependencies that return different values across test cases.
- Ensuring test order independence when cached functions are involved, preventing flaky tests that pass or fail based on execution sequence.
- Selectively disabling cache for specific modules while preserving it in others to avoid breaking dependencies that rely on caching.
- Debugging test pollution issues caused by persistent cached state from previous test runs.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Clears functools.lru_cache between pytest test runs to prevent test pollution from cached values persisting across tests.
Yes. This is a targeted fix for a real problem—test pollution from lru_cache—with low install friction, active maintenance, no vulnerabilities, and permissive licensing. If you use lru_cache in code under test and have experienced test ordering issues or mocking problems, this plugin eliminates the problem with zero code changes.
Install
pytest-antilru on PyPI
pip
pip install pytest-antilruuv
uv add pytest-antilrupoetry
poetry add pytest-antilruInstalling pytest-antilru
Before you install
Low friction install as a pure Python wheel with only pytest as a runtime dependency. Active maintenance with a recent release (103 days ago) and no known vulnerabilities.
License in practice
MIT license permits unrestricted use, modification, and distribution in both open-source and proprietary projects.
Quickstart
pip install pytest-antilru
# Once installed, pytest automatically clears lru_cache between test runs.
# Optional: configure in pytest.ini to target specific modules:
# [tool.pytest.ini_options]
# lru_cache_disabled = 'my_module.util'
Requires Python >=3.9 and pytest >=3 (pytest <9 for Python 3.9, <10 for Python 3.10+).
Verify before relying
- Whether disabling lru_cache globally (the default) impacts performance of the test suite itself.
- Behavior when lru_cache is used in third-party dependencies that expect caching to persist across test runs.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — pytest |
| Maintenance | actively maintained — 103 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 239,801/month — #8,915 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_antilru-2.0.1-py2.py3-none-any.whl
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
backports.functools-lru-cacheProvides the lru_cache decorator from Python…
permissive · top 5,000 on PyPI
async-lruAn LRU cache decorator for async functions that…
permissive · top 1,000 on PyPI
methodtoolsProvides LRU caching for methods, classmethods,…
permissive · top 5,000 on PyPI
detect-test-pollutionDetects test pollution—failures caused by side…
permissive · top 15,000 on PyPI
cacheboxCachebox is a high-performance in-memory…
permissive · top 5,000 on PyPI
cachetoolsProvides memoizing collections and function…
permissive · top 1,000 on PyPI
onecacheOneCache provides LRU-based caching decorators…
permissive · top 15,000 on PyPI
functools32Backport of Python 3.2's functools module…
permissive · top 15,000 on PyPI
memoizationDecorator-based function result caching with…
permissive · top 5,000 on PyPI
pytest-socketA pytest plugin that blocks or restricts socket…
permissive · top 5,000 on PyPI