Skip to content

Commit f977516

Browse files
authored
Revert by_alias default value change for to_json() and to_jsonable_python() (#1672)
1 parent f256531 commit f977516

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

python/pydantic_core/_pydantic_core.pyi

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,10 @@ def to_json(
392392
indent: int | None = None,
393393
include: _IncEx | None = None,
394394
exclude: _IncEx | None = None,
395-
by_alias: bool | None = None,
395+
# Note: In Pydantic 2.11, the default value of `by_alias` on `SchemaSerializer` was changed from `True` to `None`,
396+
# to be consistent with the Pydantic "dump" methods. However, the default of `True` was kept here for
397+
# backwards compatibility. In Pydantic V3, `by_alias` is expected to default to `True` everywhere:
398+
by_alias: bool = True,
396399
exclude_none: bool = False,
397400
round_trip: bool = False,
398401
timedelta_mode: Literal['iso8601', 'float'] = 'iso8601',
@@ -468,7 +471,10 @@ def to_jsonable_python(
468471
*,
469472
include: _IncEx | None = None,
470473
exclude: _IncEx | None = None,
471-
by_alias: bool | None = None,
474+
# Note: In Pydantic 2.11, the default value of `by_alias` on `SchemaSerializer` was changed from `True` to `None`,
475+
# to be consistent with the Pydantic "dump" methods. However, the default of `True` was kept here for
476+
# backwards compatibility. In Pydantic V3, `by_alias` is expected to default to `True` everywhere:
477+
by_alias: bool = True,
472478
exclude_none: bool = False,
473479
round_trip: bool = False,
474480
timedelta_mode: Literal['iso8601', 'float'] = 'iso8601',

src/serializers/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl SchemaSerializer {
239239

240240
#[allow(clippy::too_many_arguments)]
241241
#[pyfunction]
242-
#[pyo3(signature = (value, *, indent = None, include = None, exclude = None, by_alias = None,
242+
#[pyo3(signature = (value, *, indent = None, include = None, exclude = None, by_alias = true,
243243
exclude_none = false, round_trip = false, timedelta_mode = "iso8601", bytes_mode = "utf8",
244244
inf_nan_mode = "constants", serialize_unknown = false, fallback = None, serialize_as_any = false,
245245
context = None))]
@@ -249,7 +249,7 @@ pub fn to_json(
249249
indent: Option<usize>,
250250
include: Option<&Bound<'_, PyAny>>,
251251
exclude: Option<&Bound<'_, PyAny>>,
252-
by_alias: Option<bool>,
252+
by_alias: bool,
253253
exclude_none: bool,
254254
round_trip: bool,
255255
timedelta_mode: &str,
@@ -265,7 +265,7 @@ pub fn to_json(
265265
let extra = state.extra(
266266
py,
267267
&SerMode::Json,
268-
by_alias,
268+
Some(by_alias),
269269
exclude_none,
270270
round_trip,
271271
serialize_unknown,
@@ -282,15 +282,15 @@ pub fn to_json(
282282

283283
#[allow(clippy::too_many_arguments)]
284284
#[pyfunction]
285-
#[pyo3(signature = (value, *, include = None, exclude = None, by_alias = None, exclude_none = false, round_trip = false,
285+
#[pyo3(signature = (value, *, include = None, exclude = None, by_alias = true, exclude_none = false, round_trip = false,
286286
timedelta_mode = "iso8601", bytes_mode = "utf8", inf_nan_mode = "constants", serialize_unknown = false, fallback = None,
287287
serialize_as_any = false, context = None))]
288288
pub fn to_jsonable_python(
289289
py: Python,
290290
value: &Bound<'_, PyAny>,
291291
include: Option<&Bound<'_, PyAny>>,
292292
exclude: Option<&Bound<'_, PyAny>>,
293-
by_alias: Option<bool>,
293+
by_alias: bool,
294294
exclude_none: bool,
295295
round_trip: bool,
296296
timedelta_mode: &str,
@@ -306,7 +306,7 @@ pub fn to_jsonable_python(
306306
let extra = state.extra(
307307
py,
308308
&SerMode::Json,
309-
by_alias,
309+
Some(by_alias),
310310
exclude_none,
311311
round_trip,
312312
serialize_unknown,

0 commit comments

Comments
 (0)