From dc9d1c679424589480bb83af64aa5ee21031d311 2017-11-03 12:08:04 From: Thomas Kluyver Date: 2017-11-03 12:08:04 Subject: [PATCH] Merge pull request #10863 from Madhu94/add-function-signature-display Add signature to display of functions in the shell --- diff --git a/IPython/core/tests/test_oinspect.py b/IPython/core/tests/test_oinspect.py index d94bbf1..00d0938 100644 --- a/IPython/core/tests/test_oinspect.py +++ b/IPython/core/tests/test_oinspect.py @@ -305,8 +305,13 @@ def test_empty_property_has_no_source(): def test_property_sources(): - import zlib - + import posixpath + # A simple adder whose source and signature stays + # the same across Python distributions + def simple_add(a, b): + "Adds two numbers" + return a + b + class A(object): @property def foo(self): @@ -314,18 +319,18 @@ def test_property_sources(): foo = foo.setter(lambda self, v: setattr(self, 'bar', v)) - id = property(id) - compress = property(zlib.compress) + dname = property(posixpath.dirname) + adder = property(simple_add) i = inspector.info(A.foo, detail_level=1) nt.assert_in('def foo(self):', i['source']) nt.assert_in('lambda self, v:', i['source']) - i = inspector.info(A.id, detail_level=1) - nt.assert_in('fget = ', i['source']) - - i = inspector.info(A.compress, detail_level=1) - nt.assert_in('fget = ', i['source']) + i = inspector.info(A.dname, detail_level=1) + nt.assert_in('def dirname(p)', i['source']) + + i = inspector.info(A.adder, detail_level=1) + nt.assert_in('def simple_add(a, b)', i['source']) def test_property_docstring_is_in_info_for_detail_level_0(): diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 899ff02..cbbb726 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -88,7 +88,7 @@ from warnings import warn from IPython.utils.decorators import undoc from IPython.utils.py3compat import PYPY - +from IPython.utils.signatures import signature __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter', 'for_type', 'for_type_by_name'] @@ -711,7 +711,11 @@ def _function_pprint(obj, p, cycle): mod = obj.__module__ if mod and mod not in ('__builtin__', 'builtins', 'exceptions'): name = mod + '.' + name - p.text('' % name) + try: + func_def = name + str(signature(obj)) + except ValueError: + func_def = name + p.text('' % func_def) def _exception_pprint(obj, p, cycle): diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py index 934498b..6d65743 100644 --- a/IPython/lib/tests/test_pretty.py +++ b/IPython/lib/tests/test_pretty.py @@ -405,3 +405,19 @@ def test_mappingproxy(): ] for obj, expected in cases: nt.assert_equal(pretty.pretty(obj), expected) + +def test_function_pretty(): + "Test pretty print of function" + # posixpath is a pure python module, its interface is consistent + # across Python distributions + import posixpath + nt.assert_equal(pretty.pretty(posixpath.join), '') + + # custom function + def meaning_of_life(question=None): + if question: + return 42 + return "Don't panic" + + nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life)) +