test_python_errors.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. """
  2. Testing if parso finds syntax errors and indentation errors.
  3. """
  4. import re
  5. import sys
  6. import warnings
  7. import pytest
  8. import parso
  9. from textwrap import dedent
  10. from parso._compatibility import is_pypy
  11. from .failing_examples import FAILING_EXAMPLES, indent, build_nested
  12. if is_pypy:
  13. # The errors in PyPy might be different. Just skip the module for now.
  14. pytestmark = pytest.mark.skip()
  15. def _get_error_list(code, version=None):
  16. grammar = parso.load_grammar(version=version)
  17. tree = grammar.parse(code)
  18. return list(grammar.iter_errors(tree))
  19. def assert_comparison(code, error_code, positions):
  20. errors = [(error.start_pos, error.code) for error in _get_error_list(code)]
  21. assert [(pos, error_code) for pos in positions] == errors
  22. @pytest.mark.skipif(sys.version_info >= (3, 10), reason="parso don't support Python 3.10 yet")
  23. @pytest.mark.parametrize('code', FAILING_EXAMPLES)
  24. def test_python_exception_matches(code):
  25. wanted, line_nr = _get_actual_exception(code)
  26. errors = _get_error_list(code)
  27. actual = None
  28. if errors:
  29. error, = errors
  30. actual = error.message
  31. assert actual in wanted
  32. # Somehow in Python2.7 the SyntaxError().lineno is sometimes None
  33. assert line_nr is None or line_nr == error.start_pos[0]
  34. def test_non_async_in_async():
  35. """
  36. This example doesn't work with FAILING_EXAMPLES, because the line numbers
  37. are not always the same / incorrect in Python 3.8.
  38. """
  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] not in ((3, 8), (3, 9)):
  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/3.9
  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: assignment to keyword':
  101. return [wanted, "SyntaxError: can't assign to keyword",
  102. 'SyntaxError: cannot assign to __debug__'], line_nr
  103. elif wanted == 'SyntaxError: f-string: unterminated string':
  104. wanted = 'SyntaxError: EOL while scanning string literal'
  105. elif wanted == 'SyntaxError: f-string expression part cannot include a backslash':
  106. return [
  107. wanted,
  108. "SyntaxError: EOL while scanning string literal",
  109. "SyntaxError: unexpected character after line continuation character",
  110. ], line_nr
  111. elif wanted == "SyntaxError: f-string: expecting '}'":
  112. wanted = 'SyntaxError: EOL while scanning string literal'
  113. elif wanted == 'SyntaxError: f-string: empty expression not allowed':
  114. wanted = 'SyntaxError: invalid syntax'
  115. elif wanted == "SyntaxError: f-string expression part cannot include '#'":
  116. wanted = 'SyntaxError: invalid syntax'
  117. elif wanted == "SyntaxError: f-string: single '}' is not allowed":
  118. wanted = 'SyntaxError: invalid syntax'
  119. elif "Maybe you meant '==' instead of '='?" in wanted:
  120. wanted = wanted.removesuffix(" here. Maybe you meant '==' instead of '='?")
  121. elif re.match(
  122. r"SyntaxError: unterminated string literal \(detected at line \d+\)", wanted
  123. ):
  124. wanted = "SyntaxError: EOL while scanning string literal"
  125. elif re.match(
  126. r"SyntaxError: unterminated triple-quoted string literal \(detected at line \d+\)",
  127. wanted,
  128. ):
  129. wanted = 'SyntaxError: EOF while scanning triple-quoted string literal'
  130. elif wanted == 'SyntaxError: cannot use starred expression here':
  131. wanted = "SyntaxError: can't use starred expression here"
  132. elif wanted == 'SyntaxError: f-string: cannot use starred expression here':
  133. wanted = "SyntaxError: f-string: can't use starred expression here"
  134. elif re.match(
  135. r"IndentationError: expected an indented block after '[^']*' statement on line \d",
  136. wanted,
  137. ):
  138. wanted = 'IndentationError: expected an indented block'
  139. elif wanted == 'SyntaxError: unterminated string literal':
  140. wanted = 'SyntaxError: EOL while scanning string literal'
  141. return [wanted], line_nr
  142. def test_default_except_error_postition():
  143. # For this error the position seemed to be one line off in Python < 3.10,
  144. # but that doesn't really matter.
  145. code = 'try: pass\nexcept: pass\nexcept X: pass'
  146. wanted, line_nr = _get_actual_exception(code)
  147. error, = _get_error_list(code)
  148. assert error.message in wanted
  149. if sys.version_info[:2] >= (3, 10):
  150. assert line_nr == error.start_pos[0]
  151. else:
  152. assert line_nr != error.start_pos[0]
  153. # I think this is the better position.
  154. assert error.start_pos[0] == 2
  155. def test_statically_nested_blocks():
  156. def build(code, depth):
  157. if depth == 0:
  158. return code
  159. new_code = 'if 1:\n' + indent(code)
  160. return build(new_code, depth - 1)
  161. def get_error(depth, add_func=False):
  162. code = build('foo', depth)
  163. if add_func:
  164. code = 'def bar():\n' + indent(code)
  165. errors = _get_error_list(code)
  166. if errors:
  167. assert errors[0].message == 'SyntaxError: too many statically nested blocks'
  168. return errors[0]
  169. return None
  170. assert get_error(19) is None
  171. assert get_error(19, add_func=True) is None
  172. assert get_error(20)
  173. assert get_error(20, add_func=True)
  174. def test_future_import_first():
  175. def is_issue(code, *args, **kwargs):
  176. code = code % args
  177. return bool(_get_error_list(code, **kwargs))
  178. i1 = 'from __future__ import division'
  179. i2 = 'from __future__ import absolute_import'
  180. i3 = 'from __future__ import annotations'
  181. assert not is_issue(i1)
  182. assert not is_issue(i1 + ';' + i2)
  183. assert not is_issue(i1 + '\n' + i2)
  184. assert not is_issue('"";' + i1)
  185. assert not is_issue('"";' + i1)
  186. assert not is_issue('""\n' + i1)
  187. assert not is_issue('""\n%s\n%s', i1, i2)
  188. assert not is_issue('""\n%s;%s', i1, i2)
  189. assert not is_issue('"";%s;%s ', i1, i2)
  190. assert not is_issue('"";%s\n%s ', i1, i2)
  191. assert not is_issue(i3, version="3.7")
  192. assert is_issue(i3, version="3.6")
  193. assert is_issue('1;' + i1)
  194. assert is_issue('1\n' + i1)
  195. assert is_issue('"";1\n' + i1)
  196. assert is_issue('""\n%s\nfrom x import a\n%s', i1, i2)
  197. assert is_issue('%s\n""\n%s', i1, i2)
  198. def test_named_argument_issues(works_not_in_py):
  199. message = works_not_in_py.get_error_message('def foo(*, **dict): pass')
  200. message = works_not_in_py.get_error_message('def foo(*): pass')
  201. if works_not_in_py.version.startswith('2'):
  202. assert message == 'SyntaxError: invalid syntax'
  203. else:
  204. assert message == 'SyntaxError: named arguments must follow bare *'
  205. works_not_in_py.assert_no_error_in_passing('def foo(*, name): pass')
  206. works_not_in_py.assert_no_error_in_passing('def foo(bar, *, name=1): pass')
  207. works_not_in_py.assert_no_error_in_passing('def foo(bar, *, name=1, **dct): pass')
  208. def test_escape_decode_literals(each_version):
  209. """
  210. We are using internal functions to assure that unicode/bytes escaping is
  211. without syntax errors. Here we make a bit of quality assurance that this
  212. works through versions, because the internal function might change over
  213. time.
  214. """
  215. def get_msg(end, to=1):
  216. base = "SyntaxError: (unicode error) 'unicodeescape' " \
  217. "codec can't decode bytes in position 0-%s: " % to
  218. return base + end
  219. def get_msgs(escape):
  220. return (get_msg('end of string in escape sequence'),
  221. get_msg(r"truncated %s escape" % escape))
  222. error, = _get_error_list(r'u"\x"', version=each_version)
  223. assert error.message in get_msgs(r'\xXX')
  224. error, = _get_error_list(r'u"\u"', version=each_version)
  225. assert error.message in get_msgs(r'\uXXXX')
  226. error, = _get_error_list(r'u"\U"', version=each_version)
  227. assert error.message in get_msgs(r'\UXXXXXXXX')
  228. error, = _get_error_list(r'u"\N{}"', version=each_version)
  229. assert error.message == get_msg(r'malformed \N character escape', to=2)
  230. error, = _get_error_list(r'u"\N{foo}"', version=each_version)
  231. assert error.message == get_msg(r'unknown Unicode character name', to=6)
  232. # Finally bytes.
  233. error, = _get_error_list(r'b"\x"', version=each_version)
  234. wanted = r'SyntaxError: (value error) invalid \x escape at position 0'
  235. assert error.message == wanted
  236. def test_too_many_levels_of_indentation():
  237. assert not _get_error_list(build_nested('pass', 99))
  238. assert _get_error_list(build_nested('pass', 100))
  239. base = 'def x():\n if x:\n'
  240. assert not _get_error_list(build_nested('pass', 49, base=base))
  241. assert _get_error_list(build_nested('pass', 50, base=base))
  242. def test_paren_kwarg():
  243. assert _get_error_list("print((sep)=seperator)", version="3.8")
  244. assert not _get_error_list("print((sep)=seperator)", version="3.7")
  245. @pytest.mark.parametrize(
  246. 'code', [
  247. "f'{*args,}'",
  248. r'f"\""',
  249. r'f"\\\""',
  250. r'fr"\""',
  251. r'fr"\\\""',
  252. r"print(f'Some {x:.2f} and some {y}')",
  253. # Unparenthesized yield expression
  254. 'def foo(): return f"{yield 1}"',
  255. ]
  256. )
  257. def test_valid_fstrings(code):
  258. assert not _get_error_list(code, version='3.6')
  259. @pytest.mark.parametrize(
  260. 'code', [
  261. 'a = (b := 1)',
  262. '[x4 := x ** 5 for x in range(7)]',
  263. '[total := total + v for v in range(10)]',
  264. 'while chunk := file.read(2):\n pass',
  265. 'numbers = [y := math.factorial(x), y**2, y**3]',
  266. '{(a:="a"): (b:=1)}',
  267. '{(y:=1): 2 for x in range(5)}',
  268. 'a[(b:=0)]',
  269. 'a[(b:=0, c:=0)]',
  270. 'a[(b:=0):1:2]',
  271. ]
  272. )
  273. def test_valid_namedexpr(code):
  274. assert not _get_error_list(code, version='3.8')
  275. @pytest.mark.parametrize(
  276. 'code', [
  277. '{x := 1, 2, 3}',
  278. '{x4 := x ** 5 for x in range(7)}',
  279. ]
  280. )
  281. def test_valid_namedexpr_set(code):
  282. assert not _get_error_list(code, version='3.9')
  283. @pytest.mark.parametrize(
  284. 'code', [
  285. 'a[b:=0]',
  286. 'a[b:=0, c:=0]',
  287. ]
  288. )
  289. def test_valid_namedexpr_index(code):
  290. assert not _get_error_list(code, version='3.10')
  291. @pytest.mark.parametrize(
  292. ('code', 'message'), [
  293. ("f'{1+}'", ('invalid syntax')),
  294. (r'f"\"', ('invalid syntax')),
  295. (r'fr"\"', ('invalid syntax')),
  296. ]
  297. )
  298. def test_invalid_fstrings(code, message):
  299. """
  300. Some fstring errors are handled differntly in 3.6 and other versions.
  301. Therefore check specifically for these errors here.
  302. """
  303. error, = _get_error_list(code, version='3.6')
  304. assert message in error.message
  305. @pytest.mark.parametrize(
  306. 'code', [
  307. "from foo import (\nbar,\n rab,\n)",
  308. "from foo import (bar, rab, )",
  309. ]
  310. )
  311. def test_trailing_comma(code):
  312. errors = _get_error_list(code)
  313. assert not errors
  314. def test_continue_in_finally():
  315. code = dedent('''\
  316. for a in [1]:
  317. try:
  318. pass
  319. finally:
  320. continue
  321. ''')
  322. assert not _get_error_list(code, version="3.8")
  323. assert _get_error_list(code, version="3.7")
  324. @pytest.mark.parametrize(
  325. 'template', [
  326. "a, b, {target}, c = d",
  327. "a, b, *{target}, c = d",
  328. "(a, *{target}), c = d",
  329. "for x, {target} in y: pass",
  330. "for x, q, {target} in y: pass",
  331. "for x, q, *{target} in y: pass",
  332. "for (x, *{target}), q in y: pass",
  333. ]
  334. )
  335. @pytest.mark.parametrize(
  336. 'target', [
  337. "True",
  338. "False",
  339. "None",
  340. "__debug__"
  341. ]
  342. )
  343. def test_forbidden_name(template, target):
  344. assert _get_error_list(template.format(target=target), version="3")
  345. def test_repeated_kwarg():
  346. # python 3.9+ shows which argument is repeated
  347. assert (
  348. _get_error_list("f(q=1, q=2)", version="3.8")[0].message
  349. == "SyntaxError: keyword argument repeated"
  350. )
  351. assert (
  352. _get_error_list("f(q=1, q=2)", version="3.9")[0].message
  353. == "SyntaxError: keyword argument repeated: q"
  354. )
  355. @pytest.mark.parametrize(
  356. ('source', 'no_errors'), [
  357. ('a(a for a in b,)', False),
  358. ('a(a for a in b, a)', False),
  359. ('a(a, a for a in b)', False),
  360. ('a(a, b, a for a in b, c, d)', False),
  361. ('a(a for a in b)', True),
  362. ('a((a for a in b), c)', True),
  363. ('a(c, (a for a in b))', True),
  364. ('a(a, b, (a for a in b), c, d)', True),
  365. ]
  366. )
  367. def test_unparenthesized_genexp(source, no_errors):
  368. assert bool(_get_error_list(source)) ^ no_errors
  369. @pytest.mark.parametrize(
  370. ('source', 'no_errors'), [
  371. ('*x = 2', False),
  372. ('(*y) = 1', False),
  373. ('((*z)) = 1', False),
  374. ('*a,', True),
  375. ('*a, = 1', True),
  376. ('(*a,)', True),
  377. ('(*a,) = 1', True),
  378. ('[*a]', True),
  379. ('[*a] = 1', True),
  380. ('a, *b', True),
  381. ('a, *b = 1', True),
  382. ('a, *b, c', True),
  383. ('a, *b, c = 1', True),
  384. ('a, (*b, c), d', True),
  385. ('a, (*b, c), d = 1', True),
  386. ('*a.b,', True),
  387. ('*a.b, = 1', True),
  388. ('*a[b],', True),
  389. ('*a[b], = 1', True),
  390. ('*a[b::], c', True),
  391. ('*a[b::], c = 1', True),
  392. ('(a, *[b, c])', True),
  393. ('(a, *[b, c]) = 1', True),
  394. ('[a, *(b, [*c])]', True),
  395. ('[a, *(b, [*c])] = 1', True),
  396. ('[*(1,2,3)]', True),
  397. ('{*(1,2,3)}', True),
  398. ('[*(1,2,3),]', True),
  399. ('[*(1,2,3), *(4,5,6)]', True),
  400. ('[0, *(1,2,3)]', True),
  401. ('{*(1,2,3),}', True),
  402. ('{*(1,2,3), *(4,5,6)}', True),
  403. ('{0, *(4,5,6)}', True)
  404. ]
  405. )
  406. def test_starred_expr(source, no_errors):
  407. assert bool(_get_error_list(source, version="3")) ^ no_errors
  408. @pytest.mark.parametrize(
  409. 'code', [
  410. 'a, (*b), c',
  411. 'a, (*b), c = 1',
  412. 'a, ((*b)), c',
  413. 'a, ((*b)), c = 1',
  414. ]
  415. )
  416. def test_parenthesized_single_starred_expr(code):
  417. assert not _get_error_list(code, version='3.8')
  418. assert _get_error_list(code, version='3.9')
  419. @pytest.mark.parametrize(
  420. 'code', [
  421. '() = ()',
  422. '() = []',
  423. '[] = ()',
  424. '[] = []',
  425. ]
  426. )
  427. def test_valid_empty_assignment(code):
  428. assert not _get_error_list(code)
  429. @pytest.mark.parametrize(
  430. 'code', [
  431. 'del ()',
  432. 'del []',
  433. 'del x',
  434. 'del x,',
  435. 'del x, y',
  436. 'del (x, y)',
  437. 'del [x, y]',
  438. 'del (x, [y, z])',
  439. 'del x.y, x[y]',
  440. 'del f(x)[y::]',
  441. 'del x[[*y]]',
  442. 'del x[[*y]::]',
  443. ]
  444. )
  445. def test_valid_del(code):
  446. assert not _get_error_list(code)
  447. @pytest.mark.parametrize(
  448. ('source', 'version', 'no_errors'), [
  449. ('[x for x in range(10) if lambda: 1]', '3.8', True),
  450. ('[x for x in range(10) if lambda: 1]', '3.9', False),
  451. ('[x for x in range(10) if (lambda: 1)]', '3.9', True),
  452. ]
  453. )
  454. def test_lambda_in_comp_if(source, version, no_errors):
  455. assert bool(_get_error_list(source, version=version)) ^ no_errors