@@ -306,7 +306,7 @@ impl PyAny {
306
306
/// Python::with_gil(|py| -> PyResult<()> {
307
307
/// let a: &PyInt = 0_u8.into_py(py).into_ref(py).downcast()?;
308
308
/// let b: &PyInt = 42_u8.into_py(py).into_ref(py).downcast()?;
309
- /// assert!(a.rich_compare(b, CompareOp::Le)?.is_true ()?);
309
+ /// assert!(a.rich_compare(b, CompareOp::Le)?.is_truthy ()?);
310
310
/// Ok(())
311
311
/// })?;
312
312
/// # Ok(())}
@@ -640,8 +640,16 @@ impl PyAny {
640
640
/// Returns whether the object is considered to be true.
641
641
///
642
642
/// This is equivalent to the Python expression `bool(self)`.
643
+ #[ deprecated( since = "0.21.0" , note = "use `.is_truthy()` instead" ) ]
643
644
pub fn is_true ( & self ) -> PyResult < bool > {
644
- Py2 :: borrowed_from_gil_ref ( & self ) . is_true ( )
645
+ self . is_truthy ( )
646
+ }
647
+
648
+ /// Returns whether the object is considered to be true.
649
+ ///
650
+ /// This applies truth value testing equivalent to the Python expression `bool(self)`.
651
+ pub fn is_truthy ( & self ) -> PyResult < bool > {
652
+ Py2 :: borrowed_from_gil_ref ( & self ) . is_truthy ( )
645
653
}
646
654
647
655
/// Returns whether the object is considered to be None.
@@ -1165,7 +1173,7 @@ pub(crate) trait PyAnyMethods<'py> {
1165
1173
/// Python::with_gil(|py| -> PyResult<()> {
1166
1174
/// let a: &PyInt = 0_u8.into_py(py).into_ref(py).downcast()?;
1167
1175
/// let b: &PyInt = 42_u8.into_py(py).into_ref(py).downcast()?;
1168
- /// assert!(a.rich_compare(b, CompareOp::Le)?.is_true ()?);
1176
+ /// assert!(a.rich_compare(b, CompareOp::Le)?.is_truthy ()?);
1169
1177
/// Ok(())
1170
1178
/// })?;
1171
1179
/// # Ok(())}
@@ -1452,7 +1460,7 @@ pub(crate) trait PyAnyMethods<'py> {
1452
1460
/// Returns whether the object is considered to be true.
1453
1461
///
1454
1462
/// This is equivalent to the Python expression `bool(self)`.
1455
- fn is_true ( & self ) -> PyResult < bool > ;
1463
+ fn is_truthy ( & self ) -> PyResult < bool > ;
1456
1464
1457
1465
/// Returns whether the object is considered to be None.
1458
1466
///
@@ -1773,7 +1781,7 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
1773
1781
let do_compare = |other, op| unsafe {
1774
1782
ffi:: PyObject_RichCompare ( any. as_ptr ( ) , other, op)
1775
1783
. assume_owned_or_err ( any. py ( ) )
1776
- . and_then ( |obj| obj. is_true ( ) )
1784
+ . and_then ( |obj| obj. is_truthy ( ) )
1777
1785
} ;
1778
1786
if do_compare ( other, ffi:: Py_EQ ) ? {
1779
1787
Ok ( Ordering :: Equal )
@@ -1816,47 +1824,47 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
1816
1824
O : ToPyObject ,
1817
1825
{
1818
1826
self . rich_compare ( other, CompareOp :: Lt )
1819
- . and_then ( |any| any. is_true ( ) )
1827
+ . and_then ( |any| any. is_truthy ( ) )
1820
1828
}
1821
1829
1822
1830
fn le < O > ( & self , other : O ) -> PyResult < bool >
1823
1831
where
1824
1832
O : ToPyObject ,
1825
1833
{
1826
1834
self . rich_compare ( other, CompareOp :: Le )
1827
- . and_then ( |any| any. is_true ( ) )
1835
+ . and_then ( |any| any. is_truthy ( ) )
1828
1836
}
1829
1837
1830
1838
fn eq < O > ( & self , other : O ) -> PyResult < bool >
1831
1839
where
1832
1840
O : ToPyObject ,
1833
1841
{
1834
1842
self . rich_compare ( other, CompareOp :: Eq )
1835
- . and_then ( |any| any. is_true ( ) )
1843
+ . and_then ( |any| any. is_truthy ( ) )
1836
1844
}
1837
1845
1838
1846
fn ne < O > ( & self , other : O ) -> PyResult < bool >
1839
1847
where
1840
1848
O : ToPyObject ,
1841
1849
{
1842
1850
self . rich_compare ( other, CompareOp :: Ne )
1843
- . and_then ( |any| any. is_true ( ) )
1851
+ . and_then ( |any| any. is_truthy ( ) )
1844
1852
}
1845
1853
1846
1854
fn gt < O > ( & self , other : O ) -> PyResult < bool >
1847
1855
where
1848
1856
O : ToPyObject ,
1849
1857
{
1850
1858
self . rich_compare ( other, CompareOp :: Gt )
1851
- . and_then ( |any| any. is_true ( ) )
1859
+ . and_then ( |any| any. is_truthy ( ) )
1852
1860
}
1853
1861
1854
1862
fn ge < O > ( & self , other : O ) -> PyResult < bool >
1855
1863
where
1856
1864
O : ToPyObject ,
1857
1865
{
1858
1866
self . rich_compare ( other, CompareOp :: Ge )
1859
- . and_then ( |any| any. is_true ( ) )
1867
+ . and_then ( |any| any. is_truthy ( ) )
1860
1868
}
1861
1869
1862
1870
fn is_callable ( & self ) -> bool {
@@ -1948,7 +1956,7 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
1948
1956
self . call_method ( name, args, None )
1949
1957
}
1950
1958
1951
- fn is_true ( & self ) -> PyResult < bool > {
1959
+ fn is_truthy ( & self ) -> PyResult < bool > {
1952
1960
let v = unsafe { ffi:: PyObject_IsTrue ( self . as_ptr ( ) ) } ;
1953
1961
err:: error_on_minusone ( self . py ( ) , v) ?;
1954
1962
Ok ( v != 0 )
@@ -2554,7 +2562,7 @@ class SimpleClass:
2554
2562
assert ! ( !py_int
2555
2563
. rich_compare( py_str, CompareOp :: Eq )
2556
2564
. unwrap( )
2557
- . is_true ( )
2565
+ . is_truthy ( )
2558
2566
. unwrap( ) ) ;
2559
2567
} )
2560
2568
}
0 commit comments