Skip to content

Commit 8dca91d

Browse files
use pyo3-signature for serializer defautls (#534)
Co-authored-by: David Montague <[email protected]>
1 parent c6b3f1c commit 8dca91d

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

src/errors/validation_exception.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl ValidationError {
141141
include_context: Option<bool>,
142142
) -> PyResult<&'py PyString> {
143143
let state = SerializationState::new(None, None);
144-
let extra = state.extra(py, &SerMode::Json, None, None, None, Some(true), None);
144+
let extra = state.extra(py, &SerMode::Json, true, false, false, true, None);
145145
let serializer = ValidationErrorSerializer {
146146
py,
147147
line_errors: &self.line_errors,

src/serializers/extra.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) struct SerializationState {
2323

2424
impl SerializationState {
2525
pub fn new(timedelta_mode: Option<&str>, bytes_mode: Option<&str>) -> Self {
26-
let warnings = CollectWarnings::new(None);
26+
let warnings = CollectWarnings::new(false);
2727
let rec_guard = SerRecursionGuard::default();
2828
let config = SerializationConfig::from_args(timedelta_mode, bytes_mode).unwrap();
2929
Self {
@@ -38,10 +38,10 @@ impl SerializationState {
3838
&'py self,
3939
py: Python<'py>,
4040
mode: &'py SerMode,
41-
by_alias: Option<bool>,
42-
exclude_none: Option<bool>,
43-
round_trip: Option<bool>,
44-
serialize_unknown: Option<bool>,
41+
by_alias: bool,
42+
exclude_none: bool,
43+
round_trip: bool,
44+
serialize_unknown: bool,
4545
fallback: Option<&'py PyAny>,
4646
) -> Extra<'py> {
4747
Extra::new(
@@ -50,8 +50,8 @@ impl SerializationState {
5050
&[],
5151
by_alias,
5252
&self.warnings,
53-
None,
54-
None,
53+
false,
54+
false,
5555
exclude_none,
5656
round_trip,
5757
&self.config,
@@ -98,33 +98,33 @@ impl<'a> Extra<'a> {
9898
py: Python<'a>,
9999
mode: &'a SerMode,
100100
slots: &'a [CombinedSerializer],
101-
by_alias: Option<bool>,
101+
by_alias: bool,
102102
warnings: &'a CollectWarnings,
103-
exclude_unset: Option<bool>,
104-
exclude_defaults: Option<bool>,
105-
exclude_none: Option<bool>,
106-
round_trip: Option<bool>,
103+
exclude_unset: bool,
104+
exclude_defaults: bool,
105+
exclude_none: bool,
106+
round_trip: bool,
107107
config: &'a SerializationConfig,
108108
rec_guard: &'a SerRecursionGuard,
109-
serialize_unknown: Option<bool>,
109+
serialize_unknown: bool,
110110
fallback: Option<&'a PyAny>,
111111
) -> Self {
112112
Self {
113113
mode,
114114
slots,
115115
ob_type_lookup: ObTypeLookup::cached(py),
116116
warnings,
117-
by_alias: by_alias.unwrap_or(true),
118-
exclude_unset: exclude_unset.unwrap_or(false),
119-
exclude_defaults: exclude_defaults.unwrap_or(false),
120-
exclude_none: exclude_none.unwrap_or(false),
121-
round_trip: round_trip.unwrap_or(false),
117+
by_alias,
118+
exclude_unset,
119+
exclude_defaults,
120+
exclude_none,
121+
round_trip,
122122
config,
123123
rec_guard,
124124
check: SerCheck::None,
125125
model: None,
126126
field_name: None,
127-
serialize_unknown: serialize_unknown.unwrap_or(false),
127+
serialize_unknown,
128128
fallback,
129129
}
130130
}
@@ -267,9 +267,9 @@ pub(crate) struct CollectWarnings {
267267
}
268268

269269
impl CollectWarnings {
270-
pub(crate) fn new(active: Option<bool>) -> Self {
270+
pub(crate) fn new(active: bool) -> Self {
271271
Self {
272-
active: active.unwrap_or(true),
272+
active,
273273
warnings: RefCell::new(None),
274274
}
275275
}

src/serializers/mod.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl SchemaSerializer {
5050
}
5151

5252
#[allow(clippy::too_many_arguments)]
53-
#[pyo3(signature = (value, *, mode = None, include = None, exclude = None, by_alias = None,
53+
#[pyo3(signature = (value, *, mode = None, include = None, exclude = None, by_alias = true,
5454
exclude_unset = false, exclude_defaults = false, exclude_none = false, round_trip = false, warnings = true,
5555
fallback = None))]
5656
pub fn to_python(
@@ -60,12 +60,12 @@ impl SchemaSerializer {
6060
mode: Option<&str>,
6161
include: Option<&PyAny>,
6262
exclude: Option<&PyAny>,
63-
by_alias: Option<bool>,
64-
exclude_unset: Option<bool>,
65-
exclude_defaults: Option<bool>,
66-
exclude_none: Option<bool>,
67-
round_trip: Option<bool>,
68-
warnings: Option<bool>,
63+
by_alias: bool,
64+
exclude_unset: bool,
65+
exclude_defaults: bool,
66+
exclude_none: bool,
67+
round_trip: bool,
68+
warnings: bool,
6969
fallback: Option<&PyAny>,
7070
) -> PyResult<PyObject> {
7171
let mode: SerMode = mode.into();
@@ -83,7 +83,7 @@ impl SchemaSerializer {
8383
round_trip,
8484
&self.config,
8585
&rec_guard,
86-
None,
86+
false,
8787
fallback,
8888
);
8989
let v = self.serializer.to_python(value, include, exclude, &extra)?;
@@ -92,7 +92,7 @@ impl SchemaSerializer {
9292
}
9393

9494
#[allow(clippy::too_many_arguments)]
95-
#[pyo3(signature = (value, *, indent = None, include = None, exclude = None, by_alias = None,
95+
#[pyo3(signature = (value, *, indent = None, include = None, exclude = None, by_alias = true,
9696
exclude_unset = false, exclude_defaults = false, exclude_none = false, round_trip = false, warnings = true,
9797
fallback = None))]
9898
pub fn to_json(
@@ -102,12 +102,12 @@ impl SchemaSerializer {
102102
indent: Option<usize>,
103103
include: Option<&PyAny>,
104104
exclude: Option<&PyAny>,
105-
by_alias: Option<bool>,
106-
exclude_unset: Option<bool>,
107-
exclude_defaults: Option<bool>,
108-
exclude_none: Option<bool>,
109-
round_trip: Option<bool>,
110-
warnings: Option<bool>,
105+
by_alias: bool,
106+
exclude_unset: bool,
107+
exclude_defaults: bool,
108+
exclude_none: bool,
109+
round_trip: bool,
110+
warnings: bool,
111111
fallback: Option<&PyAny>,
112112
) -> PyResult<PyObject> {
113113
let warnings = CollectWarnings::new(warnings);
@@ -124,7 +124,7 @@ impl SchemaSerializer {
124124
round_trip,
125125
&self.config,
126126
&rec_guard,
127-
None,
127+
false,
128128
fallback,
129129
);
130130
let bytes = to_json_bytes(
@@ -177,18 +177,18 @@ pub fn to_json(
177177
indent: Option<usize>,
178178
include: Option<&PyAny>,
179179
exclude: Option<&PyAny>,
180-
exclude_none: Option<bool>,
181-
round_trip: Option<bool>,
180+
exclude_none: bool,
181+
round_trip: bool,
182182
timedelta_mode: Option<&str>,
183183
bytes_mode: Option<&str>,
184-
serialize_unknown: Option<bool>,
184+
serialize_unknown: bool,
185185
fallback: Option<&PyAny>,
186186
) -> PyResult<PyObject> {
187187
let state = SerializationState::new(timedelta_mode, bytes_mode);
188188
let extra = state.extra(
189189
py,
190190
&SerMode::Json,
191-
None,
191+
true,
192192
exclude_none,
193193
round_trip,
194194
serialize_unknown,
@@ -203,19 +203,19 @@ pub fn to_json(
203203

204204
#[allow(clippy::too_many_arguments)]
205205
#[pyfunction]
206-
#[pyo3(signature = (value, *, include = None, exclude = None, by_alias = None, exclude_none = false, round_trip = false,
206+
#[pyo3(signature = (value, *, include = None, exclude = None, by_alias = true, exclude_none = false, round_trip = false,
207207
timedelta_mode = None, bytes_mode = None, serialize_unknown = false, fallback = None))]
208208
pub fn to_jsonable_python(
209209
py: Python,
210210
value: &PyAny,
211211
include: Option<&PyAny>,
212212
exclude: Option<&PyAny>,
213-
by_alias: Option<bool>,
214-
exclude_none: Option<bool>,
215-
round_trip: Option<bool>,
213+
by_alias: bool,
214+
exclude_none: bool,
215+
round_trip: bool,
216216
timedelta_mode: Option<&str>,
217217
bytes_mode: Option<&str>,
218-
serialize_unknown: Option<bool>,
218+
serialize_unknown: bool,
219219
fallback: Option<&PyAny>,
220220
) -> PyResult<PyObject> {
221221
let state = SerializationState::new(timedelta_mode, bytes_mode);

0 commit comments

Comments
 (0)