22
33from typing import TYPE_CHECKING , Any , Union , Generic , TypeVar , Callable , cast , overload
44from datetime import date , datetime
5- from typing_extensions import Self , Literal
5+ from typing_extensions import Self , Literal , TypedDict
66
77import pydantic
88from pydantic .fields import FieldInfo
@@ -131,6 +131,10 @@ def model_json(model: pydantic.BaseModel, *, indent: int | None = None) -> str:
131131 return model .model_dump_json (indent = indent )
132132
133133
134+ class _ModelDumpKwargs (TypedDict , total = False ):
135+ by_alias : bool
136+
137+
134138def model_dump (
135139 model : pydantic .BaseModel ,
136140 * ,
@@ -142,14 +146,17 @@ def model_dump(
142146 by_alias : bool | None = None ,
143147) -> dict [str , Any ]:
144148 if (not PYDANTIC_V1 ) or hasattr (model , "model_dump" ):
149+ kwargs : _ModelDumpKwargs = {}
150+ if by_alias is not None :
151+ kwargs ["by_alias" ] = by_alias
145152 return model .model_dump (
146153 mode = mode ,
147154 exclude = exclude ,
148155 exclude_unset = exclude_unset ,
149156 exclude_defaults = exclude_defaults ,
150157 # warnings are not supported in Pydantic v1
151158 warnings = True if PYDANTIC_V1 else warnings ,
152- by_alias = by_alias ,
159+ ** kwargs ,
153160 )
154161 return cast (
155162 "dict[str, Any]" ,
0 commit comments