test_python_errors.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. """
  2. Testing if parso finds syntax errors and indentation errors.
  3. """
  4. import sys
  5. import warnings
  6. import pytest
  7. import parso
  8. from textwrap import dedent
  9. from parso._compatibility import is_pypy
  10. from .failing_examples import FAILING_EXAMPLES, indent, build_nested
  11. if is_pypy:
  12. # The errors in PyPy might be different. Just skip the module for now.
  13. pytestmark = pytest.mark.skip()
  14. def _get_error_list(code, version=None):
  15. grammar = parso.load_grammar(version=version)
  16. tree = grammar.parse(code)
  17. return list(grammar.iter_errors(tree))
  18. def assert_comparison(code, error_code, positions):
  19. errors = [(error.start_pos, error.code) for error in _get_error_list(code)]
  20. assert [(pos, error_code) for pos in positions] == errors
  21. @pytest.mark.parametrize('code', FAILING_EXAMPLES)
  22. def test_python_exception_matches(code):
  23. wanted, line_nr = _get_actual_exception(code)
  24. errors = _get_error_list(code)
  25. actual = None
  26. if errors:
  27. error, = errors
  28. actual = error.message
  29. assert actual in wanted
  30. # Somehow in Python2.7 the SyntaxError().lineno is sometimes None
  31. assert line_nr is None or line_nr == error.start_pos[0]
  32. def test_non_async_in_async():
  33. """
  34. This example doesn't work with FAILING_EXAMPLES, because the line numbers
  35. are not always the same / incorrect in Python 3.8.
  36. """
  37. if sys.version_info[:2] < (3, 5):
  38. pytest.skip()
  39. # Raises multiple errors in previous versions.
  40. code = 'async def foo():\n def nofoo():[x async for x in []]'
  41. wanted, line_nr = _get_actual_exception(code)
  42. errors = _get_error_list(code)
  43. if errors:
  44. error, = errors
  45. actual = error.message
  46. assert actual in wanted
  47. if sys.version_info[:2] < (3, 8):
  48. assert line_nr == error.start_pos[0]
  49. else:
  50. assert line_nr == 0 # For whatever reason this is zero in Python 3.8+
  51. @pytest.mark.parametrize(
  52. ('code', 'positions'), [
  53. ('1 +', [(1, 3)]),
  54. ('1 +\n', [(1, 3)]),
  55. ('1 +\n2 +', [(1, 3), (2, 3)]),
  56. ('x + 2', []),
  57. ('[\n', [(2, 0)]),
  58. ('[\ndef x(): pass', [(2, 0)]),
  59. ('[\nif 1: pass', [(2, 0)]),
  60. ('1+?', [(1, 2)]),
  61. ('?', [(1, 0)]),
  62. ('??', [(1, 0)]),
  63. ('? ?', [(1, 0)]),
  64. ('?\n?', [(1, 0), (2, 0)]),
  65. ('? * ?', [(1, 0)]),
  66. ('1 + * * 2', [(1, 4)]),
  67. ('?\n1\n?', [(1, 0), (3, 0)]),
  68. ]
  69. )
  70. def test_syntax_errors(code, positions):
  71. assert_comparison(code, 901, positions)
  72. @pytest.mark.parametrize(
  73. ('code', 'positions'), [
  74. (' 1', [(1, 0)]),
  75. ('def x():\n 1\n 2', [(3, 0)]),
  76. ('def x():\n 1\n 2', [(3, 0)]),
  77. ('def x():\n1', [(2, 0)]),
  78. ]
  79. )
  80. def test_indentation_errors(code, positions):
  81. assert_comparison(code, 903, positions)
  82. def _get_actual_exception(code):
  83. with warnings.catch_warnings():
  84. # We don't care about warnings where locals/globals misbehave here.
  85. # It's as simple as either an error or not.
  86. warnings.filterwarnings('ignore', category=SyntaxWarning)
  87. try:
  88. compile(code, '<unknown>', 'exec')
  89. except (SyntaxError, IndentationError) as e:
  90. wanted = e.__class__.__name__ + ': ' + e.msg
  91. line_nr = e.lineno
  92. except ValueError as e:
  93. # The ValueError comes from byte literals in Python 2 like '\x'
  94. # that are oddly enough not SyntaxErrors.
  95. wanted = 'SyntaxError: (value error) ' + str(e)
  96. line_nr = None
  97. else:
  98. assert False, "The piece of code should raise an exception."
  99. # SyntaxError
  100. if wanted == 'SyntaxError: non-keyword arg after keyword arg':
  101. # The python 3.5+ way, a bit nicer.
  102. wanted = 'SyntaxError: positional argument follows keyword argument'
  103. elif wanted == 'SyntaxError: assignment to keyword':
  104. return [wanted, "SyntaxError: can't assign to keyword",
  105. 'SyntaxError: cannot assign to __debug__'], line_nr
  106. elif wanted == 'SyntaxError: can use starred expression only as assignment target':
  107. # Python 3.4/3.4 have a bit of a different warning than 3.5/3.6 in
  108. # certain places. But in others this error makes sense.
  109. return [wanted, "SyntaxError: can't use starred expression here"], line_nr
  110. elif wanted == 'SyntaxError: f-string: unterminated string':
  111. wanted = 'SyntaxError: EOL while scanning string literal'
  112. elif wanted == 'SyntaxError: f-string expression part cannot include a backslash':
  113. return [
  114. wanted,
  115. "SyntaxError: EOL while scanning string literal",
  116. "SyntaxError: unexpected character after line continuation character",
  117. ], line_nr
  118. elif wanted == "SyntaxError: f-string: expecting '}'":
  119. wanted = 'SyntaxError: EOL while scanning string literal'
  120. elif wanted == 'SyntaxError: f-string: empty expression not allowed':
  121. wanted = 'SyntaxError: invalid syntax'
  122. elif wanted == "SyntaxError: f-string expression part cannot include '#'":
  123. wanted = 'SyntaxError: invalid syntax'
  124. elif wanted == "SyntaxError: f-string: single '}' is not allowed":
  125. wanted = 'SyntaxError: invalid syntax'
  126. return [wanted], line_nr
  127. def test_default_except_error_postition():
  128. # For this error the position seemed to be one line off, but that doesn't
  129. # really matter.
  130. code = 'try: pass\nexcept: pass\nexcept X: pass'
  131. wanted, line_nr = _get_actual_exception(code)
  132. error, = _get_error_list(code)
  133. assert error.message in wanted
  134. assert line_nr != error.start_pos[0]
  135. # I think this is the better position.
  136. assert error.start_pos[0] == 2
  137. def test_statically_nested_blocks():
  138. def build(code, depth):
  139. if depth == 0:
  140. return code
  141. new_code = 'if 1:\n' + indent(code)
  142. return build(new_code, depth - 1)
  143. def get_error(depth, add_func=False):
  144. code = build('foo', depth)
  145. if add_func:
  146. code = 'def bar():\n' + indent(code)
  147. errors = _get_error_list(code)
  148. if errors:
  149. assert errors[0].message == 'SyntaxError: too many statically nested blocks'
  150. return errors[0]
  151. return None
  152. assert get_error(19) is None
  153. assert get_error(19, add_func=True) is None
  154. assert get_error(20)
  155. assert get_error(20, add_func=True)
  156. def test_future_import_first():
  157. def is_issue(code, *args, **kwargs):
  158. code = code % args
  159. return bool(_get_error_list(code, **kwargs))
  160. i1 = 'from __future__ import division'
  161. i2 = 'from __future__ import absolute_import'
  162. i3 = 'from __future__ import annotations'
  163. assert not is_issue(i1)
  164. assert not is_issue(i1 + ';' + i2)
  165. assert not is_issue(i1 + '\n' + i2)
  166. assert not is_issue('"";' + i1)
  167. assert not is_issue('"";' + i1)
  168. assert not is_issue('""\n' + i1)
  169. assert not is_issue('""\n%s\n%s', i1, i2)
  170. assert not is_issue('""\n%s;%s', i1, i2)
  171. assert not is_issue('"";%s;%s ', i1, i2)
  172. assert not is_issue('"";%s\n%s ', i1, i2)
  173. assert not is_issue(i3, version="3.7")
  174. assert is_issue(i3, version="3.6")
  175. assert is_issue('1;' + i1)
  176. assert is_issue('1\n' + i1)
  177. assert is_issue('"";1\n' + i1)
  178. assert is_issue('""\n%s\nfrom x import a\n%s', i1, i2)
  179. assert is_issue('%s\n""\n%s', i1, i2)
  180. def test_named_argument_issues(works_not_in_py):
  181. message = works_not_in_py.get_error_message('def foo(*, **dict): pass')
  182. message = works_not_in_py.get_error_message('def foo(*): pass')
  183. if works_not_in_py.version.startswith('2'):
  184. assert message == 'SyntaxError: invalid syntax'
  185. else:
  186. assert message == 'SyntaxError: named arguments must follow bare *'
  187. works_not_in_py.assert_no_error_in_passing('def foo(*, name): pass')
  188. works_not_in_py.assert_no_error_in_passing('def foo(bar, *, name=1): pass')
  189. works_not_in_py.assert_no_error_in_passing('def foo(bar, *, name=1, **dct): pass')
  190. def test_escape_decode_literals(each_version):
  191. """
  192. We are using internal functions to assure that unicode/bytes escaping is
  193. without syntax errors. Here we make a bit of quality assurance that this
  194. works through versions, because the internal function might change over
  195. time.
  196. """
  197. def get_msg(end, to=1):
  198. base = "SyntaxError: (unicode error) 'unicodeescape' " \
  199. "codec can't decode bytes in position 0-%s: " % to
  200. return base + end
  201. def get_msgs(escape):
  202. return (get_msg('end of string in escape sequence'),
  203. get_msg(r"truncated %s escape" % escape))
  204. error, = _get_error_list(r'u"\x"', version=each_version)
  205. assert error.message in get_msgs(r'\xXX')
  206. error, = _get_error_list(r'u"\u"', version=each_version)
  207. assert error.message in get_msgs(r'\uXXXX')
  208. error, = _get_error_list(r'u"\U"', version=each_version)
  209. assert error.message in get_msgs(r'\UXXXXXXXX')
  210. error, = _get_error_list(r'u"\N{}"', version=each_version)
  211. assert error.message == get_msg(r'malformed \N character escape', to=2)
  212. error, = _get_error_list(r'u"\N{foo}"', version=each_version)
  213. assert error.message == get_msg(r'unknown Unicode character name', to=6)
  214. # Finally bytes.
  215. error, = _get_error_list(r'b"\x"', version=each_version)
  216. wanted = r'SyntaxError: (value error) invalid \x escape'
  217. if sys.version_info >= (3, 0):
  218. # The positioning information is only available in Python 3.
  219. wanted += ' at position 0'
  220. assert error.message == wanted
  221. def test_too_many_levels_of_indentation():
  222. assert not _get_error_list(build_nested('pass', 99))
  223. assert _get_error_list(build_nested('pass', 100))
  224. base = 'def x():\n if x:\n'
  225. assert not _get_error_list(build_nested('pass', 49, base=base))
  226. assert _get_error_list(build_nested('pass', 50, base=base))
  227. def test_paren_kwarg():
  228. assert _get_error_list("print((sep)=seperator)", version="3.8")
  229. assert not _get_error_list("print((sep)=seperator)", version="3.7")
  230. @pytest.mark.parametrize(
  231. 'code', [
  232. "f'{*args,}'",
  233. r'f"\""',
  234. r'f"\\\""',
  235. r'fr"\""',
  236. r'fr"\\\""',
  237. r"print(f'Some {x:.2f} and some {y}')",
  238. ]
  239. )
  240. def test_valid_fstrings(code):
  241. assert not _get_error_list(code, version='3.6')
  242. @pytest.mark.parametrize(
  243. 'code', [
  244. 'a = (b := 1)',
  245. '[x4 := x ** 5 for x in range(7)]',
  246. '[total := total + v for v in range(10)]',
  247. 'while chunk := file.read(2):\n pass',
  248. 'numbers = [y := math.factorial(x), y**2, y**3]',
  249. ]
  250. )
  251. def test_valid_namedexpr(code):
  252. assert not _get_error_list(code, version='3.8')
  253. @pytest.mark.parametrize(
  254. ('code', 'message'), [
  255. ("f'{1+}'", ('invalid syntax')),
  256. (r'f"\"', ('invalid syntax')),
  257. (r'fr"\"', ('invalid syntax')),
  258. ]
  259. )
  260. def test_invalid_fstrings(code, message):
  261. """
  262. Some fstring errors are handled differntly in 3.6 and other versions.
  263. Therefore check specifically for these errors here.
  264. """
  265. error, = _get_error_list(code, version='3.6')
  266. assert message in error.message
  267. @pytest.mark.parametrize(
  268. 'code', [
  269. "from foo import (\nbar,\n rab,\n)",
  270. "from foo import (bar, rab, )",
  271. ]
  272. )
  273. def test_trailing_comma(code):
  274. errors = _get_error_list(code)
  275. assert not errors
  276. def test_continue_in_finally():
  277. code = dedent('''\
  278. for a in [1]:
  279. try:
  280. pass
  281. finally:
  282. continue
  283. ''')
  284. assert not _get_error_list(code, version="3.8")
  285. assert _get_error_list(code, version="3.7")
  286. @pytest.mark.parametrize(
  287. 'template', [
  288. "a, b, {target}, c = d",
  289. "a, b, *{target}, c = d",
  290. "(a, *{target}), c = d",
  291. "for x, {target} in y: pass",
  292. "for x, q, {target} in y: pass",
  293. "for x, q, *{target} in y: pass",
  294. "for (x, *{target}), q in y: pass",
  295. ]
  296. )
  297. @pytest.mark.parametrize(
  298. 'target', [
  299. "True",
  300. "False",
  301. "None",
  302. "__debug__"
  303. ]
  304. )
  305. def test_forbidden_name(template, target):
  306. assert _get_error_list(template.format(target=target), version="3")
  307. def test_repeated_kwarg():
  308. # python 3.9+ shows which argument is repeated
  309. assert (
  310. _get_error_list("f(q=1, q=2)", version="3.8")[0].message
  311. == "SyntaxError: keyword argument repeated"
  312. )
  313. assert (
  314. _get_error_list("f(q=1, q=2)", version="3.9")[0].message
  315. == "SyntaxError: keyword argument repeated: q"
  316. )
  317. @pytest.mark.parametrize(
  318. ('source', 'no_errors'), [
  319. ('a(a for a in b,)', False),
  320. ('a(a for a in b, a)', False),
  321. ('a(a, a for a in b)', False),
  322. ('a(a, b, a for a in b, c, d)', False),
  323. ('a(a for a in b)', True),
  324. ('a((a for a in b), c)', True),
  325. ('a(c, (a for a in b))', True),
  326. ('a(a, b, (a for a in b), c, d)', True),
  327. ]
  328. )
  329. def test_unparenthesized_genexp(source, no_errors):
  330. assert bool(_get_error_list(source)) ^ no_errors
  331. @pytest.mark.parametrize(
  332. ('source', 'no_errors'), [
  333. ('*x = 2', False),
  334. ('(*y) = 1', False),
  335. ('((*z)) = 1', False),
  336. ('a, *b = 1', True),
  337. ('a, *b, c = 1', True),
  338. ('a, (*b), c = 1', True),
  339. ('a, ((*b)), c = 1', True),
  340. ('a, (*b, c), d = 1', True),
  341. ('[*(1,2,3)]', True),
  342. ('{*(1,2,3)}', True),
  343. ('[*(1,2,3),]', True),
  344. ('[*(1,2,3), *(4,5,6)]', True),
  345. ('[0, *(1,2,3)]', True),
  346. ('{*(1,2,3),}', True),
  347. ('{*(1,2,3), *(4,5,6)}', True),
  348. ('{0, *(4,5,6)}', True)
  349. ]
  350. )
  351. def test_starred_expr(source, no_errors):
  352. assert bool(_get_error_list(source, version="3")) ^ no_errors