Cache Session.gethookproxy during collection#6
Open
adamtheturtle wants to merge 5 commits into
Open
Conversation
Cache path-sensitive hook proxies and invalidate when conftests load. Do not cache before a directory is recorded in _dirpath2confmods, which previously could permanently strip root conftests from Package collection (pytest-dev#2016/pytest-dev#8991). Co-authored-by: Cursor <cursoragent@cursor.com>
Keep comments focused on current invalidation rules; leave pytest-dev#2016 context for the PR description. Co-authored-by: Cursor <cursoragent@cursor.com>
Return-type / ihook annotation cleanup lives in a separate PR. Co-authored-by: Cursor <cursoragent@cursor.com>
Describe current invariants only; leave past context to the PR description. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Session.gethookproxyby path to avoid repeated conftest-set lookups during collection (addresses_pytest.main.Session.gethookproxyis called many times with the same argument on large suites pytest-dev/pytest#8991)._loadconftestmoduleshas recorded its directory, so a prematureFSHookProxycannot stick after the directory is associated with already-imported conftests.Typing of
gethookproxy/ihookasHookRelay | FSHookProxyis intentionally not in this PR; see pytest-dev/pytest#14728.Background (pytest-dev#2016)
gethookproxy(path)returns either the full hook relay or anFSHookProxythat strips conftests that do not apply to that path. An earlier path→proxy cache was removed in #2073 because it caused pytest-dev#2016:Given siblings
demo-0,demo-a(withpytest_ignore_collect → True), anddemo-b:rootcached a “complete” proxy before any local conftest existed.demo-aloaded its conftest onto that same cached complete proxy.rootreused the poisoned proxy, sodemo-a’s ignore hook incorrectly affected siblings likedemo-b.The 2016 fix was to drop caching entirely and recompute every call. This PR restores caching for large-suite performance (pytest-dev#8991), but only with invalidation that covers that failure mode:
len(_conftest_plugins)grows_dirpath2confmods_conftest_plugins, so generation alone would not clear a staleFSHookProxyTiming
Warm interleaved A/B
pytest --collect-only -q(alternate order). +% = faster.Home Assistant (
tests/, 122 476 tests, 3 pairs)mainvws-python-mock (1 403 tests, 10 pairs)
mainOn HA,
gethookproxyruns ~233k times across ~8k unique paths, so cache hits dominate. On vws-python-mock (~2k calls / ~130 paths, ~0.2s collect) absolute savings are tiny and the measured delta is within run-to-run noise.pytest’s own
testing/suite (~4k tests, 12 pairs)Within noise (
main0.535s → 0.540s, −0.9%).Test plan
testing/test_pluginmanager.py::TestPytestPluginInteractions::test_hook_proxy(pytest_ignore_collect applies to all directories collected afterwards? pytest-dev/pytest#2016-style refresh after new conftest)testing/test_pluginmanager.py::...::test_hook_proxy_not_cached_before_directory_conftests_loadedtesting/test_conftest.py::test_hook_proxy(siblingignore_collectmust not leak)testing/test_conftest.py::test_root_collect_file_applies_inside_package