1
+ use std:: borrow:: Cow ;
1
2
use std:: fmt;
2
3
use std:: fmt:: { Display , Write } ;
3
4
use std:: str:: from_utf8;
@@ -72,15 +73,15 @@ impl ValidationError {
72
73
Err ( err) => return err,
73
74
} ;
74
75
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) {
76
77
Ok ( err) => {
77
78
if validation_error_cause {
78
79
// 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) {
80
81
return cause_problem;
81
82
}
82
83
}
83
- PyErr :: from_value ( err. into_bound ( py ) . into_any ( ) )
84
+ PyErr :: from_value ( err. into_any ( ) )
84
85
}
85
86
Err ( err) => err,
86
87
}
@@ -125,16 +126,13 @@ impl ValidationError {
125
126
} = & line_error. error_type
126
127
{
127
128
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" )
129
130
} 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
+ ) )
138
136
} ;
139
137
140
138
// Notes only support 3.11 upwards:
@@ -153,7 +151,7 @@ impl ValidationError {
153
151
{
154
152
use pyo3:: exceptions:: PyUserWarning ;
155
153
156
- let wrapped = PyUserWarning :: new_err ( ( note. unbind ( ) , ) ) ;
154
+ let wrapped = PyUserWarning :: new_err ( ( note, ) ) ;
157
155
wrapped. set_cause ( py, Some ( PyErr :: from_value ( err. clone_ref ( py) . into_bound ( py) ) ) ) ;
158
156
user_py_errs. push ( wrapped) ;
159
157
}
@@ -329,7 +327,7 @@ impl ValidationError {
329
327
if let Some ( err) = iteration_error {
330
328
Err ( err)
331
329
} else {
332
- Ok ( list. into ( ) )
330
+ Ok ( list. unbind ( ) )
333
331
}
334
332
}
335
333
@@ -396,12 +394,12 @@ impl ValidationError {
396
394
let callable = slf. getattr ( "from_exception_data" ) ?;
397
395
let borrow = slf. try_borrow ( ) ?;
398
396
let args = (
399
- borrow. title . bind ( py ) ,
397
+ & borrow. title ,
400
398
borrow. errors ( py, include_url_env ( py) , true , true ) ?,
401
- borrow. input_type . into_pyobject ( py ) ? ,
399
+ borrow. input_type ,
402
400
borrow. hide_input ,
403
401
)
404
- . into_pyobject ( slf . py ( ) ) ?;
402
+ . into_pyobject ( py ) ?;
405
403
Ok ( ( callable, args) )
406
404
}
407
405
}
@@ -501,7 +499,7 @@ impl PyLineError {
501
499
) -> PyResult < Bound < ' py , PyDict > > {
502
500
let dict = PyDict :: new ( py) ;
503
501
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 ) ?;
505
503
dict. set_item ( "msg" , self . error_type . render_message ( py, input_type) ?) ?;
506
504
if include_input {
507
505
dict. set_item ( "input" , & self . input_value ) ?;
0 commit comments