@@ -109,7 +109,9 @@ impl<'py> IntoPyObject<'py> for &Duration {
109109}
110110
111111impl FromPyObject < ' _ , ' _ > for Duration {
112- fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> PyResult < Duration > {
112+ type Error = PyErr ;
113+
114+ fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
113115 let delta = ob. cast :: < PyDelta > ( ) ?;
114116 // Python size are much lower than rust size so we do not need bound checks.
115117 // 0 <= microseconds < 1000000
@@ -163,7 +165,9 @@ impl<'py> IntoPyObject<'py> for &NaiveDate {
163165}
164166
165167impl FromPyObject < ' _ , ' _ > for NaiveDate {
166- fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> PyResult < NaiveDate > {
168+ type Error = PyErr ;
169+
170+ fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
167171 let date = & * ob. cast :: < PyDate > ( ) ?;
168172 py_date_to_naive_date ( date)
169173 }
@@ -205,7 +209,9 @@ impl<'py> IntoPyObject<'py> for &NaiveTime {
205209}
206210
207211impl FromPyObject < ' _ , ' _ > for NaiveTime {
208- fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> PyResult < NaiveTime > {
212+ type Error = PyErr ;
213+
214+ fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
209215 let time = & * ob. cast :: < PyTime > ( ) ?;
210216 py_time_to_naive_time ( time)
211217 }
@@ -248,7 +254,9 @@ impl<'py> IntoPyObject<'py> for &NaiveDateTime {
248254}
249255
250256impl FromPyObject < ' _ , ' _ > for NaiveDateTime {
251- fn extract ( dt : Borrowed < ' _ , ' _ , PyAny > ) -> PyResult < NaiveDateTime > {
257+ type Error = PyErr ;
258+
259+ fn extract ( dt : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
252260 let dt = & * dt. cast :: < PyDateTime > ( ) ?;
253261
254262 // If the user tries to convert a timezone aware datetime into a naive one,
@@ -328,12 +336,14 @@ impl<'py, Tz> FromPyObject<'_, 'py> for DateTime<Tz>
328336where
329337 Tz : TimeZone + FromPyObjectOwned < ' py > ,
330338{
331- fn extract ( dt : Borrowed < ' _ , ' py , PyAny > ) -> PyResult < DateTime < Tz > > {
339+ type Error = PyErr ;
340+
341+ fn extract ( dt : Borrowed < ' _ , ' py , PyAny > ) -> Result < Self , Self :: Error > {
332342 let dt = & * dt. cast :: < PyDateTime > ( ) ?;
333343 let tzinfo = dt. get_tzinfo ( ) ;
334344
335345 let tz = if let Some ( tzinfo) = tzinfo {
336- tzinfo. extract ( ) ?
346+ tzinfo. extract ( ) . map_err ( Into :: into ) ?
337347 } else {
338348 return Err ( PyTypeError :: new_err (
339349 "expected a datetime with non-None tzinfo" ,
@@ -386,11 +396,13 @@ impl<'py> IntoPyObject<'py> for &FixedOffset {
386396}
387397
388398impl FromPyObject < ' _ , ' _ > for FixedOffset {
399+ type Error = PyErr ;
400+
389401 /// Convert python tzinfo to rust [`FixedOffset`].
390402 ///
391403 /// Note that the conversion will result in precision lost in microseconds as chrono offset
392404 /// does not supports microseconds.
393- fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> PyResult < FixedOffset > {
405+ fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
394406 let ob = ob. cast :: < PyTzInfo > ( ) ?;
395407
396408 // Passing Python's None to the `utcoffset` function will only
@@ -435,7 +447,9 @@ impl<'py> IntoPyObject<'py> for &Utc {
435447}
436448
437449impl FromPyObject < ' _ , ' _ > for Utc {
438- fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> PyResult < Utc > {
450+ type Error = PyErr ;
451+
452+ fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
439453 let py_utc = PyTzInfo :: utc ( ob. py ( ) ) ?;
440454 if ob. eq ( py_utc) ? {
441455 Ok ( Utc )
@@ -479,6 +493,8 @@ impl<'py> IntoPyObject<'py> for &Local {
479493
480494#[ cfg( feature = "chrono-local" ) ]
481495impl FromPyObject < ' _ , ' _ > for Local {
496+ type Error = PyErr ;
497+
482498 fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> PyResult < Local > {
483499 let local_tz = Local . into_pyobject ( ob. py ( ) ) ?;
484500 if ob. eq ( local_tz) ? {
0 commit comments