4343
4444use crate :: conversion:: { FromPyObjectOwned , IntoPyObject } ;
4545use crate :: exceptions:: { PyTypeError , PyUserWarning , PyValueError } ;
46+ #[ cfg( feature = "experimental-inspect" ) ]
47+ use crate :: inspect:: TypeHint ;
4648use crate :: intern;
49+ #[ cfg( feature = "experimental-inspect" ) ]
50+ use crate :: type_object:: PyTypeInfo ;
4751use crate :: types:: any:: PyAnyMethods ;
4852use crate :: types:: PyNone ;
4953use crate :: types:: { PyDate , PyDateTime , PyDelta , PyTime , PyTzInfo , PyTzInfoAccess } ;
@@ -70,6 +74,9 @@ impl<'py> IntoPyObject<'py> for Duration {
7074 type Output = Bound < ' py , Self :: Target > ;
7175 type Error = PyErr ;
7276
77+ #[ cfg( feature = "experimental-inspect" ) ]
78+ const OUTPUT_TYPE : TypeHint = PyDelta :: TYPE_HINT ;
79+
7380 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
7481 // Total number of days
7582 let days = self . num_days ( ) ;
@@ -102,6 +109,9 @@ impl<'py> IntoPyObject<'py> for &Duration {
102109 type Output = Bound < ' py , Self :: Target > ;
103110 type Error = PyErr ;
104111
112+ #[ cfg( feature = "experimental-inspect" ) ]
113+ const OUTPUT_TYPE : TypeHint = Duration :: OUTPUT_TYPE ;
114+
105115 #[ inline]
106116 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
107117 ( * self ) . into_pyobject ( py)
@@ -111,6 +121,9 @@ impl<'py> IntoPyObject<'py> for &Duration {
111121impl FromPyObject < ' _ , ' _ > for Duration {
112122 type Error = PyErr ;
113123
124+ #[ cfg( feature = "experimental-inspect" ) ]
125+ const INPUT_TYPE : TypeHint = PyDelta :: TYPE_HINT ;
126+
114127 fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
115128 let delta = ob. cast :: < PyDelta > ( ) ?;
116129 // Python size are much lower than rust size so we do not need bound checks.
@@ -147,6 +160,9 @@ impl<'py> IntoPyObject<'py> for NaiveDate {
147160 type Output = Bound < ' py , Self :: Target > ;
148161 type Error = PyErr ;
149162
163+ #[ cfg( feature = "experimental-inspect" ) ]
164+ const OUTPUT_TYPE : TypeHint = PyDate :: TYPE_HINT ;
165+
150166 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
151167 let DateArgs { year, month, day } = ( & self ) . into ( ) ;
152168 PyDate :: new ( py, year, month, day)
@@ -158,6 +174,9 @@ impl<'py> IntoPyObject<'py> for &NaiveDate {
158174 type Output = Bound < ' py , Self :: Target > ;
159175 type Error = PyErr ;
160176
177+ #[ cfg( feature = "experimental-inspect" ) ]
178+ const OUTPUT_TYPE : TypeHint = NaiveDate :: OUTPUT_TYPE ;
179+
161180 #[ inline]
162181 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
163182 ( * self ) . into_pyobject ( py)
@@ -167,6 +186,9 @@ impl<'py> IntoPyObject<'py> for &NaiveDate {
167186impl FromPyObject < ' _ , ' _ > for NaiveDate {
168187 type Error = PyErr ;
169188
189+ #[ cfg( feature = "experimental-inspect" ) ]
190+ const INPUT_TYPE : TypeHint = PyDate :: TYPE_HINT ;
191+
170192 fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
171193 let date = & * ob. cast :: < PyDate > ( ) ?;
172194 py_date_to_naive_date ( date)
@@ -178,6 +200,9 @@ impl<'py> IntoPyObject<'py> for NaiveTime {
178200 type Output = Bound < ' py , Self :: Target > ;
179201 type Error = PyErr ;
180202
203+ #[ cfg( feature = "experimental-inspect" ) ]
204+ const OUTPUT_TYPE : TypeHint = PyTime :: TYPE_HINT ;
205+
181206 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
182207 let TimeArgs {
183208 hour,
@@ -202,6 +227,9 @@ impl<'py> IntoPyObject<'py> for &NaiveTime {
202227 type Output = Bound < ' py , Self :: Target > ;
203228 type Error = PyErr ;
204229
230+ #[ cfg( feature = "experimental-inspect" ) ]
231+ const OUTPUT_TYPE : TypeHint = NaiveTime :: OUTPUT_TYPE ;
232+
205233 #[ inline]
206234 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
207235 ( * self ) . into_pyobject ( py)
@@ -211,6 +239,9 @@ impl<'py> IntoPyObject<'py> for &NaiveTime {
211239impl FromPyObject < ' _ , ' _ > for NaiveTime {
212240 type Error = PyErr ;
213241
242+ #[ cfg( feature = "experimental-inspect" ) ]
243+ const INPUT_TYPE : TypeHint = PyTime :: TYPE_HINT ;
244+
214245 fn extract ( ob : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
215246 let time = & * ob. cast :: < PyTime > ( ) ?;
216247 py_time_to_naive_time ( time)
@@ -222,6 +253,9 @@ impl<'py> IntoPyObject<'py> for NaiveDateTime {
222253 type Output = Bound < ' py , Self :: Target > ;
223254 type Error = PyErr ;
224255
256+ #[ cfg( feature = "experimental-inspect" ) ]
257+ const OUTPUT_TYPE : TypeHint = PyDateTime :: TYPE_HINT ;
258+
225259 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
226260 let DateArgs { year, month, day } = ( & self . date ( ) ) . into ( ) ;
227261 let TimeArgs {
@@ -247,6 +281,9 @@ impl<'py> IntoPyObject<'py> for &NaiveDateTime {
247281 type Output = Bound < ' py , Self :: Target > ;
248282 type Error = PyErr ;
249283
284+ #[ cfg( feature = "experimental-inspect" ) ]
285+ const OUTPUT_TYPE : TypeHint = NaiveDateTime :: OUTPUT_TYPE ;
286+
250287 #[ inline]
251288 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
252289 ( * self ) . into_pyobject ( py)
@@ -256,6 +293,9 @@ impl<'py> IntoPyObject<'py> for &NaiveDateTime {
256293impl FromPyObject < ' _ , ' _ > for NaiveDateTime {
257294 type Error = PyErr ;
258295
296+ #[ cfg( feature = "experimental-inspect" ) ]
297+ const INPUT_TYPE : TypeHint = PyDateTime :: TYPE_HINT ;
298+
259299 fn extract ( dt : Borrowed < ' _ , ' _ , PyAny > ) -> Result < Self , Self :: Error > {
260300 let dt = & * dt. cast :: < PyDateTime > ( ) ?;
261301
@@ -280,6 +320,9 @@ where
280320 type Output = Bound < ' py , Self :: Target > ;
281321 type Error = PyErr ;
282322
323+ #[ cfg( feature = "experimental-inspect" ) ]
324+ const OUTPUT_TYPE : TypeHint = <& DateTime < Tz > >:: OUTPUT_TYPE ;
325+
283326 #[ inline]
284327 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
285328 ( & self ) . into_pyobject ( py)
@@ -294,6 +337,9 @@ where
294337 type Output = Bound < ' py , Self :: Target > ;
295338 type Error = PyErr ;
296339
340+ #[ cfg( feature = "experimental-inspect" ) ]
341+ const OUTPUT_TYPE : TypeHint = PyDateTime :: TYPE_HINT ;
342+
297343 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
298344 let tz = self . timezone ( ) . into_bound_py_any ( py) ?. cast_into ( ) ?;
299345
@@ -338,6 +384,9 @@ where
338384{
339385 type Error = PyErr ;
340386
387+ #[ cfg( feature = "experimental-inspect" ) ]
388+ const INPUT_TYPE : TypeHint = PyDateTime :: TYPE_HINT ;
389+
341390 fn extract ( dt : Borrowed < ' _ , ' py , PyAny > ) -> Result < Self , Self :: Error > {
342391 let dt = & * dt. cast :: < PyDateTime > ( ) ?;
343392 let tzinfo = dt. get_tzinfo ( ) ;
@@ -365,6 +414,9 @@ impl<'py> IntoPyObject<'py> for FixedOffset {
365414 type Output = Bound < ' py , Self :: Target > ;
366415 type Error = PyErr ;
367416
417+ #[ cfg( feature = "experimental-inspect" ) ]
418+ const OUTPUT_TYPE : TypeHint = PyTzInfo :: TYPE_HINT ;
419+
368420 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
369421 let seconds_offset = self . local_minus_utc ( ) ;
370422 let td = PyDelta :: new ( py, 0 , seconds_offset, 0 , true ) ?;
@@ -377,6 +429,9 @@ impl<'py> IntoPyObject<'py> for &FixedOffset {
377429 type Output = Bound < ' py , Self :: Target > ;
378430 type Error = PyErr ;
379431
432+ #[ cfg( feature = "experimental-inspect" ) ]
433+ const OUTPUT_TYPE : TypeHint = FixedOffset :: OUTPUT_TYPE ;
434+
380435 #[ inline]
381436 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
382437 ( * self ) . into_pyobject ( py)
@@ -386,6 +441,9 @@ impl<'py> IntoPyObject<'py> for &FixedOffset {
386441impl FromPyObject < ' _ , ' _ > for FixedOffset {
387442 type Error = PyErr ;
388443
444+ #[ cfg( feature = "experimental-inspect" ) ]
445+ const INPUT_TYPE : TypeHint = PyTzInfo :: TYPE_HINT ;
446+
389447 /// Convert python tzinfo to rust [`FixedOffset`].
390448 ///
391449 /// Note that the conversion will result in precision lost in microseconds as chrono offset
@@ -418,6 +476,9 @@ impl<'py> IntoPyObject<'py> for Utc {
418476 type Output = Borrowed < ' static , ' py , Self :: Target > ;
419477 type Error = PyErr ;
420478
479+ #[ cfg( feature = "experimental-inspect" ) ]
480+ const OUTPUT_TYPE : TypeHint = PyTzInfo :: TYPE_HINT ;
481+
421482 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
422483 PyTzInfo :: utc ( py)
423484 }
@@ -428,6 +489,9 @@ impl<'py> IntoPyObject<'py> for &Utc {
428489 type Output = Borrowed < ' static , ' py , Self :: Target > ;
429490 type Error = PyErr ;
430491
492+ #[ cfg( feature = "experimental-inspect" ) ]
493+ const OUTPUT_TYPE : TypeHint = Utc :: OUTPUT_TYPE ;
494+
431495 #[ inline]
432496 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
433497 ( * self ) . into_pyobject ( py)
@@ -453,6 +517,9 @@ impl<'py> IntoPyObject<'py> for Local {
453517 type Output = Borrowed < ' static , ' py , Self :: Target > ;
454518 type Error = PyErr ;
455519
520+ #[ cfg( feature = "experimental-inspect" ) ]
521+ const OUTPUT_TYPE : TypeHint = PyTzInfo :: TYPE_HINT ;
522+
456523 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
457524 static LOCAL_TZ : PyOnceLock < Py < PyTzInfo > > = PyOnceLock :: new ( ) ;
458525 let tz = LOCAL_TZ
@@ -473,6 +540,9 @@ impl<'py> IntoPyObject<'py> for &Local {
473540 type Output = Borrowed < ' static , ' py , Self :: Target > ;
474541 type Error = PyErr ;
475542
543+ #[ cfg( feature = "experimental-inspect" ) ]
544+ const OUTPUT_TYPE : TypeHint = Local :: OUTPUT_TYPE ;
545+
476546 #[ inline]
477547 fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
478548 ( * self ) . into_pyobject ( py)
0 commit comments