|
3 | 3 | import inspect
|
4 | 4 | from typing import TYPE_CHECKING, Any, Type, Union, Generic, TypeVar, cast
|
5 | 5 | from datetime import date, datetime
|
6 |
| -from typing_extensions import ClassVar, Protocol, final, runtime_checkable |
| 6 | +from typing_extensions import Literal, ClassVar, Protocol, final, runtime_checkable |
7 | 7 |
|
8 | 8 | import pydantic
|
9 | 9 | import pydantic.generics
|
|
12 | 12 |
|
13 | 13 | from ._types import (
|
14 | 14 | Body,
|
| 15 | + IncEx, |
15 | 16 | Query,
|
16 | 17 | ModelT,
|
17 | 18 | Headers,
|
@@ -124,6 +125,105 @@ def construct(
|
124 | 125 | # although not in practice
|
125 | 126 | model_construct = construct
|
126 | 127 |
|
| 128 | + if not PYDANTIC_V2: |
| 129 | + # we define aliases for some of the new pydantic v2 methods so |
| 130 | + # that we can just document these methods without having to specify |
| 131 | + # a specifc pydantic version as some users may not know which |
| 132 | + # pydantic version they are currently using |
| 133 | + |
| 134 | + def model_dump( |
| 135 | + self, |
| 136 | + *, |
| 137 | + mode: Literal["json", "python"] | str = "python", |
| 138 | + include: IncEx = None, |
| 139 | + exclude: IncEx = None, |
| 140 | + by_alias: bool = False, |
| 141 | + exclude_unset: bool = False, |
| 142 | + exclude_defaults: bool = False, |
| 143 | + exclude_none: bool = False, |
| 144 | + round_trip: bool = False, |
| 145 | + warnings: bool = True, |
| 146 | + ) -> dict[str, Any]: |
| 147 | + """Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump |
| 148 | +
|
| 149 | + Generate a dictionary representation of the model, optionally specifying which fields to include or exclude. |
| 150 | +
|
| 151 | + Args: |
| 152 | + mode: The mode in which `to_python` should run. |
| 153 | + If mode is 'json', the dictionary will only contain JSON serializable types. |
| 154 | + If mode is 'python', the dictionary may contain any Python objects. |
| 155 | + include: A list of fields to include in the output. |
| 156 | + exclude: A list of fields to exclude from the output. |
| 157 | + by_alias: Whether to use the field's alias in the dictionary key if defined. |
| 158 | + exclude_unset: Whether to exclude fields that are unset or None from the output. |
| 159 | + exclude_defaults: Whether to exclude fields that are set to their default value from the output. |
| 160 | + exclude_none: Whether to exclude fields that have a value of `None` from the output. |
| 161 | + round_trip: Whether to enable serialization and deserialization round-trip support. |
| 162 | + warnings: Whether to log warnings when invalid fields are encountered. |
| 163 | +
|
| 164 | + Returns: |
| 165 | + A dictionary representation of the model. |
| 166 | + """ |
| 167 | + if mode != "python": |
| 168 | + raise ValueError("mode is only supported in Pydantic v2") |
| 169 | + if round_trip != False: |
| 170 | + raise ValueError("round_trip is only supported in Pydantic v2") |
| 171 | + if warnings != True: |
| 172 | + raise ValueError("warnings is only supported in Pydantic v2") |
| 173 | + return super().dict( # pyright: ignore[reportDeprecated] |
| 174 | + include=include, |
| 175 | + exclude=exclude, |
| 176 | + by_alias=by_alias, |
| 177 | + exclude_unset=exclude_unset, |
| 178 | + exclude_defaults=exclude_defaults, |
| 179 | + exclude_none=exclude_none, |
| 180 | + ) |
| 181 | + |
| 182 | + def model_dump_json( |
| 183 | + self, |
| 184 | + *, |
| 185 | + indent: int | None = None, |
| 186 | + include: IncEx = None, |
| 187 | + exclude: IncEx = None, |
| 188 | + by_alias: bool = False, |
| 189 | + exclude_unset: bool = False, |
| 190 | + exclude_defaults: bool = False, |
| 191 | + exclude_none: bool = False, |
| 192 | + round_trip: bool = False, |
| 193 | + warnings: bool = True, |
| 194 | + ) -> str: |
| 195 | + """Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump_json |
| 196 | +
|
| 197 | + Generates a JSON representation of the model using Pydantic's `to_json` method. |
| 198 | +
|
| 199 | + Args: |
| 200 | + indent: Indentation to use in the JSON output. If None is passed, the output will be compact. |
| 201 | + include: Field(s) to include in the JSON output. Can take either a string or set of strings. |
| 202 | + exclude: Field(s) to exclude from the JSON output. Can take either a string or set of strings. |
| 203 | + by_alias: Whether to serialize using field aliases. |
| 204 | + exclude_unset: Whether to exclude fields that have not been explicitly set. |
| 205 | + exclude_defaults: Whether to exclude fields that have the default value. |
| 206 | + exclude_none: Whether to exclude fields that have a value of `None`. |
| 207 | + round_trip: Whether to use serialization/deserialization between JSON and class instance. |
| 208 | + warnings: Whether to show any warnings that occurred during serialization. |
| 209 | +
|
| 210 | + Returns: |
| 211 | + A JSON string representation of the model. |
| 212 | + """ |
| 213 | + if round_trip != False: |
| 214 | + raise ValueError("round_trip is only supported in Pydantic v2") |
| 215 | + if warnings != True: |
| 216 | + raise ValueError("warnings is only supported in Pydantic v2") |
| 217 | + return super().json( # type: ignore[reportDeprecated] |
| 218 | + indent=indent, |
| 219 | + include=include, |
| 220 | + exclude=exclude, |
| 221 | + by_alias=by_alias, |
| 222 | + exclude_unset=exclude_unset, |
| 223 | + exclude_defaults=exclude_defaults, |
| 224 | + exclude_none=exclude_none, |
| 225 | + ) |
| 226 | + |
127 | 227 |
|
128 | 228 | def _construct_field(value: object, field: FieldInfo, key: str) -> object:
|
129 | 229 | if value is None:
|
|
0 commit comments