test: handle model instances in fixture discriminators#8
Merged
Conversation
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.
There was a problem hiding this comment.
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_fixtureso serialized fixture variants map to the correct union tag. - Add
isinstance(...)-based routing to_discriminate_expectedso 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.
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
Discriminator(...)callables in the conformance harness (_discriminate_expectedinexpectations.py,_discriminate_fixtureinfixtures.py) previously only handled dicts.PydanticSerializationUnexpectedValuewarnings — ~25 per round-trip pass.isinstancearms 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 skippeduv run pyright tests/conformance/harness/— 0 errorsuv run ruff check tests/conformance/harness/— clean