From c7ad1ac68975ecff4755e5f930bd7f3cfff16884 Mon Sep 17 00:00:00 2001 From: Cohen Karnell Date: Sat, 1 Aug 2026 02:50:33 -0400 Subject: [PATCH] fix(models): reject polymorphic_serialization on pydantic v1 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. --- src/cloudflare/_models.py | 4 ++++ tests/test_models.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/cloudflare/_models.py b/src/cloudflare/_models.py index 4fd83e1340d..d08376c2199 100644 --- a/src/cloudflare/_models.py +++ b/src/cloudflare/_models.py @@ -335,6 +335,8 @@ def model_dump( raise ValueError("fallback is only supported in Pydantic v2") if exclude_computed_fields != False: raise ValueError("exclude_computed_fields is only supported in Pydantic v2") + if polymorphic_serialization is not None: + raise ValueError("polymorphic_serialization is only supported in Pydantic v2") dumped = super().dict( # pyright: ignore[reportDeprecated] include=include, exclude=exclude, @@ -398,6 +400,8 @@ def model_dump_json( raise ValueError("ensure_ascii is only supported in Pydantic v2") if exclude_computed_fields != False: raise ValueError("exclude_computed_fields is only supported in Pydantic v2") + if polymorphic_serialization is not None: + raise ValueError("polymorphic_serialization is only supported in Pydantic v2") return super().json( # type: ignore[reportDeprecated] indent=indent, include=include, diff --git a/tests/test_models.py b/tests/test_models.py index 73f4ab99dd6..b8c038d2c5a 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -537,6 +537,21 @@ class Model2(BaseModel): m.to_dict(warnings=False) +def test_polymorphic_serialization_is_rejected_on_pydantic_v1() -> None: + class Model(BaseModel): + foo: int = 1 + + m = Model() + if PYDANTIC_V1: + with pytest.raises(ValueError, match="polymorphic_serialization is only supported in Pydantic v2"): + m.model_dump(polymorphic_serialization=True) + with pytest.raises(ValueError, match="polymorphic_serialization is only supported in Pydantic v2"): + m.model_dump_json(polymorphic_serialization=True) + + # the default must keep working on both major versions + assert m.model_dump() == {"foo": 1} + + def test_forwards_compat_model_dump_method() -> None: class Model(BaseModel): foo: Optional[str] = Field(alias="FOO", default=None)