1717
1818from typing import TYPE_CHECKING , Any , Union , Generic , TypeVar , Callable , cast , overload
1919from datetime import date , datetime
20- from typing_extensions import Self , Literal
20+ from typing_extensions import Self , Literal , TypedDict
2121
2222import pydantic
2323from pydantic .fields import FieldInfo
@@ -146,6 +146,10 @@ def model_json(model: pydantic.BaseModel, *, indent: int | None = None) -> str:
146146 return model .model_dump_json (indent = indent )
147147
148148
149+ class _ModelDumpKwargs (TypedDict , total = False ):
150+ by_alias : bool
151+
152+
149153def model_dump (
150154 model : pydantic .BaseModel ,
151155 * ,
@@ -157,14 +161,17 @@ def model_dump(
157161 by_alias : bool | None = None ,
158162) -> dict [str , Any ]:
159163 if (not PYDANTIC_V1 ) or hasattr (model , "model_dump" ):
164+ kwargs : _ModelDumpKwargs = {}
165+ if by_alias is not None :
166+ kwargs ["by_alias" ] = by_alias
160167 return model .model_dump (
161168 mode = mode ,
162169 exclude = exclude ,
163170 exclude_unset = exclude_unset ,
164171 exclude_defaults = exclude_defaults ,
165172 # warnings are not supported in Pydantic v1
166173 warnings = True if PYDANTIC_V1 else warnings ,
167- by_alias = by_alias ,
174+ ** kwargs ,
168175 )
169176 return cast (
170177 "dict[str, Any]" ,
0 commit comments