:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(tests): cheapen freezegun's per-freeze module cache validation (#69433)
Paul D'Ambra9b9336f2cfbc
diff --git a/conftest.py b/conftest.pyindex 780bbbfff618..7a41afc825a2 100644--- a/conftest.py+++ b/conftest.py@@ -173,11 +173,38 @@ def _cache_url_resolution() -> None:resolvers.URLResolver.resolve = resolve # type: ignore[method-assign] # ty: ignore[invalid-assignment]+def _cheapen_freezegun_module_hash() -> None:+ # Every freeze_time().start() revalidates freezegun's per-module patch cache by+ # hashing each loaded module's attribute list: hash(frozenset(dir(module))) across+ # every module in sys.modules, per freeze. dir() sorts and materializes a list per+ # module, so freeze-heavy suites pay seconds per run for it (2.25M hash calls in a+ # profiled replay-listing run). tuple(module.__dict__) carries the same invalidation+ # signal ~6x cheaper: every module attribute add/delete mutates __dict__ (dir() has+ # no extra visibility for cache purposes — PEP 562 lazy attrs only materialize into+ # __dict__ anyway), and both keys share the same blind spot (rebinding an existing+ # name), so semantics are unchanged. Installed before any freeze so the cache never+ # mixes hash schemes.+ import types # noqa: PLC0415 — deferred until pytest_configure++ from freezegun import api # noqa: PLC0415 — deferred until pytest_configure++ def _fast_module_attributes_hash(module: types.ModuleType) -> str:+ try:+ keys_hash = hash(tuple(module.__dict__))+ except (ImportError, TypeError, AttributeError):+ keys_hash = 0+ return f"{id(module)}-{keys_hash}"++ _fast_module_attributes_hash.__wrapped__ = api._get_module_attributes_hash # type: ignore[attr-defined]+ api._get_module_attributes_hash = _fast_module_attributes_hash # ty: ignore[invalid-assignment]++def pytest_configure(config) -> None:_cache_reverse_rel_identity()_cache_select_masks()_cache_drf_field_info()_cache_url_resolution()+ _cheapen_freezegun_module_hash()def pytest_collection_finish() -> None: