1+ use std:: borrow:: Cow ;
12use std:: fmt;
23use std:: fmt:: { Display , Write } ;
34use std:: str:: from_utf8;
@@ -72,15 +73,15 @@ impl ValidationError {
7273 Err ( err) => return err,
7374 } ;
7475 let validation_error = Self :: new ( line_errors, title, input_type, hide_input) ;
75- match Py :: new ( py, validation_error) {
76+ match Bound :: new ( py, validation_error) {
7677 Ok ( err) => {
7778 if validation_error_cause {
7879 // Will return an import error if the backport was needed and not installed:
79- if let Some ( cause_problem) = ValidationError :: maybe_add_cause ( err. borrow ( py ) , py) {
80+ if let Some ( cause_problem) = ValidationError :: maybe_add_cause ( err. borrow ( ) , py) {
8081 return cause_problem;
8182 }
8283 }
83- PyErr :: from_value ( err. into_bound ( py ) . into_any ( ) )
84+ PyErr :: from_value ( err. into_any ( ) )
8485 }
8586 Err ( err) => err,
8687 }
@@ -125,16 +126,13 @@ impl ValidationError {
125126 } = & line_error. error_type
126127 {
127128 let note = if let Location :: Empty = & line_error. location {
128- PyString :: new ( py , "Pydantic: cause of loc: root" )
129+ Cow :: Borrowed ( "Pydantic: cause of loc: root" )
129130 } else {
130- PyString :: new (
131- py,
132- & format ! (
133- "Pydantic: cause of loc: {}" ,
134- // Location formats with a newline at the end, hence the trim()
135- line_error. location. to_string( ) . trim( )
136- ) ,
137- )
131+ Cow :: Owned ( format ! (
132+ "Pydantic: cause of loc: {}" ,
133+ // Location formats with a newline at the end, hence the trim()
134+ line_error. location. to_string( ) . trim( )
135+ ) )
138136 } ;
139137
140138 // Notes only support 3.11 upwards:
@@ -153,7 +151,7 @@ impl ValidationError {
153151 {
154152 use pyo3:: exceptions:: PyUserWarning ;
155153
156- let wrapped = PyUserWarning :: new_err ( ( note. unbind ( ) , ) ) ;
154+ let wrapped = PyUserWarning :: new_err ( ( note, ) ) ;
157155 wrapped. set_cause ( py, Some ( PyErr :: from_value ( err. clone_ref ( py) . into_bound ( py) ) ) ) ;
158156 user_py_errs. push ( wrapped) ;
159157 }
@@ -329,7 +327,7 @@ impl ValidationError {
329327 if let Some ( err) = iteration_error {
330328 Err ( err)
331329 } else {
332- Ok ( list. into ( ) )
330+ Ok ( list. unbind ( ) )
333331 }
334332 }
335333
@@ -396,12 +394,12 @@ impl ValidationError {
396394 let callable = slf. getattr ( "from_exception_data" ) ?;
397395 let borrow = slf. try_borrow ( ) ?;
398396 let args = (
399- borrow. title . bind ( py ) ,
397+ & borrow. title ,
400398 borrow. errors ( py, include_url_env ( py) , true , true ) ?,
401- borrow. input_type . into_pyobject ( py ) ? ,
399+ borrow. input_type ,
402400 borrow. hide_input ,
403401 )
404- . into_pyobject ( slf . py ( ) ) ?;
402+ . into_pyobject ( py ) ?;
405403 Ok ( ( callable, args) )
406404 }
407405}
@@ -501,7 +499,7 @@ impl PyLineError {
501499 ) -> PyResult < Bound < ' py , PyDict > > {
502500 let dict = PyDict :: new ( py) ;
503501 dict. set_item ( "type" , self . error_type . type_string ( ) ) ?;
504- dict. set_item ( "loc" , self . location . to_object ( py ) ) ?;
502+ dict. set_item ( "loc" , & self . location ) ?;
505503 dict. set_item ( "msg" , self . error_type . render_message ( py, input_type) ?) ?;
506504 if include_input {
507505 dict. set_item ( "input" , & self . input_value ) ?;
0 commit comments