Skip to content

Commit 1fb3322

Browse files
fix(compat): update signatures of model_dump and model_dump_json for Pydantic v1
1 parent 6161879 commit 1fb3322

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/writerai/_models.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,32 +258,41 @@ def model_dump(
258258
mode: Literal["json", "python"] | str = "python",
259259
include: IncEx | None = None,
260260
exclude: IncEx | None = None,
261+
context: Any | None = None,
261262
by_alias: bool | None = None,
262263
exclude_unset: bool = False,
263264
exclude_defaults: bool = False,
264265
exclude_none: bool = False,
266+
exclude_computed_fields: bool = False,
265267
round_trip: bool = False,
266268
warnings: bool | Literal["none", "warn", "error"] = True,
267-
context: dict[str, Any] | None = None,
268-
serialize_as_any: bool = False,
269269
fallback: Callable[[Any], Any] | None = None,
270+
serialize_as_any: bool = False,
270271
) -> dict[str, Any]:
271272
"""Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump
272273
273274
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
274275
275276
Args:
276277
mode: The mode in which `to_python` should run.
277-
If mode is 'json', the dictionary will only contain JSON serializable types.
278-
If mode is 'python', the dictionary may contain any Python objects.
279-
include: A list of fields to include in the output.
280-
exclude: A list of fields to exclude from the output.
278+
If mode is 'json', the output will only contain JSON serializable types.
279+
If mode is 'python', the output may contain non-JSON-serializable Python objects.
280+
include: A set of fields to include in the output.
281+
exclude: A set of fields to exclude from the output.
282+
context: Additional context to pass to the serializer.
281283
by_alias: Whether to use the field's alias in the dictionary key if defined.
282-
exclude_unset: Whether to exclude fields that are unset or None from the output.
283-
exclude_defaults: Whether to exclude fields that are set to their default value from the output.
284-
exclude_none: Whether to exclude fields that have a value of `None` from the output.
285-
round_trip: Whether to enable serialization and deserialization round-trip support.
286-
warnings: Whether to log warnings when invalid fields are encountered.
284+
exclude_unset: Whether to exclude fields that have not been explicitly set.
285+
exclude_defaults: Whether to exclude fields that are set to their default value.
286+
exclude_none: Whether to exclude fields that have a value of `None`.
287+
exclude_computed_fields: Whether to exclude computed fields.
288+
While this can be useful for round-tripping, it is usually recommended to use the dedicated
289+
`round_trip` parameter instead.
290+
round_trip: If True, dumped values should be valid as input for non-idempotent types such as Json[T].
291+
warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
292+
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
293+
fallback: A function to call when an unknown value is encountered. If not provided,
294+
a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError] error is raised.
295+
serialize_as_any: Whether to serialize fields with duck-typing serialization behavior.
287296
288297
Returns:
289298
A dictionary representation of the model.
@@ -300,6 +309,8 @@ def model_dump(
300309
raise ValueError("serialize_as_any is only supported in Pydantic v2")
301310
if fallback is not None:
302311
raise ValueError("fallback is only supported in Pydantic v2")
312+
if exclude_computed_fields != False:
313+
raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
303314
dumped = super().dict( # pyright: ignore[reportDeprecated]
304315
include=include,
305316
exclude=exclude,
@@ -316,15 +327,17 @@ def model_dump_json(
316327
self,
317328
*,
318329
indent: int | None = None,
330+
ensure_ascii: bool = False,
319331
include: IncEx | None = None,
320332
exclude: IncEx | None = None,
333+
context: Any | None = None,
321334
by_alias: bool | None = None,
322335
exclude_unset: bool = False,
323336
exclude_defaults: bool = False,
324337
exclude_none: bool = False,
338+
exclude_computed_fields: bool = False,
325339
round_trip: bool = False,
326340
warnings: bool | Literal["none", "warn", "error"] = True,
327-
context: dict[str, Any] | None = None,
328341
fallback: Callable[[Any], Any] | None = None,
329342
serialize_as_any: bool = False,
330343
) -> str:
@@ -356,6 +369,10 @@ def model_dump_json(
356369
raise ValueError("serialize_as_any is only supported in Pydantic v2")
357370
if fallback is not None:
358371
raise ValueError("fallback is only supported in Pydantic v2")
372+
if ensure_ascii != False:
373+
raise ValueError("ensure_ascii is only supported in Pydantic v2")
374+
if exclude_computed_fields != False:
375+
raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
359376
return super().json( # type: ignore[reportDeprecated]
360377
indent=indent,
361378
include=include,

0 commit comments

Comments
 (0)