test_parser_tree.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # -*- coding: utf-8 # This file contains Unicode characters.
  2. from textwrap import dedent
  3. import pytest
  4. from parso import parse
  5. from parso.python import tree
  6. class TestsFunctionAndLambdaParsing(object):
  7. FIXTURES = [
  8. ('def my_function(x, y, z) -> str:\n return x + y * z\n', {
  9. 'name': 'my_function',
  10. 'call_sig': 'my_function(x, y, z)',
  11. 'params': ['x', 'y', 'z'],
  12. 'annotation': "str",
  13. }),
  14. ('lambda x, y, z: x + y * z\n', {
  15. 'name': '<lambda>',
  16. 'call_sig': '<lambda>(x, y, z)',
  17. 'params': ['x', 'y', 'z'],
  18. }),
  19. ]
  20. @pytest.fixture(params=FIXTURES)
  21. def node(self, request):
  22. parsed = parse(dedent(request.param[0]), version='3.5')
  23. request.keywords['expected'] = request.param[1]
  24. child = parsed.children[0]
  25. if child.type == 'simple_stmt':
  26. child = child.children[0]
  27. return child
  28. @pytest.fixture()
  29. def expected(self, request, node):
  30. return request.keywords['expected']
  31. def test_name(self, node, expected):
  32. if node.type != 'lambdef':
  33. assert isinstance(node.name, tree.Name)
  34. assert node.name.value == expected['name']
  35. def test_params(self, node, expected):
  36. assert isinstance(node.get_params(), list)
  37. assert all(isinstance(x, tree.Param) for x in node.get_params())
  38. assert [str(x.name.value) for x in node.get_params()] == [x for x in expected['params']]
  39. def test_is_generator(self, node, expected):
  40. assert node.is_generator() is expected.get('is_generator', False)
  41. def test_yields(self, node, expected):
  42. assert node.is_generator() == expected.get('yields', False)
  43. def test_annotation(self, node, expected):
  44. expected_annotation = expected.get('annotation', None)
  45. if expected_annotation is None:
  46. assert node.annotation is None
  47. else:
  48. assert node.annotation.value == expected_annotation
  49. def test_end_pos_line(each_version):
  50. # jedi issue #150
  51. s = "x()\nx( )\nx( )\nx ( )\n"
  52. module = parse(s, version=each_version)
  53. for i, simple_stmt in enumerate(module.children[:-1]):
  54. expr_stmt = simple_stmt.children[0]
  55. assert expr_stmt.end_pos == (i + 1, i + 3)
  56. def test_default_param(each_version):
  57. func = parse('def x(foo=42): pass', version=each_version).children[0]
  58. param, = func.get_params()
  59. assert param.default.value == '42'
  60. assert param.annotation is None
  61. assert not param.star_count
  62. def test_annotation_param(each_py3_version):
  63. func = parse('def x(foo: 3): pass', version=each_py3_version).children[0]
  64. param, = func.get_params()
  65. assert param.default is None
  66. assert param.annotation.value == '3'
  67. assert not param.star_count
  68. def test_annotation_params(each_py3_version):
  69. func = parse('def x(foo: 3, bar: 4): pass', version=each_py3_version).children[0]
  70. param1, param2 = func.get_params()
  71. assert param1.default is None
  72. assert param1.annotation.value == '3'
  73. assert not param1.star_count
  74. assert param2.default is None
  75. assert param2.annotation.value == '4'
  76. assert not param2.star_count
  77. def test_default_and_annotation_param(each_py3_version):
  78. func = parse('def x(foo:3=42): pass', version=each_py3_version).children[0]
  79. param, = func.get_params()
  80. assert param.default.value == '42'
  81. assert param.annotation.value == '3'
  82. assert not param.star_count
  83. def test_ellipsis_py2(each_py2_version):
  84. module = parse('[0][...]', version=each_py2_version, error_recovery=False)
  85. expr = module.children[0]
  86. trailer = expr.children[-1]
  87. subscript = trailer.children[1]
  88. assert subscript.type == 'subscript'
  89. assert [leaf.value for leaf in subscript.children] == ['.', '.', '.']
  90. def get_yield_exprs(code, version):
  91. return list(parse(code, version=version).children[0].iter_yield_exprs())
  92. def get_return_stmts(code):
  93. return list(parse(code).children[0].iter_return_stmts())
  94. def get_raise_stmts(code, child):
  95. return list(parse(code).children[child].iter_raise_stmts())
  96. def test_yields(each_version):
  97. y, = get_yield_exprs('def x(): yield', each_version)
  98. assert y.value == 'yield'
  99. assert y.type == 'keyword'
  100. y, = get_yield_exprs('def x(): (yield 1)', each_version)
  101. assert y.type == 'yield_expr'
  102. y, = get_yield_exprs('def x(): [1, (yield)]', each_version)
  103. assert y.type == 'keyword'
  104. def test_yield_from():
  105. y, = get_yield_exprs('def x(): (yield from 1)', '3.8')
  106. assert y.type == 'yield_expr'
  107. def test_returns():
  108. r, = get_return_stmts('def x(): return')
  109. assert r.value == 'return'
  110. assert r.type == 'keyword'
  111. r, = get_return_stmts('def x(): return 1')
  112. assert r.type == 'return_stmt'
  113. def test_raises():
  114. code = """
  115. def single_function():
  116. raise Exception
  117. def top_function():
  118. def inner_function():
  119. raise NotImplementedError()
  120. inner_function()
  121. raise Exception
  122. def top_function_three():
  123. try:
  124. raise NotImplementedError()
  125. except NotImplementedError:
  126. pass
  127. raise Exception
  128. """
  129. r = get_raise_stmts(code, 0) # Lists in a simple Function
  130. assert len(list(r)) == 1
  131. r = get_raise_stmts(code, 1) # Doesn't Exceptions list in closures
  132. assert len(list(r)) == 1
  133. r = get_raise_stmts(code, 2) # Lists inside try-catch
  134. assert len(list(r)) == 2
  135. @pytest.mark.parametrize(
  136. 'code, name_index, is_definition, include_setitem', [
  137. ('x = 3', 0, True, False),
  138. ('x.y = 3', 0, False, False),
  139. ('x.y = 3', 1, True, False),
  140. ('x.y = u.v = z', 0, False, False),
  141. ('x.y = u.v = z', 1, True, False),
  142. ('x.y = u.v = z', 2, False, False),
  143. ('x.y = u.v, w = z', 3, True, False),
  144. ('x.y = u.v, w = z', 4, True, False),
  145. ('x.y = u.v, w = z', 5, False, False),
  146. ('x, y = z', 0, True, False),
  147. ('x, y = z', 1, True, False),
  148. ('x, y = z', 2, False, False),
  149. ('x, y = z', 2, False, False),
  150. ('x[0], y = z', 2, False, False),
  151. ('x[0] = z', 0, False, False),
  152. ('x[0], y = z', 0, False, False),
  153. ('x[0], y = z', 2, False, True),
  154. ('x[0] = z', 0, True, True),
  155. ('x[0], y = z', 0, True, True),
  156. ('x: int = z', 0, True, False),
  157. ('x: int = z', 1, False, False),
  158. ('x: int = z', 2, False, False),
  159. ('x: int', 0, True, False),
  160. ('x: int', 1, False, False),
  161. ]
  162. )
  163. def test_is_definition(code, name_index, is_definition, include_setitem):
  164. module = parse(code, version='3.8')
  165. name = module.get_first_leaf()
  166. while True:
  167. if name.type == 'name':
  168. if name_index == 0:
  169. break
  170. name_index -= 1
  171. name = name.get_next_leaf()
  172. assert name.is_definition(include_setitem=include_setitem) == is_definition
  173. def test_iter_funcdefs():
  174. code = dedent('''
  175. def normal(): ...
  176. async def asyn(): ...
  177. @dec
  178. def dec_normal(): ...
  179. @dec1
  180. @dec2
  181. async def dec_async(): ...
  182. def broken
  183. ''')
  184. module = parse(code, version='3.8')
  185. func_names = [f.name.value for f in module.iter_funcdefs()]
  186. assert func_names == ['normal', 'asyn', 'dec_normal', 'dec_async']