Skip to content

Commit 294aed0

Browse files
authored
derive debug only in dev mode (#204)
* derive debug only in dev mode * one more...
1 parent c78cc1c commit 294aed0

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

src/errors/kinds.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use super::PydanticValueError;
1010
/// * the variables in the message need to match the enum struct
1111
/// * you need to add an entry to the `render` enum to render the error message as a template
1212
/// * you need to add an entry to the `py_dict` enum to generate `ctx` for error messages
13-
#[derive(Debug, Display, EnumMessage, Clone)]
13+
#[derive(Display, EnumMessage, Clone)]
14+
#[cfg_attr(debug_assertions, derive(Debug))]
1415
#[strum(serialize_all = "snake_case")]
1516
pub enum ErrorKind {
1617
#[strum(message = "Invalid input")]

src/errors/line_error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::validation_exception::{pretty_py_line_errors, PyLineError};
1010

1111
pub type ValResult<'a, T> = Result<T, ValError<'a>>;
1212

13-
#[derive(Debug)]
13+
#[cfg_attr(debug_assertions, derive(Debug))]
1414
pub enum ValError<'a> {
1515
LineErrors(Vec<ValLineError<'a>>),
1616
InternalErr(PyErr),
@@ -65,7 +65,7 @@ pub fn pretty_line_errors(py: Python, line_errors: Vec<ValLineError>) -> String
6565
/// A `ValLineError` is a single error that occurred during validation which is converted to a `PyLineError`
6666
/// to eventually form a `ValidationError`.
6767
/// I don't like the name `ValLineError`, but it's the best I could come up with (for now).
68-
#[derive(Debug)]
68+
#[cfg_attr(debug_assertions, derive(Debug))]
6969
pub struct ValLineError<'a> {
7070
pub kind: ErrorKind,
7171
// location is reversed so that adding an "outer" location item is pushing, it's reversed before showing to the user
@@ -112,7 +112,7 @@ impl<'a> ValLineError<'a> {
112112
}
113113
}
114114

115-
#[derive(Debug)]
115+
#[cfg_attr(debug_assertions, derive(Debug))]
116116
pub enum InputValue<'a> {
117117
PyAny(&'a PyAny),
118118
JsonInput(&'a JsonInput),

src/errors/location.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use pyo3::types::PyList;
55

66
/// Used to store individual items of the error location, e.g. a string for key/field names
77
/// or a number for array indices.
8-
#[derive(Debug, Clone)]
8+
#[derive(Clone)]
9+
#[cfg_attr(debug_assertions, derive(Debug))]
910
pub enum LocItem {
1011
/// string type key, used to identify items from a dict or anything that implements `__getitem__`
1112
S(String),
@@ -55,7 +56,8 @@ impl ToPyObject for LocItem {
5556
/// Note: location in List is stored in **REVERSE** so adding an "outer" item to location involves
5657
/// pushing to the vec which is faster than inserting and shifting everything along.
5758
/// Then when "using" location in `Display` and `ToPyObject` order has to be reversed
58-
#[derive(Debug, Clone)]
59+
#[derive(Clone)]
60+
#[cfg_attr(debug_assertions, derive(Debug))]
5961
pub enum Location {
6062
// no location, avoid creating an unnecessary vec
6163
Empty,

src/errors/validation_exception.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use super::location::Location;
1313
use super::ValError;
1414

1515
#[pyclass(extends=PyValueError, module="pydantic_core._pydantic_core")]
16-
#[derive(Debug, Clone)]
16+
#[derive(Clone)]
17+
#[cfg_attr(debug_assertions, derive(Debug))]
1718
pub struct ValidationError {
1819
line_errors: Vec<PyLineError>,
1920
title: PyObject,
@@ -110,7 +111,8 @@ pub fn pretty_py_line_errors<'a>(py: Python, line_errors_iter: impl Iterator<Ite
110111

111112
/// `PyLineError` are the public version of `ValLineError`, as help and used in `ValidationError`s
112113
#[pyclass]
113-
#[derive(Debug, Clone)]
114+
#[derive(Clone)]
115+
#[cfg_attr(debug_assertions, derive(Debug))]
114116
pub struct PyLineError {
115117
kind: ErrorKind,
116118
location: Location,

src/errors/value_exception.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::input::Input;
77
use super::{ErrorKind, ValError};
88

99
#[pyclass(extends=PyValueError, module="pydantic_core._pydantic_core")]
10-
#[derive(Debug, Clone)]
10+
#[derive(Clone)]
11+
#[cfg_attr(debug_assertions, derive(Debug))]
1112
pub struct PydanticValueError {
1213
kind: String,
1314
message_template: String,

src/input/datetime.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::errors::{ErrorKind, ValError, ValResult};
88

99
use super::Input;
1010

11+
#[cfg_attr(debug_assertions, derive(Debug))]
1112
pub enum EitherDate<'a> {
1213
Raw(Date),
1314
Py(&'a PyDate),
@@ -53,6 +54,7 @@ impl<'a> EitherDate<'a> {
5354
}
5455
}
5556

57+
#[cfg_attr(debug_assertions, derive(Debug))]
5658
pub enum EitherTime<'a> {
5759
Raw(Time),
5860
Py(&'a PyTime),
@@ -70,7 +72,7 @@ impl<'a> From<&'a PyTime> for EitherTime<'a> {
7072
}
7173
}
7274

73-
#[derive(Debug, Clone)]
75+
#[cfg_attr(debug_assertions, derive(Debug))]
7476
pub enum EitherTimedelta<'a> {
7577
Raw(Duration),
7678
Py(&'a PyDelta),
@@ -168,6 +170,7 @@ impl<'a> EitherTime<'a> {
168170
}
169171
}
170172

173+
#[cfg_attr(debug_assertions, derive(Debug))]
171174
pub enum EitherDateTime<'a> {
172175
Raw(DateTime),
173176
Py(&'a PyDateTime),
@@ -386,7 +389,8 @@ pub fn float_as_duration(total_seconds: f64) -> Duration {
386389
}
387390

388391
#[pyclass(module = "pydantic_core._pydantic_core", extends = PyTzInfo)]
389-
#[derive(Debug, Clone)]
392+
#[derive(Clone)]
393+
#[cfg_attr(debug_assertions, derive(Debug))]
390394
struct TzInfo {
391395
seconds: i32,
392396
}

src/input/return_enums.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::Input;
1313
/// Container for all the "list-like" types which can be converted to each other in lax mode.
1414
/// This cannot be called `GenericSequence` (as it previously was) or `GenericIterable` since it's
1515
/// members don't match python's definition of `Sequence` or `Iterable`.
16-
#[derive(Debug)]
16+
#[cfg_attr(debug_assertions, derive(Debug))]
1717
pub enum GenericListLike<'a> {
1818
List(&'a PyList),
1919
Tuple(&'a PyTuple),
@@ -165,7 +165,7 @@ impl<'a> GenericListLike<'a> {
165165
}
166166
}
167167

168-
#[derive(Debug)]
168+
#[cfg_attr(debug_assertions, derive(Debug))]
169169
pub enum GenericMapping<'a> {
170170
PyDict(&'a PyDict),
171171
PyGetAttr(&'a PyAny),
@@ -176,7 +176,7 @@ derive_from!(GenericMapping, PyDict, PyDict);
176176
derive_from!(GenericMapping, PyGetAttr, PyAny);
177177
derive_from!(GenericMapping, JsonObject, JsonObject);
178178

179-
#[derive(Debug)]
179+
#[cfg_attr(debug_assertions, derive(Debug))]
180180
pub struct PyArgs<'a> {
181181
pub args: Option<&'a PyTuple>,
182182
pub kwargs: Option<&'a PyDict>,
@@ -188,7 +188,7 @@ impl<'a> PyArgs<'a> {
188188
}
189189
}
190190

191-
#[derive(Debug)]
191+
#[cfg_attr(debug_assertions, derive(Debug))]
192192
pub struct JsonArgs<'a> {
193193
pub args: Option<&'a [JsonInput]>,
194194
pub kwargs: Option<&'a JsonObject>,
@@ -200,7 +200,7 @@ impl<'a> JsonArgs<'a> {
200200
}
201201
}
202202

203-
#[derive(Debug)]
203+
#[cfg_attr(debug_assertions, derive(Debug))]
204204
pub enum GenericArguments<'a> {
205205
Py(PyArgs<'a>),
206206
Json(JsonArgs<'a>),
@@ -218,7 +218,7 @@ impl<'a> From<JsonArgs<'a>> for GenericArguments<'a> {
218218
}
219219
}
220220

221-
#[derive(Debug)]
221+
#[cfg_attr(debug_assertions, derive(Debug))]
222222
pub enum EitherString<'a> {
223223
Cow(Cow<'a, str>),
224224
Py(&'a PyString),
@@ -264,7 +264,7 @@ impl<'a> IntoPy<PyObject> for EitherString<'a> {
264264
}
265265
}
266266

267-
#[derive(Debug)]
267+
#[cfg_attr(debug_assertions, derive(Debug))]
268268
pub enum EitherBytes<'a> {
269269
Cow(Cow<'a, [u8]>),
270270
Py(&'a PyBytes),

0 commit comments

Comments
 (0)