From e5d97fbbebedb31d3be99ae510733ceba7dbce1c 2023-04-25 12:58:36 From: Matthias Bussonnier Date: 2023-04-25 12:58:36 Subject: [PATCH] Backport PR #14004: Don't do truthy check on object; use `is not None` --- diff --git a/IPython/core/oinspect.py b/IPython/core/oinspect.py index 83aada8..0b8b2b8 100644 --- a/IPython/core/oinspect.py +++ b/IPython/core/oinspect.py @@ -835,7 +835,7 @@ class Inspector(Colorable): att_name = oname.split(".")[-1] parents_docs = None prelude = "" - if info and info.parent and hasattr(info.parent, HOOK_NAME): + if info and info.parent is not None and hasattr(info.parent, HOOK_NAME): parents_docs_dict = getattr(info.parent, HOOK_NAME) parents_docs = parents_docs_dict.get(att_name, None) out = dict( diff --git a/IPython/core/tests/test_oinspect.py b/IPython/core/tests/test_oinspect.py index d37a405..fd325c5 100644 --- a/IPython/core/tests/test_oinspect.py +++ b/IPython/core/tests/test_oinspect.py @@ -370,6 +370,23 @@ def cleanup_user_ns(**kwargs): del ip.user_ns[k] +def test_pinfo_bool_raise(): + """ + Test that bool method is not called on parent. + """ + + class RaiseBool: + attr = None + + def __bool__(self): + raise ValueError("pinfo should not access this method") + + raise_bool = RaiseBool() + + with cleanup_user_ns(raise_bool=raise_bool): + ip._inspect("pinfo", "raise_bool.attr", detail_level=0) + + def test_pinfo_getindex(): def dummy(): """