failing_examples.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. from textwrap import dedent
  4. def indent(code):
  5. lines = code.splitlines(True)
  6. return ''.join([' ' * 2 + line for line in lines])
  7. def build_nested(code, depth, base='def f():\n'):
  8. if depth == 0:
  9. return code
  10. new_code = base + indent(code)
  11. return build_nested(new_code, depth - 1, base=base)
  12. FAILING_EXAMPLES = [
  13. '1 +',
  14. '?',
  15. 'continue',
  16. 'break',
  17. 'return',
  18. 'yield',
  19. # SyntaxError from Python/ast.c
  20. 'f(x for x in bar, 1)',
  21. 'from foo import a,',
  22. 'from __future__ import whatever',
  23. 'from __future__ import braces',
  24. 'from .__future__ import whatever',
  25. 'def f(x=3, y): pass',
  26. 'lambda x=3, y: x',
  27. '__debug__ = 1',
  28. 'with x() as __debug__: pass',
  29. '[]: int',
  30. '[a, b]: int',
  31. '(): int',
  32. '(()): int',
  33. '((())): int',
  34. '{}: int',
  35. 'True: int',
  36. '(a, b): int',
  37. '*star,: int',
  38. 'a, b: int = 3',
  39. 'foo(+a=3)',
  40. 'f(lambda: 1=1)',
  41. 'f(x=1, x=2)',
  42. 'f(**x, y)',
  43. 'f(x=2, y)',
  44. 'f(**x, *y)',
  45. 'f(**x, y=3, z)',
  46. # augassign
  47. 'a, b += 3',
  48. '(a, b) += 3',
  49. '[a, b] += 3',
  50. '[a, 1] += 3',
  51. 'f() += 1',
  52. 'lambda x:None+=1',
  53. '{} += 1',
  54. '{a:b} += 1',
  55. '{1} += 1',
  56. '{*x} += 1',
  57. '(x,) += 1',
  58. '(x, y if a else q) += 1',
  59. '[] += 1',
  60. '[1,2] += 1',
  61. '[] += 1',
  62. 'None += 1',
  63. '... += 1',
  64. 'a > 1 += 1',
  65. '"test" += 1',
  66. '1 += 1',
  67. '1.0 += 1',
  68. '(yield) += 1',
  69. '(yield from x) += 1',
  70. '(x if x else y) += 1',
  71. 'a() += 1',
  72. 'a + b += 1',
  73. '+a += 1',
  74. 'a and b += 1',
  75. '*a += 1',
  76. 'a, b += 1',
  77. 'f"xxx" += 1',
  78. # All assignment tests
  79. 'lambda a: 1 = 1',
  80. '[x for x in y] = 1',
  81. '{x for x in y} = 1',
  82. '{x:x for x in y} = 1',
  83. '(x for x in y) = 1',
  84. 'None = 1',
  85. '... = 1',
  86. 'a == b = 1',
  87. '{a, b} = 1',
  88. '{a: b} = 1',
  89. '1 = 1',
  90. '"" = 1',
  91. 'b"" = 1',
  92. 'b"" = 1',
  93. '"" "" = 1',
  94. '1 | 1 = 3',
  95. '1**1 = 3',
  96. '~ 1 = 3',
  97. 'not 1 = 3',
  98. '1 and 1 = 3',
  99. 'def foo(): (yield 1) = 3',
  100. 'def foo(): x = yield 1 = 3',
  101. 'async def foo(): await x = 3',
  102. '(a if a else a) = a',
  103. 'a, 1 = x',
  104. 'foo() = 1',
  105. # Cases without the equals but other assignments.
  106. 'with x as foo(): pass',
  107. 'del bar, 1',
  108. 'for x, 1 in []: pass',
  109. 'for (not 1) in []: pass',
  110. '[x for 1 in y]',
  111. '[x for a, 3 in y]',
  112. '(x for 1 in y)',
  113. '{x for 1 in y}',
  114. '{x:x for 1 in y}',
  115. # Unicode/Bytes issues.
  116. r'u"\x"',
  117. r'u"\"',
  118. r'u"\u"',
  119. r'u"""\U"""',
  120. r'u"\Uffffffff"',
  121. r"u'''\N{}'''",
  122. r"u'\N{foo}'",
  123. r'b"\x"',
  124. r'b"\"',
  125. 'b"ä"',
  126. '*a, *b = 3, 3',
  127. 'async def foo(): yield from []',
  128. 'yield from []',
  129. '*a = 3',
  130. 'del *a, b',
  131. 'def x(*): pass',
  132. '(%s *d) = x' % ('a,' * 256),
  133. '{**{} for a in [1]}',
  134. '(True,) = x',
  135. '([False], a) = x',
  136. 'def x(): from math import *',
  137. # invalid del statements
  138. 'del x + y',
  139. 'del x(y)',
  140. 'async def foo(): del await x',
  141. 'def foo(): del (yield x)',
  142. 'del [x for x in range(10)]',
  143. 'del *x',
  144. 'del *x,',
  145. 'del (*x,)',
  146. 'del [*x]',
  147. 'del x, *y',
  148. 'del *x.y,',
  149. 'del *x[y],',
  150. 'del *x[y::], z',
  151. 'del x, (y, *z)',
  152. 'del (x, *[y, z])',
  153. 'del [x, *(y, [*z])]',
  154. 'del {}',
  155. 'del {x}',
  156. 'del {x, y}',
  157. 'del {x, *y}',
  158. # invalid starred expressions
  159. '*x',
  160. '(*x)',
  161. '((*x))',
  162. '1 + (*x)',
  163. '*x; 1',
  164. '1; *x',
  165. '1\n*x',
  166. 'x = *y',
  167. 'x: int = *y',
  168. 'def foo(): return *x',
  169. 'def foo(): yield *x',
  170. 'f"{*x}"',
  171. 'for *x in 1: pass',
  172. '[1 for *x in 1]',
  173. # str/bytes combinations
  174. '"s" b""',
  175. '"s" b"" ""',
  176. 'b"" "" b"" ""',
  177. 'f"s" b""',
  178. 'b"s" f""',
  179. # Parser/tokenize.c
  180. r'"""',
  181. r'"',
  182. r"'''",
  183. r"'",
  184. r"\blub",
  185. # IndentationError: too many levels of indentation
  186. build_nested('pass', 100),
  187. # SyntaxErrors from Python/symtable.c
  188. 'def f(x, x): pass',
  189. 'nonlocal a',
  190. # IndentationError
  191. ' foo',
  192. 'def x():\n 1\n 2',
  193. 'def x():\n 1\n 2',
  194. 'if 1:\nfoo',
  195. 'if 1: blubb\nif 1:\npass\nTrue and False',
  196. # f-strings
  197. 'f"{}"',
  198. r'f"{\}"',
  199. 'f"{\'\\\'}"',
  200. 'f"{#}"',
  201. "f'{1!b}'",
  202. "f'{1:{5:{3}}}'",
  203. "f'{'",
  204. "f'{'",
  205. "f'}'",
  206. "f'{\"}'",
  207. "f'{\"}'",
  208. # Now nested parsing
  209. "f'{continue}'",
  210. "f'{1;1}'",
  211. "f'{a;}'",
  212. "f'{b\"\" \"\"}'",
  213. # f-string expression part cannot include a backslash
  214. r'''f"{'\n'}"''',
  215. 'async def foo():\n yield x\n return 1',
  216. 'async def foo():\n yield x\n return 1',
  217. '[*[] for a in [1]]',
  218. 'async def bla():\n def x(): await bla()',
  219. 'del None',
  220. 'del True',
  221. 'del False',
  222. 'del ...',
  223. # Errors of global / nonlocal
  224. dedent('''
  225. def glob():
  226. x = 3
  227. x.z
  228. global x'''),
  229. dedent('''
  230. def glob():
  231. x = 3
  232. global x'''),
  233. dedent('''
  234. def glob():
  235. x
  236. global x'''),
  237. dedent('''
  238. def glob():
  239. x = 3
  240. x.z
  241. nonlocal x'''),
  242. dedent('''
  243. def glob():
  244. x = 3
  245. nonlocal x'''),
  246. dedent('''
  247. def glob():
  248. x
  249. nonlocal x'''),
  250. # Annotation issues
  251. dedent('''
  252. def glob():
  253. x[0]: foo
  254. global x'''),
  255. dedent('''
  256. def glob():
  257. x.a: foo
  258. global x'''),
  259. dedent('''
  260. def glob():
  261. x: foo
  262. global x'''),
  263. dedent('''
  264. def glob():
  265. x: foo = 5
  266. global x'''),
  267. dedent('''
  268. def glob():
  269. x: foo = 5
  270. x
  271. global x'''),
  272. dedent('''
  273. def glob():
  274. global x
  275. x: foo = 3
  276. '''),
  277. # global/nonlocal + param
  278. dedent('''
  279. def glob(x):
  280. global x
  281. '''),
  282. dedent('''
  283. def glob(x):
  284. nonlocal x
  285. '''),
  286. dedent('''
  287. def x():
  288. a =3
  289. def z():
  290. nonlocal a
  291. a = 3
  292. nonlocal a
  293. '''),
  294. dedent('''
  295. def x():
  296. a = 4
  297. def y():
  298. global a
  299. nonlocal a
  300. '''),
  301. # Missing binding of nonlocal
  302. dedent('''
  303. def x():
  304. nonlocal a
  305. '''),
  306. dedent('''
  307. def x():
  308. def y():
  309. nonlocal a
  310. '''),
  311. dedent('''
  312. def x():
  313. a = 4
  314. def y():
  315. global a
  316. print(a)
  317. def z():
  318. nonlocal a
  319. '''),
  320. # Name is assigned before nonlocal declaration
  321. dedent('''
  322. def x(a):
  323. def y():
  324. a = 10
  325. nonlocal a
  326. '''),
  327. ]
  328. if sys.version_info[:2] >= (3, 7):
  329. # This is somehow ok in previous versions.
  330. FAILING_EXAMPLES += [
  331. 'class X(base for base in bases): pass',
  332. ]
  333. if sys.version_info[:2] < (3, 8):
  334. FAILING_EXAMPLES += [
  335. # Python/compile.c
  336. dedent('''\
  337. for a in [1]:
  338. try:
  339. pass
  340. finally:
  341. continue
  342. '''), # 'continue' not supported inside 'finally' clause"
  343. ]
  344. if sys.version_info[:2] >= (3, 8):
  345. # assignment expressions from issue#89
  346. FAILING_EXAMPLES += [
  347. # Case 2
  348. '(lambda: x := 1)',
  349. '((lambda: x) := 1)',
  350. # Case 3
  351. '(a[i] := x)',
  352. '((a[i]) := x)',
  353. '(a(i) := x)',
  354. # Case 4
  355. '(a.b := c)',
  356. '[(i.i:= 0) for ((i), j) in range(5)]',
  357. # Case 5
  358. '[i:= 0 for i, j in range(5)]',
  359. '[(i:= 0) for ((i), j) in range(5)]',
  360. '[(i:= 0) for ((i), j), in range(5)]',
  361. '[(i:= 0) for ((i), j.i), in range(5)]',
  362. '[[(i:= i) for j in range(5)] for i in range(5)]',
  363. '[i for i, j in range(5) if True or (i:= 1)]',
  364. '[False and (i:= 0) for i, j in range(5)]',
  365. # Case 6
  366. '[i+1 for i in (i:= range(5))]',
  367. '[i+1 for i in (j:= range(5))]',
  368. '[i+1 for i in (lambda: (j:= range(5)))()]',
  369. # Case 7
  370. 'class Example:\n [(j := i) for i in range(5)]',
  371. # Not in that issue
  372. '(await a := x)',
  373. '((await a) := x)',
  374. # new discoveries
  375. '((a, b) := (1, 2))',
  376. '([a, b] := [1, 2])',
  377. '({a, b} := {1, 2})',
  378. '({a: b} := {1: 2})',
  379. '(a + b := 1)',
  380. '(True := 1)',
  381. '(False := 1)',
  382. '(None := 1)',
  383. '(__debug__ := 1)',
  384. # Unparenthesized walrus not allowed in dict literals, dict comprehensions and slices
  385. '{a:="a": b:=1}',
  386. '{y:=1: 2 for x in range(5)}',
  387. 'a[b:=0:1:2]',
  388. ]
  389. # f-string debugging syntax with invalid conversion character
  390. FAILING_EXAMPLES += [
  391. "f'{1=!b}'",
  392. ]