failing_examples.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. # Mostly 3.6 relevant
  30. '[]: int',
  31. '[a, b]: int',
  32. '(): int',
  33. '(()): int',
  34. '((())): int',
  35. '{}: int',
  36. 'True: int',
  37. '(a, b): int',
  38. '*star,: int',
  39. 'a, b: int = 3',
  40. 'foo(+a=3)',
  41. 'f(lambda: 1=1)',
  42. 'f(x=1, x=2)',
  43. 'f(**x, y)',
  44. 'f(x=2, y)',
  45. 'f(**x, *y)',
  46. 'f(**x, y=3, z)',
  47. # augassign
  48. 'a, b += 3',
  49. '(a, b) += 3',
  50. '[a, b] += 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. '*a, *b = 3, 3',
  126. 'async def foo(): yield from []',
  127. 'yield from []',
  128. '*a = 3',
  129. 'del *a, b',
  130. 'def x(*): pass',
  131. '(%s *d) = x' % ('a,' * 256),
  132. '{**{} for a in [1]}',
  133. # Parser/tokenize.c
  134. r'"""',
  135. r'"',
  136. r"'''",
  137. r"'",
  138. r"\blub",
  139. # IndentationError: too many levels of indentation
  140. build_nested('pass', 100),
  141. # SyntaxErrors from Python/symtable.c
  142. 'def f(x, x): pass',
  143. 'nonlocal a',
  144. # IndentationError
  145. ' foo',
  146. 'def x():\n 1\n 2',
  147. 'def x():\n 1\n 2',
  148. 'if 1:\nfoo',
  149. 'if 1: blubb\nif 1:\npass\nTrue and False',
  150. # f-strings
  151. 'f"{}"',
  152. r'f"{\}"',
  153. 'f"{\'\\\'}"',
  154. 'f"{#}"',
  155. "f'{1!b}'",
  156. "f'{1:{5:{3}}}'",
  157. "f'{'",
  158. "f'{'",
  159. "f'}'",
  160. "f'{\"}'",
  161. "f'{\"}'",
  162. # Now nested parsing
  163. "f'{continue}'",
  164. "f'{1;1}'",
  165. "f'{a;}'",
  166. "f'{b\"\" \"\"}'",
  167. ]
  168. GLOBAL_NONLOCAL_ERROR = [
  169. dedent('''
  170. def glob():
  171. x = 3
  172. x.z
  173. global x'''),
  174. dedent('''
  175. def glob():
  176. x = 3
  177. global x'''),
  178. dedent('''
  179. def glob():
  180. x
  181. global x'''),
  182. dedent('''
  183. def glob():
  184. x = 3
  185. x.z
  186. nonlocal x'''),
  187. dedent('''
  188. def glob():
  189. x = 3
  190. nonlocal x'''),
  191. dedent('''
  192. def glob():
  193. x
  194. nonlocal x'''),
  195. # Annotation issues
  196. dedent('''
  197. def glob():
  198. x[0]: foo
  199. global x'''),
  200. dedent('''
  201. def glob():
  202. x.a: foo
  203. global x'''),
  204. dedent('''
  205. def glob():
  206. x: foo
  207. global x'''),
  208. dedent('''
  209. def glob():
  210. x: foo = 5
  211. global x'''),
  212. dedent('''
  213. def glob():
  214. x: foo = 5
  215. x
  216. global x'''),
  217. dedent('''
  218. def glob():
  219. global x
  220. x: foo = 3
  221. '''),
  222. # global/nonlocal + param
  223. dedent('''
  224. def glob(x):
  225. global x
  226. '''),
  227. dedent('''
  228. def glob(x):
  229. nonlocal x
  230. '''),
  231. dedent('''
  232. def x():
  233. a =3
  234. def z():
  235. nonlocal a
  236. a = 3
  237. nonlocal a
  238. '''),
  239. dedent('''
  240. def x():
  241. a = 4
  242. def y():
  243. global a
  244. nonlocal a
  245. '''),
  246. # Missing binding of nonlocal
  247. dedent('''
  248. def x():
  249. nonlocal a
  250. '''),
  251. dedent('''
  252. def x():
  253. def y():
  254. nonlocal a
  255. '''),
  256. dedent('''
  257. def x():
  258. a = 4
  259. def y():
  260. global a
  261. print(a)
  262. def z():
  263. nonlocal a
  264. '''),
  265. ]
  266. if sys.version_info >= (3, 6):
  267. FAILING_EXAMPLES += GLOBAL_NONLOCAL_ERROR
  268. if sys.version_info >= (3, 5):
  269. FAILING_EXAMPLES += [
  270. # Raises different errors so just ignore them for now.
  271. '[*[] for a in [1]]',
  272. # Raises multiple errors in previous versions.
  273. 'async def bla():\n def x(): await bla()',
  274. ]
  275. if sys.version_info >= (3, 4):
  276. # Before that del None works like del list, it gives a NameError.
  277. FAILING_EXAMPLES.append('del None')
  278. if sys.version_info >= (3,):
  279. FAILING_EXAMPLES += [
  280. # Unfortunately assigning to False and True do not raise an error in
  281. # 2.x.
  282. '(True,) = x',
  283. '([False], a) = x',
  284. # A symtable error that raises only a SyntaxWarning in Python 2.
  285. 'def x(): from math import *',
  286. # unicode chars in bytes are allowed in python 2
  287. 'b"ä"',
  288. # combining strings and unicode is allowed in Python 2.
  289. '"s" b""',
  290. '"s" b"" ""',
  291. 'b"" "" b"" ""',
  292. ]
  293. if sys.version_info >= (3, 6):
  294. FAILING_EXAMPLES += [
  295. # Same as above, but for f-strings.
  296. 'f"s" b""',
  297. 'b"s" f""',
  298. # f-string expression part cannot include a backslash
  299. r'''f"{'\n'}"''',
  300. ]
  301. FAILING_EXAMPLES.append('[a, 1] += 3')
  302. if sys.version_info[:2] == (3, 5):
  303. # yields are not allowed in 3.5 async functions. Therefore test them
  304. # separately, here.
  305. FAILING_EXAMPLES += [
  306. 'async def foo():\n yield x',
  307. 'async def foo():\n yield x',
  308. ]
  309. else:
  310. FAILING_EXAMPLES += [
  311. 'async def foo():\n yield x\n return 1',
  312. 'async def foo():\n yield x\n return 1',
  313. ]
  314. if sys.version_info[:2] <= (3, 4):
  315. # Python > 3.4 this is valid code.
  316. FAILING_EXAMPLES += [
  317. 'a = *[1], 2',
  318. '(*[1], 2)',
  319. ]
  320. if sys.version_info[:2] >= (3, 7):
  321. # This is somehow ok in previous versions.
  322. FAILING_EXAMPLES += [
  323. 'class X(base for base in bases): pass',
  324. ]
  325. if sys.version_info[:2] < (3, 8):
  326. FAILING_EXAMPLES += [
  327. # Python/compile.c
  328. dedent('''\
  329. for a in [1]:
  330. try:
  331. pass
  332. finally:
  333. continue
  334. '''), # 'continue' not supported inside 'finally' clause"
  335. ]
  336. if sys.version_info[:2] >= (3, 8):
  337. # assignment expressions from issue#89
  338. FAILING_EXAMPLES += [
  339. # Case 2
  340. '(lambda: x := 1)',
  341. '((lambda: x) := 1)',
  342. # Case 3
  343. '(a[i] := x)',
  344. '((a[i]) := x)',
  345. '(a(i) := x)',
  346. # Case 4
  347. '(a.b := c)',
  348. '[(i.i:= 0) for ((i), j) in range(5)]',
  349. # Case 5
  350. '[i:= 0 for i, j in range(5)]',
  351. '[(i:= 0) for ((i), j) in range(5)]',
  352. '[(i:= 0) for ((i), j), in range(5)]',
  353. '[(i:= 0) for ((i), j.i), in range(5)]',
  354. '[[(i:= i) for j in range(5)] for i in range(5)]',
  355. '[i for i, j in range(5) if True or (i:= 1)]',
  356. '[False and (i:= 0) for i, j in range(5)]',
  357. # Case 6
  358. '[i+1 for i in (i:= range(5))]',
  359. '[i+1 for i in (j:= range(5))]',
  360. '[i+1 for i in (lambda: (j:= range(5)))()]',
  361. # Case 7
  362. 'class Example:\n [(j := i) for i in range(5)]',
  363. # Not in that issue
  364. '(await a := x)',
  365. '((await a) := x)',
  366. # new discoveries
  367. '((a, b) := (1, 2))',
  368. '([a, b] := [1, 2])',
  369. '({a, b} := {1, 2})',
  370. '({a: b} := {1: 2})',
  371. '(a + b := 1)',
  372. '(True := 1)',
  373. '(False := 1)',
  374. '(None := 1)',
  375. '(__debug__ := 1)',
  376. ]