/IPython/core/tests/test_oinspect.py

http://github.com/ipython/ipython · Python · 439 lines · 287 code · 96 blank · 56 comment · 15 complexity · e3024826fd7f7ce85cc02f6208d4a285 MD5 · raw file

  1. """Tests for the object inspection functionality.
  2. """
  3. # Copyright (c) IPython Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. from inspect import signature, Signature, Parameter
  6. import os
  7. import re
  8. import nose.tools as nt
  9. from .. import oinspect
  10. from decorator import decorator
  11. from IPython.testing.tools import AssertPrints, AssertNotPrints
  12. from IPython.utils.path import compress_user
  13. #-----------------------------------------------------------------------------
  14. # Globals and constants
  15. #-----------------------------------------------------------------------------
  16. inspector = None
  17. def setup_module():
  18. global inspector
  19. inspector = oinspect.Inspector()
  20. #-----------------------------------------------------------------------------
  21. # Local utilities
  22. #-----------------------------------------------------------------------------
  23. # WARNING: since this test checks the line number where a function is
  24. # defined, if any code is inserted above, the following line will need to be
  25. # updated. Do NOT insert any whitespace between the next line and the function
  26. # definition below.
  27. THIS_LINE_NUMBER = 41 # Put here the actual number of this line
  28. from unittest import TestCase
  29. class Test(TestCase):
  30. def test_find_source_lines(self):
  31. self.assertEqual(oinspect.find_source_lines(Test.test_find_source_lines),
  32. THIS_LINE_NUMBER+6)
  33. # A couple of utilities to ensure these tests work the same from a source or a
  34. # binary install
  35. def pyfile(fname):
  36. return os.path.normcase(re.sub('.py[co]$', '.py', fname))
  37. def match_pyfiles(f1, f2):
  38. nt.assert_equal(pyfile(f1), pyfile(f2))
  39. def test_find_file():
  40. match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))
  41. def test_find_file_decorated1():
  42. @decorator
  43. def noop1(f):
  44. def wrapper(*a, **kw):
  45. return f(*a, **kw)
  46. return wrapper
  47. @noop1
  48. def f(x):
  49. "My docstring"
  50. match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
  51. nt.assert_equal(f.__doc__, "My docstring")
  52. def test_find_file_decorated2():
  53. @decorator
  54. def noop2(f, *a, **kw):
  55. return f(*a, **kw)
  56. @noop2
  57. @noop2
  58. @noop2
  59. def f(x):
  60. "My docstring 2"
  61. match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
  62. nt.assert_equal(f.__doc__, "My docstring 2")
  63. def test_find_file_magic():
  64. run = ip.find_line_magic('run')
  65. nt.assert_not_equal(oinspect.find_file(run), None)
  66. # A few generic objects we can then inspect in the tests below
  67. class Call(object):
  68. """This is the class docstring."""
  69. def __init__(self, x, y=1):
  70. """This is the constructor docstring."""
  71. def __call__(self, *a, **kw):
  72. """This is the call docstring."""
  73. def method(self, x, z=2):
  74. """Some method's docstring"""
  75. class HasSignature(object):
  76. """This is the class docstring."""
  77. __signature__ = Signature([Parameter('test', Parameter.POSITIONAL_OR_KEYWORD)])
  78. def __init__(self, *args):
  79. """This is the init docstring"""
  80. class SimpleClass(object):
  81. def method(self, x, z=2):
  82. """Some method's docstring"""
  83. class Awkward(object):
  84. def __getattr__(self, name):
  85. raise Exception(name)
  86. class NoBoolCall:
  87. """
  88. callable with `__bool__` raising should still be inspect-able.
  89. """
  90. def __call__(self):
  91. """does nothing"""
  92. pass
  93. def __bool__(self):
  94. """just raise NotImplemented"""
  95. raise NotImplementedError('Must be implemented')
  96. class SerialLiar(object):
  97. """Attribute accesses always get another copy of the same class.
  98. unittest.mock.call does something similar, but it's not ideal for testing
  99. as the failure mode is to eat all your RAM. This gives up after 10k levels.
  100. """
  101. def __init__(self, max_fibbing_twig, lies_told=0):
  102. if lies_told > 10000:
  103. raise RuntimeError('Nose too long, honesty is the best policy')
  104. self.max_fibbing_twig = max_fibbing_twig
  105. self.lies_told = lies_told
  106. max_fibbing_twig[0] = max(max_fibbing_twig[0], lies_told)
  107. def __getattr__(self, item):
  108. return SerialLiar(self.max_fibbing_twig, self.lies_told + 1)
  109. #-----------------------------------------------------------------------------
  110. # Tests
  111. #-----------------------------------------------------------------------------
  112. def test_info():
  113. "Check that Inspector.info fills out various fields as expected."
  114. i = inspector.info(Call, oname='Call')
  115. nt.assert_equal(i['type_name'], 'type')
  116. expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
  117. nt.assert_equal(i['base_class'], expted_class)
  118. nt.assert_regex(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'( at 0x[0-9a-f]{1,9})?>")
  119. fname = __file__
  120. if fname.endswith(".pyc"):
  121. fname = fname[:-1]
  122. # case-insensitive comparison needed on some filesystems
  123. # e.g. Windows:
  124. nt.assert_equal(i['file'].lower(), compress_user(fname).lower())
  125. nt.assert_equal(i['definition'], None)
  126. nt.assert_equal(i['docstring'], Call.__doc__)
  127. nt.assert_equal(i['source'], None)
  128. nt.assert_true(i['isclass'])
  129. nt.assert_equal(i['init_definition'], "Call(x, y=1)")
  130. nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
  131. i = inspector.info(Call, detail_level=1)
  132. nt.assert_not_equal(i['source'], None)
  133. nt.assert_equal(i['docstring'], None)
  134. c = Call(1)
  135. c.__doc__ = "Modified instance docstring"
  136. i = inspector.info(c)
  137. nt.assert_equal(i['type_name'], 'Call')
  138. nt.assert_equal(i['docstring'], "Modified instance docstring")
  139. nt.assert_equal(i['class_docstring'], Call.__doc__)
  140. nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
  141. nt.assert_equal(i['call_docstring'], Call.__call__.__doc__)
  142. def test_class_signature():
  143. info = inspector.info(HasSignature, 'HasSignature')
  144. nt.assert_equal(info['init_definition'], "HasSignature(test)")
  145. nt.assert_equal(info['init_docstring'], HasSignature.__init__.__doc__)
  146. def test_info_awkward():
  147. # Just test that this doesn't throw an error.
  148. inspector.info(Awkward())
  149. def test_bool_raise():
  150. inspector.info(NoBoolCall())
  151. def test_info_serialliar():
  152. fib_tracker = [0]
  153. inspector.info(SerialLiar(fib_tracker))
  154. # Nested attribute access should be cut off at 100 levels deep to avoid
  155. # infinite loops: https://github.com/ipython/ipython/issues/9122
  156. nt.assert_less(fib_tracker[0], 9000)
  157. def support_function_one(x, y=2, *a, **kw):
  158. """A simple function."""
  159. def test_calldef_none():
  160. # We should ignore __call__ for all of these.
  161. for obj in [support_function_one, SimpleClass().method, any, str.upper]:
  162. i = inspector.info(obj)
  163. nt.assert_is(i['call_def'], None)
  164. def f_kwarg(pos, *, kwonly):
  165. pass
  166. def test_definition_kwonlyargs():
  167. i = inspector.info(f_kwarg, oname='f_kwarg') # analysis:ignore
  168. nt.assert_equal(i['definition'], "f_kwarg(pos, *, kwonly)")
  169. def test_getdoc():
  170. class A(object):
  171. """standard docstring"""
  172. pass
  173. class B(object):
  174. """standard docstring"""
  175. def getdoc(self):
  176. return "custom docstring"
  177. class C(object):
  178. """standard docstring"""
  179. def getdoc(self):
  180. return None
  181. a = A()
  182. b = B()
  183. c = C()
  184. nt.assert_equal(oinspect.getdoc(a), "standard docstring")
  185. nt.assert_equal(oinspect.getdoc(b), "custom docstring")
  186. nt.assert_equal(oinspect.getdoc(c), "standard docstring")
  187. def test_empty_property_has_no_source():
  188. i = inspector.info(property(), detail_level=1)
  189. nt.assert_is(i['source'], None)
  190. def test_property_sources():
  191. import posixpath
  192. # A simple adder whose source and signature stays
  193. # the same across Python distributions
  194. def simple_add(a, b):
  195. "Adds two numbers"
  196. return a + b
  197. class A(object):
  198. @property
  199. def foo(self):
  200. return 'bar'
  201. foo = foo.setter(lambda self, v: setattr(self, 'bar', v))
  202. dname = property(posixpath.dirname)
  203. adder = property(simple_add)
  204. i = inspector.info(A.foo, detail_level=1)
  205. nt.assert_in('def foo(self):', i['source'])
  206. nt.assert_in('lambda self, v:', i['source'])
  207. i = inspector.info(A.dname, detail_level=1)
  208. nt.assert_in('def dirname(p)', i['source'])
  209. i = inspector.info(A.adder, detail_level=1)
  210. nt.assert_in('def simple_add(a, b)', i['source'])
  211. def test_property_docstring_is_in_info_for_detail_level_0():
  212. class A(object):
  213. @property
  214. def foobar(self):
  215. """This is `foobar` property."""
  216. pass
  217. ip.user_ns['a_obj'] = A()
  218. nt.assert_equal(
  219. 'This is `foobar` property.',
  220. ip.object_inspect('a_obj.foobar', detail_level=0)['docstring'])
  221. ip.user_ns['a_cls'] = A
  222. nt.assert_equal(
  223. 'This is `foobar` property.',
  224. ip.object_inspect('a_cls.foobar', detail_level=0)['docstring'])
  225. def test_pdef():
  226. # See gh-1914
  227. def foo(): pass
  228. inspector.pdef(foo, 'foo')
  229. def test_pinfo_nonascii():
  230. # See gh-1177
  231. from . import nonascii2
  232. ip.user_ns['nonascii2'] = nonascii2
  233. ip._inspect('pinfo', 'nonascii2', detail_level=1)
  234. def test_pinfo_type():
  235. """
  236. type can fail in various edge case, for example `type.__subclass__()`
  237. """
  238. ip._inspect('pinfo', 'type')
  239. def test_pinfo_docstring_no_source():
  240. """Docstring should be included with detail_level=1 if there is no source"""
  241. with AssertPrints('Docstring:'):
  242. ip._inspect('pinfo', 'str.format', detail_level=0)
  243. with AssertPrints('Docstring:'):
  244. ip._inspect('pinfo', 'str.format', detail_level=1)
  245. def test_pinfo_no_docstring_if_source():
  246. """Docstring should not be included with detail_level=1 if source is found"""
  247. def foo():
  248. """foo has a docstring"""
  249. ip.user_ns['foo'] = foo
  250. with AssertPrints('Docstring:'):
  251. ip._inspect('pinfo', 'foo', detail_level=0)
  252. with AssertPrints('Source:'):
  253. ip._inspect('pinfo', 'foo', detail_level=1)
  254. with AssertNotPrints('Docstring:'):
  255. ip._inspect('pinfo', 'foo', detail_level=1)
  256. def test_pinfo_docstring_if_detail_and_no_source():
  257. """ Docstring should be displayed if source info not available """
  258. obj_def = '''class Foo(object):
  259. """ This is a docstring for Foo """
  260. def bar(self):
  261. """ This is a docstring for Foo.bar """
  262. pass
  263. '''
  264. ip.run_cell(obj_def)
  265. ip.run_cell('foo = Foo()')
  266. with AssertNotPrints("Source:"):
  267. with AssertPrints('Docstring:'):
  268. ip._inspect('pinfo', 'foo', detail_level=0)
  269. with AssertPrints('Docstring:'):
  270. ip._inspect('pinfo', 'foo', detail_level=1)
  271. with AssertPrints('Docstring:'):
  272. ip._inspect('pinfo', 'foo.bar', detail_level=0)
  273. with AssertNotPrints('Docstring:'):
  274. with AssertPrints('Source:'):
  275. ip._inspect('pinfo', 'foo.bar', detail_level=1)
  276. def test_pinfo_magic():
  277. with AssertPrints('Docstring:'):
  278. ip._inspect('pinfo', 'lsmagic', detail_level=0)
  279. with AssertPrints('Source:'):
  280. ip._inspect('pinfo', 'lsmagic', detail_level=1)
  281. def test_init_colors():
  282. # ensure colors are not present in signature info
  283. info = inspector.info(HasSignature)
  284. init_def = info['init_definition']
  285. nt.assert_not_in('[0m', init_def)
  286. def test_builtin_init():
  287. info = inspector.info(list)
  288. init_def = info['init_definition']
  289. nt.assert_is_not_none(init_def)
  290. def test_render_signature_short():
  291. def short_fun(a=1): pass
  292. sig = oinspect._render_signature(
  293. signature(short_fun),
  294. short_fun.__name__,
  295. )
  296. nt.assert_equal(sig, 'short_fun(a=1)')
  297. def test_render_signature_long():
  298. from typing import Optional
  299. def long_function(
  300. a_really_long_parameter: int,
  301. and_another_long_one: bool = False,
  302. let_us_make_sure_this_is_looong: Optional[str] = None,
  303. ) -> bool: pass
  304. sig = oinspect._render_signature(
  305. signature(long_function),
  306. long_function.__name__,
  307. )
  308. nt.assert_in(sig, [
  309. # Python >=3.7
  310. '''\
  311. long_function(
  312. a_really_long_parameter: int,
  313. and_another_long_one: bool = False,
  314. let_us_make_sure_this_is_looong: Union[str, NoneType] = None,
  315. ) -> bool\
  316. ''', # Python <=3.6
  317. '''\
  318. long_function(
  319. a_really_long_parameter:int,
  320. and_another_long_one:bool=False,
  321. let_us_make_sure_this_is_looong:Union[str, NoneType]=None,
  322. ) -> bool\
  323. ''',
  324. ])