Skip to content

Use UserWarning from pyo3 #1559

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 2 commits into from
Dec 3, 2024
Merged
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
6 changes: 3 additions & 3 deletions src/serializers/extra.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::fmt;
use std::sync::Mutex;

use pyo3::exceptions::{PyTypeError, PyValueError};
use pyo3::intern;
use pyo3::exceptions::{PyTypeError, PyUserWarning, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyBool, PyString};
use pyo3::{intern, PyTypeInfo};

use serde::ser::Error;

Expand Down Expand Up @@ -471,7 +471,7 @@ impl CollectWarnings {

let message = format!("Pydantic serializer warnings:\n {}", warnings.join("\n "));
if self.mode == WarningsMode::Warn {
let user_warning_type = py.import_bound("builtins")?.getattr("UserWarning")?;
let user_warning_type = PyUserWarning::type_object(py);
PyErr::warn_bound(py, &user_warning_type, &message, 0)
} else {
Err(PydanticSerializationError::new_err(message))
Expand Down
Loading