Skip to content

fix(models): reject polymorphic_serialization on pydantic v1 - #2741

Open
ckarnell wants to merge 1 commit into
cloudflare:mainfrom
ckarnell:fix/polymorphic-serialization-v1-guard
Open

fix(models): reject polymorphic_serialization on pydantic v1#2741
ckarnell wants to merge 1 commit into
cloudflare:mainfrom
ckarnell:fix/polymorphic-serialization-v1-guard

Conversation

@ckarnell

@ckarnell ckarnell commented Aug 1, 2026

Copy link
Copy Markdown

On pydantic v1, BaseModel aliases the v2 method names and raises a clear ValueError for every option v1 cannot honour: mode, round_trip, warnings, context, fallback, serialize_as_any, exclude_computed_fields. polymorphic_serialization is the one exception. It sits in the signature of both model_dump and model_dump_json and is never read.

Measured, not eyeballed: of the 14 keyword-only parameters on model_dump, 13 are either forwarded to super().dict() or guarded by a raise. polymorphic_serialization is neither. Same in model_dump_json, 14 of 15.

So a v1 user who passes it gets no error and no effect, and the call looks like it worked. Every sibling in the same function tells them otherwise.

The fix adds the guard in the existing style, next to the exclude_computed_fields one, in both methods. Four lines.

Verified under pydantic 1.10.26, which is where the shim is actually live. The new test fails without the change with DID NOT RAISE ValueError and passes with it. tests/test_models.py is 46 passed and 13 skipped on v1, and 59 passed on pydantic v2 where the shim is inactive, so nothing regresses on the path most users are on. ruff check and ruff format --check are clean at the repo's own line length.

Note that this is low severity, it needs a v1 user who reaches for a fairly new pydantic option, and the consequence is a silently ignored argument, not wrong output. The argument for fixing it is consistency: thirteen neighbours already do the right thing.

I didn't check whether the generator would reintroduce it. CONTRIBUTING says manual modifications persist between generations, which is why I sent a patch instead of an issue, but I have not seen a regeneration happen.

The pydantic v1 compatibility shim raises a clear ValueError for every v2-only
option it cannot honour: mode, round_trip, warnings, context, fallback,
serialize_as_any and exclude_computed_fields. polymorphic_serialization was the
one exception, present in the signature of both model_dump and model_dump_json
and never read, so a v1 caller passing it got no error and no effect.

Adds the guard in the existing style next to the exclude_computed_fields one, in
both methods, plus a test that fails without the change.
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.

1 participant