test_python_errors.py 15 KB

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