@@ -772,7 +772,7 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed {
772772 /// })
773773 /// # }
774774 /// ```
775- // FIXME(icxolu) deprecate in favor of `Bound::cast`
775+ # [ deprecated ( since = "0.27.0" , note = "use `Bound::cast` instead" ) ]
776776 fn downcast < T > ( & self ) -> Result < & Bound < ' py , T > , DowncastError < ' _ , ' py > >
777777 where
778778 T : PyTypeCheck ;
@@ -800,7 +800,7 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed {
800800 /// assert!(obj.downcast_into::<PyDict>().is_ok());
801801 /// })
802802 /// ```
803- // FIXME(icxolu) deprecate in favor of `Bound::cast_into`
803+ # [ deprecated ( since = "0.27.0" , note = "use `Bound::cast_into` instead" ) ]
804804 fn downcast_into < T > ( self ) -> Result < Bound < ' py , T > , DowncastIntoError < ' py > >
805805 where
806806 T : PyTypeCheck ;
@@ -836,13 +836,13 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed {
836836 /// assert!(any.downcast_exact::<PyBool>().is_ok());
837837 /// });
838838 /// ```
839- // FIXME(icxolu) deprecate in favor of `Bound::cast_exact`
839+ # [ deprecated ( since = "0.27.0" , note = "use `Bound::cast_exact` instead" ) ]
840840 fn downcast_exact < T > ( & self ) -> Result < & Bound < ' py , T > , DowncastError < ' _ , ' py > >
841841 where
842842 T : PyTypeInfo ;
843843
844844 /// Like `downcast_exact` but takes ownership of `self`.
845- // FIXME(icxolu) deprecate in favor of `Bound::cast_into_exact`
845+ # [ deprecated ( since = "0.27.0" , note = "use `Bound::cast_into_exact` instead" ) ]
846846 fn downcast_into_exact < T > ( self ) -> Result < Bound < ' py , T > , DowncastIntoError < ' py > >
847847 where
848848 T : PyTypeInfo ;
@@ -852,15 +852,15 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed {
852852 /// # Safety
853853 ///
854854 /// Callers must ensure that the type is valid or risk type confusion.
855- // FIXME(icxolu) deprecate in favor of `Bound::cast_unchecked`
855+ # [ deprecated ( since = "0.27.0" , note = "use `Bound::cast_unchecked` instead" ) ]
856856 unsafe fn downcast_unchecked < T > ( & self ) -> & Bound < ' py , T > ;
857857
858858 /// Like `downcast_unchecked` but takes ownership of `self`.
859859 ///
860860 /// # Safety
861861 ///
862862 /// Callers must ensure that the type is valid or risk type confusion.
863- // FIXME(icxolu) deprecate in favor of `Bound::cast_into_unchecked`
863+ # [ deprecated ( since = "0.27.0" , note = "use `Bound::cast_into_unchecked` instead" ) ]
864864 unsafe fn downcast_into_unchecked < T > ( self ) -> Bound < ' py , T > ;
865865
866866 /// Extracts some type from the Python object.
@@ -1449,31 +1449,55 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
14491449 where
14501450 T : PyTypeCheck ,
14511451 {
1452- self . cast ( )
1452+ if T :: type_check ( self ) {
1453+ // Safety: type_check is responsible for ensuring that the type is correct
1454+ Ok ( unsafe { self . cast_unchecked ( ) } )
1455+ } else {
1456+ #[ allow( deprecated) ]
1457+ Err ( DowncastError :: new ( self , T :: NAME ) )
1458+ }
14531459 }
14541460
14551461 #[ inline]
14561462 fn downcast_into < T > ( self ) -> Result < Bound < ' py , T > , DowncastIntoError < ' py > >
14571463 where
14581464 T : PyTypeCheck ,
14591465 {
1460- self . cast_into ( )
1466+ if T :: type_check ( & self ) {
1467+ // Safety: type_check is responsible for ensuring that the type is correct
1468+ Ok ( unsafe { self . cast_into_unchecked ( ) } )
1469+ } else {
1470+ #[ allow( deprecated) ]
1471+ Err ( DowncastIntoError :: new ( self , T :: NAME ) )
1472+ }
14611473 }
14621474
14631475 #[ inline]
14641476 fn downcast_exact < T > ( & self ) -> Result < & Bound < ' py , T > , DowncastError < ' _ , ' py > >
14651477 where
14661478 T : PyTypeInfo ,
14671479 {
1468- self . cast_exact ( )
1480+ if T :: is_exact_type_of ( self ) {
1481+ // Safety: is_exact_type_of is responsible for ensuring that the type is correct
1482+ Ok ( unsafe { self . cast_unchecked ( ) } )
1483+ } else {
1484+ #[ allow( deprecated) ]
1485+ Err ( DowncastError :: new ( self , T :: NAME ) )
1486+ }
14691487 }
14701488
14711489 #[ inline]
14721490 fn downcast_into_exact < T > ( self ) -> Result < Bound < ' py , T > , DowncastIntoError < ' py > >
14731491 where
14741492 T : PyTypeInfo ,
14751493 {
1476- self . cast_into_exact ( )
1494+ if T :: is_exact_type_of ( & self ) {
1495+ // Safety: is_exact_type_of is responsible for ensuring that the type is correct
1496+ Ok ( unsafe { self . cast_into_unchecked ( ) } )
1497+ } else {
1498+ #[ allow( deprecated) ]
1499+ Err ( DowncastIntoError :: new ( self , T :: NAME ) )
1500+ }
14771501 }
14781502
14791503 #[ inline]
0 commit comments