You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug(types): freezegun's freeze_time() crashes or leaves models unbuildable because of lazy imports, import-time TypeAdapters and defer_build #1310
On 8.0.0, adopters' tests that use freezegun.freeze_time() break in two ways.
freeze_time().start() walks sys.modules and getattr()s every attribute after it has swapped datetime.date for FakeDate. adcp resolves its top-level names lazily (adcp/__init__.py:879-915), so the walk imports most of the package mid-freeze. adcp/negotiation.py:33-34 builds TypeAdapter(RefineProposalsRequest) at import time, and that build fails. The exception escapes freeze_time().__enter__, and datetime.date stays FakeDate for the rest of the process.
Every model uses defer_build=True (adcp/types/base.py:270-275). If a model's first validate or serialize happens inside freeze_time(), the schema build fails, even when adcp is on freezegun's ignore list.
Which tests fail depends on test order: whether something built the model, or imported the lazy name, before time was frozen.
importdatetimefromadcp.typesimportAcquireRightsRequest# noqa: F401fromfreezegunimportfreeze_timetry:
withfreeze_time("2026-01-01"):
passexceptExceptionasexc:
print("freeze_time:", type(exc).__name__, "|", str(exc).splitlines()[0][:90])
print("datetime.date after the failed start:", datetime.date)
freeze_time: PydanticSchemaGenerationError | Unable to generate pydantic-core schema for <class 'datetime.date'>. Set `arbitrary_types_
datetime.date after the failed start: <class 'freezegun.api.FakeDate'>
The traceback runs freezegun/api.py_get_module_attributes -> adcp/__init__.py__getattr__ -> adcp/negotiation.py line 34.
schema built before freeze: False
first build inside freeze_time: PydanticSchemaGenerationError | Unable to generate pydantic-core schema for <class 'datetime.date'>. Set `arbitrary_types_
Why an adopter hits this
freezegun is the usual way to test date-dependent seller logic: flight dates, delivery windows, reporting periods. Embedded Sales Agent's tests/conftest.py carries two workarounds:
freezegun.configure(extend_ignore_list=["adcp"]);
a monkeypatch of the private freezegun.api._freeze_time.start that first walks AdCPBaseModel.__subclasses__() and calls model_rebuild(force=True) on every model with __pydantic_complete__ still false.
Before those, failures appeared only in some CI shards. One failed start also left the clock frozen for every later test in the process.
Ask
Don't build TypeAdapters at module import. Build them on first use (e.g. a functools.cache'd accessor). 14 modules do this today, including adcp/negotiation.py:33-34, adcp/decisioning/negotiation.py:30-31, adcp/compat/purchase_continuation.py:44 and several under adcp/reporting/.
Add a public helper that finishes all deferred builds for imported models (e.g. adcp.testing.build_all_models()), so test suites can call it once before freezing time instead of walking SDK internals.
Optionally, document the freezegun interaction in the testing guide.
Classification: Bug Bucket(s): client, types (no exact label match found — flagged in run summary) Status: ready-to-implement Milestone: omitted (no version specified)
What the experts said:
code-reviewer: approved non-breaking fix overall — one blocker: AdCPBaseModel.__subclasses__() is a shallow walk that misses all multi-level descendants; must use a recursive walk
code-reviewer: model_rebuild(force=True) is expensive across 700+ models; prefer force=False (matches existing _build_deferred_serializers pattern) or justify why force-rebuild is needed
dx-expert: approved — adcp.testing is the correct namespace, build_all_models() with no params is the right v1 surface; ship with a docstring that includes a conftest snippet
My take: This is a clear, well-diagnosed bug with a straight fix path. Both root causes (import-time TypeAdapter construction + deferred schema builds) are internal SDK concerns, not spec or protocol changes. The fix is additive/internal-refactor only — no public API removals, no signature changes, no error-class changes.
Implementation scope:
Lazy TypeAdapters (~8–9 modules). Wrap each module-level TypeAdapter(...) in a @functools.cache-decorated function. The private names (_REFINE_REQUEST_ADAPTER, _CORE, _EVENT, etc.) are not in any __all__; callers in the same file use the function instead. Files: src/adcp/negotiation.py, src/adcp/decisioning/negotiation.py, src/adcp/reporting/receipts/records.py, src/adcp/reporting/receipts/capture.py, src/adcp/reporting/materializer/capture.py, src/adcp/reporting/projection/capture.py, src/adcp/reporting/projection/history.py, src/adcp/reporting/ledger/notification_models.py. Confirm whether src/adcp/types/legacy.py:_URL_ADAPTER (wraps AnyUrl, no datetime.date dep) needs to be included — check the repro traceback.
adcp.testing.build_all_models(). Add to src/adcp/testing/ (module already exists). Must use a recursive__subclasses__() walk — the shallow form misses multi-level descendants (Countries7(Countries5), CoverageGap(DistributionId), envelope subclasses, etc.). Use model_rebuild(force=False) to skip already-complete models (matches the existing pattern in AdCPBaseModel._build_deferred_serializers). Export from adcp.testing.__all__. Docstring must show the conftest call site (adcp.testing.build_all_models() at module level, before any freeze_time block).
No new public deps. The new function must not pull in any heavy test-only dep at import time.
Required checks:pytest on affected modules, mypy src/, ruff check . / black --check ..
PR title:fix(types): lazy TypeAdapter construction and build_all_models() for freezegun compatibility (Release Please reads conventional-commits titles for patch bump).
Activity
bokelley commentedon Oct 2, 2026
Triage
Classification: Bug
Bucket(s): client, types (no exact label match found — flagged in run summary)
Status: ready-to-implement
Milestone: omitted (no version specified)
What the experts said:
AdCPBaseModel.__subclasses__()is a shallow walk that misses all multi-level descendants; must use a recursive walkmodel_rebuild(force=True)is expensive across 700+ models; preferforce=False(matches existing_build_deferred_serializerspattern) or justify why force-rebuild is neededadcp.testingis the correct namespace,build_all_models()with no params is the right v1 surface; ship with a docstring that includes a conftest snippetMy take: This is a clear, well-diagnosed bug with a straight fix path. Both root causes (import-time
TypeAdapterconstruction + deferred schema builds) are internal SDK concerns, not spec or protocol changes. The fix is additive/internal-refactor only — no public API removals, no signature changes, no error-class changes.Implementation scope:
TypeAdapter(...)in a@functools.cache-decorated function. The private names (_REFINE_REQUEST_ADAPTER,_CORE,_EVENT, etc.) are not in any__all__; callers in the same file use the function instead. Files:src/adcp/negotiation.py,src/adcp/decisioning/negotiation.py,src/adcp/reporting/receipts/records.py,src/adcp/reporting/receipts/capture.py,src/adcp/reporting/materializer/capture.py,src/adcp/reporting/projection/capture.py,src/adcp/reporting/projection/history.py,src/adcp/reporting/ledger/notification_models.py. Confirm whethersrc/adcp/types/legacy.py:_URL_ADAPTER(wrapsAnyUrl, nodatetime.datedep) needs to be included — check the repro traceback.adcp.testing.build_all_models(). Add tosrc/adcp/testing/(module already exists). Must use a recursive__subclasses__()walk — the shallow form misses multi-level descendants (Countries7(Countries5),CoverageGap(DistributionId), envelope subclasses, etc.). Usemodel_rebuild(force=False)to skip already-complete models (matches the existing pattern inAdCPBaseModel._build_deferred_serializers). Export fromadcp.testing.__all__. Docstring must show the conftest call site (adcp.testing.build_all_models()at module level, before anyfreeze_timeblock).pyteston affected modules,mypy src/,ruff check ./black --check ..fix(types): lazy TypeAdapter construction and build_all_models() for freezegun compatibility(Release Please reads conventional-commits titles for patch bump).Triaged by Claude Code. Session: https://claude.ai/code/session_01AJ6jSUcJ7TDpmfs9qEfm6o
Generated by Claude Code