##// END OF EJS Templates
Merge pull request #9335 from minrk/class-signature...
Matthias Bussonnier -
r22178:24fa4d88 merge
parent child Browse files
Show More
@@ -420,7 +420,6 b' class Inspector(Colorable):'
420
420
421 if inspect.isclass(obj):
421 if inspect.isclass(obj):
422 header = self.__head('Class constructor information:\n')
422 header = self.__head('Class constructor information:\n')
423 obj = obj.__init__
424 elif (not py3compat.PY3) and type(obj) is types.InstanceType:
423 elif (not py3compat.PY3) and type(obj) is types.InstanceType:
425 obj = obj.__call__
424 obj = obj.__call__
426
425
@@ -766,23 +765,29 b' class Inspector(Colorable):'
766 # Constructor docstring for classes
765 # Constructor docstring for classes
767 if inspect.isclass(obj):
766 if inspect.isclass(obj):
768 out['isclass'] = True
767 out['isclass'] = True
769 # reconstruct the function definition and print it:
768
769 # get the init signature:
770 try:
770 try:
771 obj_init = obj.__init__
771 init_def = self._getdef(obj, oname)
772 except AttributeError:
772 except AttributeError:
773 init_def = init_ds = None
773 init_def = None
774
775 if init_def:
776 out['init_definition'] = self.format(init_def)
777
778 # get the __init__ docstring
779 try:
780 obj_init = obj.__init__
781 except AttributeError:
782 init_ds = None
774 else:
783 else:
775 init_def = self._getdef(obj_init,oname)
784 init_ds = getdoc(obj_init)
776 init_ds = getdoc(obj_init)
777 # Skip Python's auto-generated docstrings
785 # Skip Python's auto-generated docstrings
778 if init_ds == _object_init_docstring:
786 if init_ds == _object_init_docstring:
779 init_ds = None
787 init_ds = None
780
788
781 if init_def or init_ds:
789 if init_ds:
782 if init_def:
790 out['init_docstring'] = init_ds
783 out['init_definition'] = self.format(init_def)
784 if init_ds:
785 out['init_docstring'] = init_ds
786
791
787 # and class docstring for instances:
792 # and class docstring for instances:
788 else:
793 else:
@@ -1,27 +1,17 b''
1 """Tests for the object inspection functionality.
1 """Tests for the object inspection functionality.
2 """
2 """
3 #-----------------------------------------------------------------------------
4 # Copyright (C) 2010-2011 The IPython Development Team.
5 #
6 # Distributed under the terms of the BSD License.
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
10
3
11 #-----------------------------------------------------------------------------
4 # Copyright (c) IPython Development Team.
12 # Imports
5 # Distributed under the terms of the Modified BSD License.
13 #-----------------------------------------------------------------------------
6
14 from __future__ import print_function
7 from __future__ import print_function
15
8
16 # Stdlib imports
17 import os
9 import os
18 import re
10 import re
19 import sys
11 import sys
20
12
21 # Third-party imports
22 import nose.tools as nt
13 import nose.tools as nt
23
14
24 # Our own imports
25 from .. import oinspect
15 from .. import oinspect
26 from IPython.core.magic import (Magics, magics_class, line_magic,
16 from IPython.core.magic import (Magics, magics_class, line_magic,
27 cell_magic, line_cell_magic,
17 cell_magic, line_cell_magic,
@@ -32,6 +22,7 b' from IPython.testing.decorators import skipif'
32 from IPython.testing.tools import AssertPrints
22 from IPython.testing.tools import AssertPrints
33 from IPython.utils.path import compress_user
23 from IPython.utils.path import compress_user
34 from IPython.utils import py3compat
24 from IPython.utils import py3compat
25 from IPython.utils.signatures import Signature, Parameter
35
26
36
27
37 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
@@ -49,7 +40,7 b' ip = get_ipython()'
49 # defined, if any code is inserted above, the following line will need to be
40 # defined, if any code is inserted above, the following line will need to be
50 # updated. Do NOT insert any whitespace between the next line and the function
41 # updated. Do NOT insert any whitespace between the next line and the function
51 # definition below.
42 # definition below.
52 THIS_LINE_NUMBER = 52 # Put here the actual number of this line
43 THIS_LINE_NUMBER = 43 # Put here the actual number of this line
53 def test_find_source_lines():
44 def test_find_source_lines():
54 nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
45 nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
55 THIS_LINE_NUMBER+1)
46 THIS_LINE_NUMBER+1)
@@ -120,6 +111,14 b' class Call(object):'
120 def method(self, x, z=2):
111 def method(self, x, z=2):
121 """Some method's docstring"""
112 """Some method's docstring"""
122
113
114 class HasSignature(object):
115 """This is the class docstring."""
116 __signature__ = Signature([Parameter('test', Parameter.POSITIONAL_OR_KEYWORD)])
117
118 def __init__(self, *args):
119 """This is the init docstring"""
120
121
123 class SimpleClass(object):
122 class SimpleClass(object):
124 def method(self, x, z=2):
123 def method(self, x, z=2):
125 """Some method's docstring"""
124 """Some method's docstring"""
@@ -282,7 +281,8 b' def test_info():'
282 nt.assert_equal(i['docstring'], Call.__doc__)
281 nt.assert_equal(i['docstring'], Call.__doc__)
283 nt.assert_equal(i['source'], None)
282 nt.assert_equal(i['source'], None)
284 nt.assert_true(i['isclass'])
283 nt.assert_true(i['isclass'])
285 nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
284 _self_py2 = '' if py3compat.PY3 else 'self, '
285 nt.assert_equal(i['init_definition'], "Call(%sx, y=1)\n" % _self_py2)
286 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
286 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
287
287
288 i = inspector.info(Call, detail_level=1)
288 i = inspector.info(Call, detail_level=1)
@@ -307,6 +307,11 b' def test_info():'
307 nt.assert_equal(i['type_name'], 'instance')
307 nt.assert_equal(i['type_name'], 'instance')
308 nt.assert_equal(i['docstring'], OldStyle.__doc__)
308 nt.assert_equal(i['docstring'], OldStyle.__doc__)
309
309
310 def test_class_signature():
311 info = inspector.info(HasSignature, 'HasSignature')
312 nt.assert_equal(info['init_definition'], "HasSignature(test)\n")
313 nt.assert_equal(info['init_docstring'], HasSignature.__init__.__doc__)
314
310 def test_info_awkward():
315 def test_info_awkward():
311 # Just test that this doesn't throw an error.
316 # Just test that this doesn't throw an error.
312 i = inspector.info(Awkward())
317 i = inspector.info(Awkward())
General Comments 0
You need to be logged in to leave comments. Login now