Skip to content

test: handle model instances in fixture discriminators#8

Merged
chris-colinsky merged 1 commit into
mainfrom
fix/expected-block-serialization
May 5, 2026
Merged

test: handle model instances in fixture discriminators#8
chris-colinsky merged 1 commit into
mainfrom
fix/expected-block-serialization

Conversation

@chris-colinsky

Copy link
Copy Markdown
Member

Summary

  • Phase 0 follow-up. The two Discriminator(...) callables in the conformance harness (_discriminate_expected in expectations.py, _discriminate_fixture in fixtures.py) previously only handled dicts.
  • Pydantic v2 calls the discriminator on both validate and serialize paths. On dump the value is already a concrete variant, not a dict, so the callable fell through to the first union arm and pydantic emitted PydanticSerializationUnexpectedValue warnings — ~25 per round-trip pass.
  • Adding isinstance arms before the dict check routes serialization to the correct variant. Validate path is unchanged.

Test plan

  • uv run pytest tests/conformance/test_fixture_parsing.py -W error::UserWarning — 137 passed, zero warnings (was 25 before)
  • uv run pytest -q — 204 passed, 7 skipped
  • uv run pyright tests/conformance/harness/ — 0 errors
  • uv run ruff check tests/conformance/harness/ — clean

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.
Copilot AI review requested due to automatic review settings May 5, 2026 06:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the conformance harness’ Pydantic v2 discriminator callables to correctly handle both validation (dict inputs) and serialization (already-instantiated model inputs), preventing PydanticSerializationUnexpectedValue warnings during model_dump round-trips.

Changes:

  • Add isinstance(...)-based routing to _discriminate_fixture so serialized fixture variants map to the correct union tag.
  • Add isinstance(...)-based routing to _discriminate_expected so serialized expected-block variants map to the correct union tag.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/conformance/harness/fixtures.py Discriminator now recognizes concrete fixture model instances before dict-key discrimination.
tests/conformance/harness/expectations.py Discriminator now recognizes concrete expected-block model instances before dict-key discrimination.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@chris-colinsky chris-colinsky merged commit 616dac5 into main May 5, 2026
9 checks passed
@chris-colinsky chris-colinsky deleted the fix/expected-block-serialization branch May 5, 2026 06:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants