Skip to content

Commit 616dac5

Browse files
test: handle model instances in discriminators (#8)
Pydantic invokes the Discriminator callable on both validate and serialize paths. The two harness discriminators only inspected dicts, so on dump they fell through to the first variant and emitted PydanticSerializationUnexpectedValue warnings — ~25 per round-trip pass. Adding isinstance arms before the dict check routes serialization to the correct variant and silences the warnings, with the validate path unchanged.
1 parent aa07c88 commit 616dac5

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

tests/conformance/harness/expectations.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,21 @@ def _discriminate_expected(
224224
blocks themselves don't know that, so we discriminate on the keys
225225
that ARE distinctive and fall back to graph-engine for plain
226226
``final_state``-only fixtures.
227+
228+
Pydantic invokes this callable on both the validation and
229+
serialization paths. On serialization the value is already one of the
230+
concrete variants, so route by ``isinstance`` first; otherwise the
231+
dump path falls through to ``graph_engine`` and warns that the
232+
serialized variant doesn't match the union's first arm.
227233
"""
234+
if isinstance(value, GraphEngineExpected):
235+
return "graph_engine"
236+
if isinstance(value, LlmProviderExpected):
237+
return "llm_provider"
238+
if isinstance(value, PipelineUtilitiesExpected):
239+
return "pipeline_utilities"
240+
if isinstance(value, ObservabilityExpected):
241+
return "observability"
228242
if not isinstance(value, dict):
229243
return "graph_engine"
230244
keys: set[str] = {str(k) for k in cast("dict[str, Any]", value)}

tests/conformance/harness/fixtures.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,17 @@ def _discriminate_fixture(value: Any) -> Literal["llm_provider", "cases", "graph
241241
llm-provider fixtures (e.g. 003-message-validation) have BOTH —
242242
``mock_provider`` is the load-bearing discriminator, ``cases`` is just
243243
the table style for sub-cases.
244+
245+
Also handle the serialization path (where the value is a concrete
246+
variant) so a future ``model_dump`` through the top-level union
247+
doesn't fall through to ``graph`` and warn.
244248
"""
249+
if isinstance(value, LlmProviderFixture):
250+
return "llm_provider"
251+
if isinstance(value, CasesFixture):
252+
return "cases"
253+
if isinstance(value, GraphFixture):
254+
return "graph"
245255
if isinstance(value, dict):
246256
if "mock_provider" in value:
247257
return "llm_provider"

0 commit comments

Comments
 (0)