Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 92dac38

Browse files
committed
Add signature to the display of functions in the shell
1 parent 5f1d831 commit 92dac38

3 files changed

Lines changed: 36 additions & 11 deletions

File tree

IPython/core/tests/test_oinspect.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,27 +305,32 @@ def test_empty_property_has_no_source():
305305

306306

307307
def test_property_sources():
308-
import zlib
309-
308+
import posixpath
309+
# A simple adder whose source and signature stays
310+
# the same across Python distributions
311+
def simple_add(a, b):
312+
"Adds two numbers"
313+
return a + b
314+
310315
class A(object):
311316
@property
312317
def foo(self):
313318
return 'bar'
314319

315320
foo = foo.setter(lambda self, v: setattr(self, 'bar', v))
316321

317-
id = property(id)
318-
compress = property(zlib.compress)
322+
dname = property(posixpath.dirname)
323+
adder = property(simple_add)
319324

320325
i = inspector.info(A.foo, detail_level=1)
321326
nt.assert_in('def foo(self):', i['source'])
322327
nt.assert_in('lambda self, v:', i['source'])
323328

324-
i = inspector.info(A.id, detail_level=1)
325-
nt.assert_in('fget = <function id>', i['source'])
326-
327-
i = inspector.info(A.compress, detail_level=1)
328-
nt.assert_in('fget = <function zlib.compress>', i['source'])
329+
i = inspector.info(A.dname, detail_level=1)
330+
nt.assert_in('def dirname(p)', i['source'])
331+
332+
i = inspector.info(A.adder, detail_level=1)
333+
nt.assert_in('def simple_add(a, b)', i['source'])
329334

330335

331336
def test_property_docstring_is_in_info_for_detail_level_0():

IPython/lib/pretty.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _repr_pretty_(self, p, cycle):
8888

8989
from IPython.utils.decorators import undoc
9090
from IPython.utils.py3compat import PYPY
91-
91+
from IPython.utils.signatures import signature
9292

9393
__all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter',
9494
'for_type', 'for_type_by_name']
@@ -711,7 +711,11 @@ def _function_pprint(obj, p, cycle):
711711
mod = obj.__module__
712712
if mod and mod not in ('__builtin__', 'builtins', 'exceptions'):
713713
name = mod + '.' + name
714-
p.text('<function %s>' % name)
714+
try:
715+
func_def = name + str(signature(obj))
716+
except ValueError:
717+
func_def = name
718+
p.text('<function %s>' % func_def)
715719

716720

717721
def _exception_pprint(obj, p, cycle):

IPython/lib/tests/test_pretty.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,19 @@ def test_mappingproxy():
405405
]
406406
for obj, expected in cases:
407407
nt.assert_equal(pretty.pretty(obj), expected)
408+
409+
def test_function_pretty():
410+
"Test pretty print of function"
411+
# posixpath is a pure python function, its interface is consistent
412+
# across Python distributions
413+
import os
414+
nt.assert_equal(pretty.pretty(os.path.join), '<function posixpath.join(a, *p)>')
415+
416+
# custom function
417+
def meaning_of_life(question=None):
418+
if question:
419+
return 42
420+
return "Don't panic"
421+
422+
nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life))
423+

0 commit comments

Comments
 (0)