test_parser.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # -*- coding: utf-8 -*-
  2. from textwrap import dedent
  3. import pytest
  4. from parso._compatibility import u
  5. from parso import parse
  6. from parso.python import tree
  7. from parso.utils import split_lines
  8. def test_basic_parsing(each_version):
  9. def compare(string):
  10. """Generates the AST object and then regenerates the code."""
  11. assert parse(string, version=each_version).get_code() == string
  12. compare('\na #pass\n')
  13. compare('wblabla* 1\t\n')
  14. compare('def x(a, b:3): pass\n')
  15. compare('assert foo\n')
  16. def test_subscope_names(each_version):
  17. def get_sub(source):
  18. return parse(source, version=each_version).children[0]
  19. name = get_sub('class Foo: pass').name
  20. assert name.start_pos == (1, len('class '))
  21. assert name.end_pos == (1, len('class Foo'))
  22. assert name.value == 'Foo'
  23. name = get_sub('def foo(): pass').name
  24. assert name.start_pos == (1, len('def '))
  25. assert name.end_pos == (1, len('def foo'))
  26. assert name.value == 'foo'
  27. def test_import_names(each_version):
  28. def get_import(source):
  29. return next(parse(source, version=each_version).iter_imports())
  30. imp = get_import('import math\n')
  31. names = imp.get_defined_names()
  32. assert len(names) == 1
  33. assert names[0].value == 'math'
  34. assert names[0].start_pos == (1, len('import '))
  35. assert names[0].end_pos == (1, len('import math'))
  36. assert imp.start_pos == (1, 0)
  37. assert imp.end_pos == (1, len('import math'))
  38. def test_end_pos(each_version):
  39. s = dedent('''
  40. x = ['a', 'b', 'c']
  41. def func():
  42. y = None
  43. ''')
  44. parser = parse(s, version=each_version)
  45. scope = next(parser.iter_funcdefs())
  46. assert scope.start_pos == (3, 0)
  47. assert scope.end_pos == (5, 0)
  48. def test_carriage_return_statements(each_version):
  49. source = dedent('''
  50. foo = 'ns1!'
  51. # this is a namespace package
  52. ''')
  53. source = source.replace('\n', '\r\n')
  54. stmt = parse(source, version=each_version).children[0]
  55. assert '#' not in stmt.get_code()
  56. def test_incomplete_list_comprehension(each_version):
  57. """ Shouldn't raise an error, same bug as #418. """
  58. # With the old parser this actually returned a statement. With the new
  59. # parser only valid statements generate one.
  60. children = parse('(1 for def', version=each_version).children
  61. assert [c.type for c in children] == \
  62. ['error_node', 'error_node', 'endmarker']
  63. def test_newline_positions(each_version):
  64. endmarker = parse('a\n', version=each_version).children[-1]
  65. assert endmarker.end_pos == (2, 0)
  66. new_line = endmarker.get_previous_leaf()
  67. assert new_line.start_pos == (1, 1)
  68. assert new_line.end_pos == (2, 0)
  69. def test_end_pos_error_correction(each_version):
  70. """
  71. Source code without ending newline are given one, because the Python
  72. grammar needs it. However, they are removed again. We still want the right
  73. end_pos, even if something breaks in the parser (error correction).
  74. """
  75. s = 'def x():\n .'
  76. m = parse(s, version=each_version)
  77. func = m.children[0]
  78. assert func.type == 'funcdef'
  79. assert func.end_pos == (2, 2)
  80. assert m.end_pos == (2, 2)
  81. def test_param_splitting(each_version):
  82. """
  83. Jedi splits parameters into params, this is not what the grammar does,
  84. but Jedi does this to simplify argument parsing.
  85. """
  86. def check(src, result):
  87. # Python 2 tuple params should be ignored for now.
  88. m = parse(src, version=each_version)
  89. if each_version.startswith('2'):
  90. # We don't want b and c to be a part of the param enumeration. Just
  91. # ignore them, because it's not what we want to support in the
  92. # future.
  93. func = next(m.iter_funcdefs())
  94. assert [param.name.value for param in func.get_params()] == result
  95. else:
  96. assert not list(m.iter_funcdefs())
  97. check('def x(a, (b, c)):\n pass', ['a'])
  98. check('def x((b, c)):\n pass', [])
  99. def test_unicode_string():
  100. s = tree.String(None, u('bö'), (0, 0))
  101. assert repr(s) # Should not raise an Error!
  102. def test_backslash_dos_style(each_version):
  103. assert parse('\\\r\n', version=each_version)
  104. def test_started_lambda_stmt(each_version):
  105. m = parse(u'lambda a, b: a i', version=each_version)
  106. assert m.children[0].type == 'error_node'
  107. def test_python2_octal(each_version):
  108. module = parse('0660', version=each_version)
  109. first = module.children[0]
  110. if each_version.startswith('2'):
  111. assert first.type == 'number'
  112. else:
  113. assert first.type == 'error_node'
  114. @pytest.mark.parametrize('code', ['foo "', 'foo """\n', 'foo """\nbar'])
  115. def test_open_string_literal(each_version, code):
  116. """
  117. Testing mostly if removing the last newline works.
  118. """
  119. lines = split_lines(code, keepends=True)
  120. end_pos = (len(lines), len(lines[-1]))
  121. module = parse(code, version=each_version)
  122. assert module.get_code() == code
  123. assert module.end_pos == end_pos == module.children[1].end_pos
  124. def test_too_many_params():
  125. with pytest.raises(TypeError):
  126. parse('asdf', hello=3)
  127. def test_dedent_at_end(each_version):
  128. code = dedent('''
  129. for foobar in [1]:
  130. foobar''')
  131. module = parse(code, version=each_version)
  132. assert module.get_code() == code
  133. suite = module.children[0].children[-1]
  134. foobar = suite.children[-1]
  135. assert foobar.type == 'name'
  136. def test_no_error_nodes(each_version):
  137. def check(node):
  138. assert node.type not in ('error_leaf', 'error_node')
  139. try:
  140. children = node.children
  141. except AttributeError:
  142. pass
  143. else:
  144. for child in children:
  145. check(child)
  146. check(parse("if foo:\n bar", version=each_version))
  147. def test_named_expression(works_ge_py38):
  148. works_ge_py38.parse("(a := 1, a + 1)")
  149. def test_extended_rhs_annassign(works_ge_py38):
  150. works_ge_py38.parse("x: y = z,")
  151. works_ge_py38.parse("x: Tuple[int, ...] = z, *q, w")
  152. @pytest.mark.parametrize(
  153. 'param_code', [
  154. 'a=1, /',
  155. 'a, /',
  156. 'a=1, /, b=3',
  157. 'a, /, b',
  158. 'a, /, b',
  159. 'a, /, *, b',
  160. 'a, /, **kwargs',
  161. ]
  162. )
  163. def test_positional_only_arguments(works_ge_py38, param_code):
  164. works_ge_py38.parse("def x(%s): pass" % param_code)
  165. @pytest.mark.parametrize(
  166. 'expression', [
  167. 'a + a',
  168. 'lambda x: x',
  169. 'a := lambda x: x'
  170. ]
  171. )
  172. def test_decorator_expression(works_ge_py39, expression):
  173. works_ge_py39.parse("@%s\ndef x(): pass" % expression)