Skip to content

Commit ff50285

Browse files
authored
Merge pull request #3657 from PyO3/is-truthy
Transition is_true to is_truthy to clarify that the test is not based on identity with or equality to the True singleton.
2 parents 867a273 + ced97f8 commit ff50285

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

newsfragments/3657.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Changed `.is_true` to `.is_truthy` on `PyAny` and `Py<PyAny>` to clarify that the test is not based on identity with or equality to the True singleton.

src/instance.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,15 @@ impl<T> Py<T> {
787787
/// Returns whether the object is considered to be true.
788788
///
789789
/// This is equivalent to the Python expression `bool(self)`.
790+
#[deprecated(since = "0.21.0", note = "use `.is_truthy()` instead")]
790791
pub fn is_true(&self, py: Python<'_>) -> PyResult<bool> {
792+
self.is_truthy(py)
793+
}
794+
795+
/// Returns whether the object is considered to be true.
796+
///
797+
/// This applies truth value testing equivalent to the Python expression `bool(self)`.
798+
pub fn is_truthy(&self, py: Python<'_>) -> PyResult<bool> {
791799
let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
792800
err::error_on_minusone(py, v)?;
793801
Ok(v != 0)

src/types/any.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl PyAny {
306306
/// Python::with_gil(|py| -> PyResult<()> {
307307
/// let a: &PyInt = 0_u8.into_py(py).into_ref(py).downcast()?;
308308
/// 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()?);
310310
/// Ok(())
311311
/// })?;
312312
/// # Ok(())}
@@ -640,8 +640,16 @@ impl PyAny {
640640
/// Returns whether the object is considered to be true.
641641
///
642642
/// This is equivalent to the Python expression `bool(self)`.
643+
#[deprecated(since = "0.21.0", note = "use `.is_truthy()` instead")]
643644
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()
645653
}
646654

647655
/// Returns whether the object is considered to be None.
@@ -1165,7 +1173,7 @@ pub(crate) trait PyAnyMethods<'py> {
11651173
/// Python::with_gil(|py| -> PyResult<()> {
11661174
/// let a: &PyInt = 0_u8.into_py(py).into_ref(py).downcast()?;
11671175
/// 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()?);
11691177
/// Ok(())
11701178
/// })?;
11711179
/// # Ok(())}
@@ -1452,7 +1460,7 @@ pub(crate) trait PyAnyMethods<'py> {
14521460
/// Returns whether the object is considered to be true.
14531461
///
14541462
/// This is equivalent to the Python expression `bool(self)`.
1455-
fn is_true(&self) -> PyResult<bool>;
1463+
fn is_truthy(&self) -> PyResult<bool>;
14561464

14571465
/// Returns whether the object is considered to be None.
14581466
///
@@ -1773,7 +1781,7 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
17731781
let do_compare = |other, op| unsafe {
17741782
ffi::PyObject_RichCompare(any.as_ptr(), other, op)
17751783
.assume_owned_or_err(any.py())
1776-
.and_then(|obj| obj.is_true())
1784+
.and_then(|obj| obj.is_truthy())
17771785
};
17781786
if do_compare(other, ffi::Py_EQ)? {
17791787
Ok(Ordering::Equal)
@@ -1816,47 +1824,47 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
18161824
O: ToPyObject,
18171825
{
18181826
self.rich_compare(other, CompareOp::Lt)
1819-
.and_then(|any| any.is_true())
1827+
.and_then(|any| any.is_truthy())
18201828
}
18211829

18221830
fn le<O>(&self, other: O) -> PyResult<bool>
18231831
where
18241832
O: ToPyObject,
18251833
{
18261834
self.rich_compare(other, CompareOp::Le)
1827-
.and_then(|any| any.is_true())
1835+
.and_then(|any| any.is_truthy())
18281836
}
18291837

18301838
fn eq<O>(&self, other: O) -> PyResult<bool>
18311839
where
18321840
O: ToPyObject,
18331841
{
18341842
self.rich_compare(other, CompareOp::Eq)
1835-
.and_then(|any| any.is_true())
1843+
.and_then(|any| any.is_truthy())
18361844
}
18371845

18381846
fn ne<O>(&self, other: O) -> PyResult<bool>
18391847
where
18401848
O: ToPyObject,
18411849
{
18421850
self.rich_compare(other, CompareOp::Ne)
1843-
.and_then(|any| any.is_true())
1851+
.and_then(|any| any.is_truthy())
18441852
}
18451853

18461854
fn gt<O>(&self, other: O) -> PyResult<bool>
18471855
where
18481856
O: ToPyObject,
18491857
{
18501858
self.rich_compare(other, CompareOp::Gt)
1851-
.and_then(|any| any.is_true())
1859+
.and_then(|any| any.is_truthy())
18521860
}
18531861

18541862
fn ge<O>(&self, other: O) -> PyResult<bool>
18551863
where
18561864
O: ToPyObject,
18571865
{
18581866
self.rich_compare(other, CompareOp::Ge)
1859-
.and_then(|any| any.is_true())
1867+
.and_then(|any| any.is_truthy())
18601868
}
18611869

18621870
fn is_callable(&self) -> bool {
@@ -1948,7 +1956,7 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
19481956
self.call_method(name, args, None)
19491957
}
19501958

1951-
fn is_true(&self) -> PyResult<bool> {
1959+
fn is_truthy(&self) -> PyResult<bool> {
19521960
let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
19531961
err::error_on_minusone(self.py(), v)?;
19541962
Ok(v != 0)
@@ -2554,7 +2562,7 @@ class SimpleClass:
25542562
assert!(!py_int
25552563
.rich_compare(py_str, CompareOp::Eq)
25562564
.unwrap()
2557-
.is_true()
2565+
.is_truthy()
25582566
.unwrap());
25592567
})
25602568
}

tests/test_proto_methods.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ fn test_hash() {
159159
fn test_bool() {
160160
Python::with_gil(|py| {
161161
let example_py = make_example(py);
162-
assert!(example_py.is_true().unwrap());
162+
assert!(example_py.is_truthy().unwrap());
163163
example_py.borrow_mut().value = 0;
164-
assert!(!example_py.is_true().unwrap());
164+
assert!(!example_py.is_truthy().unwrap());
165165
})
166166
}
167167

0 commit comments

Comments
 (0)