From ebedbe275bb7c89098114f9f44ab0f44b92d102a 2016-12-01 13:21:57 From: Min RK Date: 2016-12-01 13:21:57 Subject: [PATCH] include docstring in detail_level=1 if no source is found We currently display source for ?? and docstring for ?, which means less info is displayed if no source is found (e.g. compiled functions). This displays docstring for ?? as a fallback when no Source is found. This is a regression in 5.x --- diff --git a/IPython/core/oinspect.py b/IPython/core/oinspect.py index 892b6f0..0684770 100644 --- a/IPython/core/oinspect.py +++ b/IPython/core/oinspect.py @@ -645,7 +645,7 @@ class Inspector(Colorable): # Functions, methods, classes append_field(_mime, 'Signature', 'definition', code_formatter) append_field(_mime, 'Init signature', 'init_definition', code_formatter) - if detail_level > 0: + if detail_level > 0 and info['source']: append_field(_mime, 'Source', 'source', code_formatter) else: append_field(_mime, 'Docstring', 'docstring', formatter) diff --git a/IPython/core/tests/test_oinspect.py b/IPython/core/tests/test_oinspect.py index 3db81fb..1f9b920 100644 --- a/IPython/core/tests/test_oinspect.py +++ b/IPython/core/tests/test_oinspect.py @@ -18,7 +18,7 @@ from IPython.core.magic import (Magics, magics_class, line_magic, register_line_cell_magic) from decorator import decorator from IPython.testing.decorators import skipif -from IPython.testing.tools import AssertPrints +from IPython.testing.tools import AssertPrints, AssertNotPrints from IPython.utils.path import compress_user from IPython.utils import py3compat from IPython.utils.signatures import Signature, Parameter @@ -406,12 +406,12 @@ def test_property_docstring_is_in_info_for_detail_level_0(): pass ip.user_ns['a_obj'] = A() - nt.assert_equals( + nt.assert_equal( 'This is `foobar` property.', ip.object_inspect('a_obj.foobar', detail_level=0)['docstring']) ip.user_ns['a_cls'] = A - nt.assert_equals( + nt.assert_equal( 'This is `foobar` property.', ip.object_inspect('a_cls.foobar', detail_level=0)['docstring']) @@ -429,6 +429,29 @@ def test_pinfo_nonascii(): ip._inspect('pinfo', 'nonascii2', detail_level=1) +def test_pinfo_docstring_no_source(): + """Docstring should be included with detail_level=1 if there is no source""" + with AssertPrints('Docstring:'): + ip._inspect('pinfo', 'str.format', detail_level=0) + with AssertPrints('Docstring:'): + ip._inspect('pinfo', 'str.format', detail_level=1) + + +def test_pinfo_no_docstring_if_source(): + """Docstring should not be included with detail_level=1 if source is found""" + def foo(): + """foo has a docstring""" + + ip.user_ns['foo'] = foo + + with AssertPrints('Docstring:'): + ip._inspect('pinfo', 'foo', detail_level=0) + with AssertPrints('Source:'): + ip._inspect('pinfo', 'foo', detail_level=1) + with AssertNotPrints('Docstring:'): + ip._inspect('pinfo', 'foo', detail_level=1) + + def test_pinfo_magic(): with AssertPrints('Docstring:'): ip._inspect('pinfo', 'lsmagic', detail_level=0)