Skip to content

rename hide_input_in_errors -> hide_input on ValidationError #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pydantic_core/_pydantic_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class ValidationError(ValueError):
title: str,
errors: 'list[InitErrorDetails]',
error_mode: Literal['python', 'json'] = 'python',
hide_input_in_errors: bool = False,
hide_input: bool = False,
) -> ValidationError:
"""
Provisory constructor for a Validation Error.
Expand Down
41 changes: 15 additions & 26 deletions src/errors/validation_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,16 @@ pub struct ValidationError {
line_errors: Vec<PyLineError>,
error_mode: ErrorMode,
title: PyObject,
hide_input_in_errors: bool,
hide_input: bool,
}

impl ValidationError {
pub fn new(
line_errors: Vec<PyLineError>,
title: PyObject,
error_mode: ErrorMode,
hide_input_in_errors: bool,
) -> Self {
pub fn new(line_errors: Vec<PyLineError>, title: PyObject, error_mode: ErrorMode, hide_input: bool) -> Self {
Self {
line_errors,
title,
error_mode,
hide_input_in_errors,
hide_input,
}
}

Expand All @@ -56,7 +51,7 @@ impl ValidationError {
error_mode: ErrorMode,
error: ValError,
outer_location: Option<LocItem>,
hide_input_in_errors: bool,
hide_input: bool,
) -> PyErr {
match error {
ValError::LineErrors(raw_errors) => {
Expand All @@ -67,7 +62,7 @@ impl ValidationError {
.collect(),
None => raw_errors.into_iter().map(|e| e.into_py(py)).collect(),
};
let validation_error = Self::new(line_errors, title, error_mode, hide_input_in_errors);
let validation_error = Self::new(line_errors, title, error_mode, hide_input);
match Py::new(py, validation_error) {
Ok(err) => PyErr::from_value(err.into_ref(py)),
Err(err) => err,
Expand All @@ -78,15 +73,9 @@ impl ValidationError {
}
}

pub fn display(&self, py: Python, prefix_override: Option<&'static str>, hide_input_in_errors: bool) -> String {
pub fn display(&self, py: Python, prefix_override: Option<&'static str>, hide_input: bool) -> String {
let url_prefix = get_url_prefix(py, include_url_env(py));
let line_errors = pretty_py_line_errors(
py,
&self.error_mode,
self.line_errors.iter(),
url_prefix,
hide_input_in_errors,
);
let line_errors = pretty_py_line_errors(py, &self.error_mode, self.line_errors.iter(), url_prefix, hide_input);
if let Some(prefix) = prefix_override {
format!("{prefix}\n{line_errors}")
} else {
Expand Down Expand Up @@ -139,21 +128,21 @@ impl<'a> IntoPy<ValError<'a>> for ValidationError {
#[pymethods]
impl ValidationError {
#[staticmethod]
#[pyo3(signature = (title, line_errors, error_mode=None, hide_input_in_errors=false))]
#[pyo3(signature = (title, line_errors, error_mode=None, hide_input=false))]
fn from_exception_data(
py: Python,
title: PyObject,
line_errors: &PyList,
error_mode: Option<&str>,
hide_input_in_errors: bool,
hide_input: bool,
) -> PyResult<Py<Self>> {
Py::new(
py,
Self {
line_errors: line_errors.iter().map(PyLineError::try_from).collect::<PyResult<_>>()?,
title,
error_mode: ErrorMode::try_from(error_mode)?,
hide_input_in_errors,
hide_input,
},
)
}
Expand Down Expand Up @@ -228,7 +217,7 @@ impl ValidationError {
}

fn __repr__(&self, py: Python) -> String {
self.display(py, None, self.hide_input_in_errors)
self.display(py, None, self.hide_input)
}

fn __str__(&self, py: Python) -> String {
Expand Down Expand Up @@ -256,10 +245,10 @@ pub fn pretty_py_line_errors<'a>(
error_mode: &ErrorMode,
line_errors_iter: impl Iterator<Item = &'a PyLineError>,
url_prefix: Option<&str>,
hide_input_in_errors: bool,
hide_input: bool,
) -> String {
line_errors_iter
.map(|i| i.pretty(py, error_mode, url_prefix, hide_input_in_errors))
.map(|i| i.pretty(py, error_mode, url_prefix, hide_input))
.collect::<Result<Vec<_>, _>>()
.unwrap_or_else(|err| vec![format!("[error formatting line errors: {err}]")])
.join("\n")
Expand Down Expand Up @@ -373,7 +362,7 @@ impl PyLineError {
py: Python,
error_mode: &ErrorMode,
url_prefix: Option<&str>,
hide_input_in_errors: bool,
hide_input: bool,
) -> Result<String, fmt::Error> {
let mut output = String::with_capacity(200);
write!(output, "{}", self.location)?;
Expand All @@ -384,7 +373,7 @@ impl PyLineError {
};
write!(output, " {message} [type={}", self.error_type.type_string())?;

if !hide_input_in_errors {
if !hide_input {
let input_value = self.input_value.as_ref(py);
let input_str = safe_repr(input_value);
truncate_input_value!(output, input_str);
Expand Down
2 changes: 1 addition & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def test_raise_validation_error_hide_input(hide_input_in_errors, input_str):
raise ValidationError.from_exception_data(
'Foobar',
[{'type': 'greater_than', 'loc': ('a', 2), 'input': 4, 'ctx': {'gt': 5}}],
hide_input_in_errors=hide_input_in_errors,
hide_input=hide_input_in_errors,
)


Expand Down