Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 1 | # Test various flavors of legal and illegal future statements |
| 2 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 5 | from textwrap import dedent |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 6 | import os |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 7 | import re |
| 8 | |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 9 | rx = re.compile(r'\((\S+).py, line (\d+)') |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 10 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 11 | def get_error_location(msg): |
| 12 | mo = rx.search(str(msg)) |
| 13 | return mo.group(1, 2) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 14 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 15 | class FutureTest(unittest.TestCase): |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 16 | |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 17 | def check_syntax_error(self, err, basename, lineno, offset=1): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 18 | self.assertIn('%s.py, line %d' % (basename, lineno), str(err)) |
| 19 | self.assertEqual(os.path.basename(err.filename), basename + '.py') |
| 20 | self.assertEqual(err.lineno, lineno) |
| 21 | self.assertEqual(err.offset, offset) |
| 22 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 23 | def test_future1(self): |
Ezio Melotti | 1ed6be3 | 2013-02-27 10:00:03 +0200 | [diff] [blame] | 24 | with support.CleanImport('future_test1'): |
| 25 | from test import future_test1 |
| 26 | self.assertEqual(future_test1.result, 6) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 27 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 28 | def test_future2(self): |
Ezio Melotti | 1ed6be3 | 2013-02-27 10:00:03 +0200 | [diff] [blame] | 29 | with support.CleanImport('future_test2'): |
| 30 | from test import future_test2 |
| 31 | self.assertEqual(future_test2.result, 6) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 32 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 33 | def test_future3(self): |
Ezio Melotti | 1ed6be3 | 2013-02-27 10:00:03 +0200 | [diff] [blame] | 34 | with support.CleanImport('test_future3'): |
| 35 | from test import test_future3 |
Jeremy Hylton | 8471a35 | 2001-08-20 20:33:42 +0000 | [diff] [blame] | 36 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 37 | def test_badfuture3(self): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 38 | with self.assertRaises(SyntaxError) as cm: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 39 | from test import badsyntax_future3 |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 40 | self.check_syntax_error(cm.exception, "badsyntax_future3", 3) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 41 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 42 | def test_badfuture4(self): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 43 | with self.assertRaises(SyntaxError) as cm: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 44 | from test import badsyntax_future4 |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 45 | self.check_syntax_error(cm.exception, "badsyntax_future4", 3) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 46 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 47 | def test_badfuture5(self): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 48 | with self.assertRaises(SyntaxError) as cm: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 49 | from test import badsyntax_future5 |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 50 | self.check_syntax_error(cm.exception, "badsyntax_future5", 4) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 51 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 52 | def test_badfuture6(self): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 53 | with self.assertRaises(SyntaxError) as cm: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 54 | from test import badsyntax_future6 |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 55 | self.check_syntax_error(cm.exception, "badsyntax_future6", 3) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 56 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 57 | def test_badfuture7(self): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 58 | with self.assertRaises(SyntaxError) as cm: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 59 | from test import badsyntax_future7 |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 60 | self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 53) |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 61 | |
| 62 | def test_badfuture8(self): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 63 | with self.assertRaises(SyntaxError) as cm: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 64 | from test import badsyntax_future8 |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 65 | self.check_syntax_error(cm.exception, "badsyntax_future8", 3) |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 66 | |
| 67 | def test_badfuture9(self): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 68 | with self.assertRaises(SyntaxError) as cm: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 69 | from test import badsyntax_future9 |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 70 | self.check_syntax_error(cm.exception, "badsyntax_future9", 3) |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 71 | |
Benjamin Peterson | 2d6acd2 | 2013-03-16 09:15:47 -0700 | [diff] [blame] | 72 | def test_badfuture10(self): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 73 | with self.assertRaises(SyntaxError) as cm: |
Benjamin Peterson | 2d6acd2 | 2013-03-16 09:15:47 -0700 | [diff] [blame] | 74 | from test import badsyntax_future10 |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 75 | self.check_syntax_error(cm.exception, "badsyntax_future10", 3) |
Benjamin Peterson | 2d6acd2 | 2013-03-16 09:15:47 -0700 | [diff] [blame] | 76 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 77 | def test_parserhack(self): |
| 78 | # test that the parser.c::future_hack function works as expected |
| 79 | # Note: although this test must pass, it's not testing the original |
| 80 | # bug as of 2.6 since the with statement is not optional and |
| 81 | # the parser hack disabled. If a new keyword is introduced in |
| 82 | # 2.6, change this to refer to the new future import. |
| 83 | try: |
Benjamin Peterson | 9aebc61 | 2008-10-26 20:58:53 +0000 | [diff] [blame] | 84 | exec("from __future__ import print_function; print 0") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 85 | except SyntaxError: |
| 86 | pass |
| 87 | else: |
| 88 | self.fail("syntax error didn't occur") |
| 89 | |
| 90 | try: |
Benjamin Peterson | 9aebc61 | 2008-10-26 20:58:53 +0000 | [diff] [blame] | 91 | exec("from __future__ import (print_function); print 0") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 92 | except SyntaxError: |
| 93 | pass |
| 94 | else: |
| 95 | self.fail("syntax error didn't occur") |
| 96 | |
Benjamin Peterson | 9aebc61 | 2008-10-26 20:58:53 +0000 | [diff] [blame] | 97 | def test_multiple_features(self): |
Ezio Melotti | 1ed6be3 | 2013-02-27 10:00:03 +0200 | [diff] [blame] | 98 | with support.CleanImport("test.test_future5"): |
| 99 | from test import test_future5 |
Benjamin Peterson | 9aebc61 | 2008-10-26 20:58:53 +0000 | [diff] [blame] | 100 | |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 101 | def test_unicode_literals_exec(self): |
| 102 | scope = {} |
| 103 | exec("from __future__ import unicode_literals; x = ''", {}, scope) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 104 | self.assertIsInstance(scope["x"], str) |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 106 | class AnnotationsFutureTestCase(unittest.TestCase): |
| 107 | template = dedent( |
| 108 | """ |
| 109 | from __future__ import annotations |
| 110 | def f() -> {ann}: |
| 111 | ... |
| 112 | def g(arg: {ann}) -> None: |
| 113 | ... |
| 114 | var: {ann} |
| 115 | var2: {ann} = None |
| 116 | """ |
| 117 | ) |
| 118 | |
| 119 | def getActual(self, annotation): |
| 120 | scope = {} |
| 121 | exec(self.template.format(ann=annotation), {}, scope) |
| 122 | func_ret_ann = scope['f'].__annotations__['return'] |
| 123 | func_arg_ann = scope['g'].__annotations__['arg'] |
| 124 | var_ann1 = scope['__annotations__']['var'] |
| 125 | var_ann2 = scope['__annotations__']['var2'] |
| 126 | self.assertEqual(func_ret_ann, func_arg_ann) |
| 127 | self.assertEqual(func_ret_ann, var_ann1) |
| 128 | self.assertEqual(func_ret_ann, var_ann2) |
| 129 | return func_ret_ann |
| 130 | |
| 131 | def assertAnnotationEqual( |
| 132 | self, annotation, expected=None, drop_parens=False, is_tuple=False, |
| 133 | ): |
| 134 | actual = self.getActual(annotation) |
| 135 | if expected is None: |
| 136 | expected = annotation if not is_tuple else annotation[1:-1] |
| 137 | if drop_parens: |
| 138 | self.assertNotEqual(actual, expected) |
| 139 | actual = actual.replace("(", "").replace(")", "") |
| 140 | |
| 141 | self.assertEqual(actual, expected) |
| 142 | |
| 143 | def test_annotations(self): |
| 144 | eq = self.assertAnnotationEqual |
| 145 | eq('...') |
| 146 | eq("'some_string'") |
| 147 | eq("b'\\xa3'") |
| 148 | eq('Name') |
| 149 | eq('None') |
| 150 | eq('True') |
| 151 | eq('False') |
| 152 | eq('1') |
| 153 | eq('1.0') |
| 154 | eq('1j') |
| 155 | eq('True or False') |
| 156 | eq('True or False or None') |
| 157 | eq('True and False') |
| 158 | eq('True and False and None') |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 159 | eq('Name1 and Name2 or Name3') |
| 160 | eq('Name1 and (Name2 or Name3)') |
| 161 | eq('Name1 or Name2 and Name3') |
| 162 | eq('(Name1 or Name2) and Name3') |
| 163 | eq('Name1 and Name2 or Name3 and Name4') |
| 164 | eq('Name1 or Name2 and Name3 or Name4') |
| 165 | eq('a + b + (c + d)') |
| 166 | eq('a * b * (c * d)') |
| 167 | eq('(a ** b) ** c ** d') |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 168 | eq('v1 << 2') |
| 169 | eq('1 >> v2') |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 170 | eq('1 % finished') |
| 171 | eq('1 + v2 - v3 * 4 ^ 5 ** v6 / 7 // 8') |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 172 | eq('not great') |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 173 | eq('not not great') |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 174 | eq('~great') |
| 175 | eq('+value') |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 176 | eq('++value') |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 177 | eq('-1') |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 178 | eq('~int and not v1 ^ 123 + v2 | True') |
| 179 | eq('a + (not b)') |
Serhiy Storchaka | 2a2940e | 2018-09-30 21:07:05 +0300 | [diff] [blame] | 180 | eq('lambda: None') |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 181 | eq('lambda arg: None') |
| 182 | eq('lambda a=True: a') |
| 183 | eq('lambda a, b, c=True: a') |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 184 | eq("lambda a, b, c=True, *, d=1 << v2, e='str': a") |
Serhiy Storchaka | 2a2940e | 2018-09-30 21:07:05 +0300 | [diff] [blame] | 185 | eq("lambda a, b, c=True, *vararg, d, e='str', **kwargs: a + b") |
Pablo Galindo | da6129e | 2019-05-18 23:40:22 +0100 | [diff] [blame] | 186 | eq("lambda a, /, b, c=True, *vararg, d, e='str', **kwargs: a + b") |
| 187 | eq('lambda x, /: x') |
| 188 | eq('lambda x=1, /: x') |
| 189 | eq('lambda x, /, y: x + y') |
| 190 | eq('lambda x=1, /, y=2: x + y') |
| 191 | eq('lambda x, /, y=1: x + y') |
| 192 | eq('lambda x, /, y=1, *, z=3: x + y + z') |
| 193 | eq('lambda x=1, /, y=2, *, z=3: x + y + z') |
| 194 | eq('lambda x=1, /, y=2, *, z: x + y + z') |
| 195 | eq('lambda x=1, y=2, z=3, /, w=4, *, l, l2: x + y + z + w + l + l2') |
| 196 | eq('lambda x=1, y=2, z=3, /, w=4, *, l, l2, **kwargs: x + y + z + w + l + l2') |
| 197 | eq('lambda x, /, y=1, *, z: x + y + z') |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 198 | eq('lambda x: lambda y: x + y') |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 199 | eq('1 if True else 2') |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 200 | eq('str or None if int or True else str or bytes or None') |
| 201 | eq('str or None if (1 if True else 2) else str or bytes or None') |
| 202 | eq("0 if not x else 1 if x > 0 else -1") |
| 203 | eq("(1 if x > 0 else -1) if x else 0") |
| 204 | eq("{'2.7': dead, '3.7': long_live or die_hard}") |
| 205 | eq("{'2.7': dead, '3.7': long_live or die_hard, **{'3.6': verygood}}") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 206 | eq("{**a, **b, **c}") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 207 | eq("{'2.7', '3.6', '3.7', '3.8', '3.9', '4.0' if gilectomy else '3.10'}") |
| 208 | eq("{*a, *b, *c}") |
| 209 | eq("({'a': 'b'}, True or False, +value, 'string', b'bytes') or None") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 210 | eq("()") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 211 | eq("(a,)") |
| 212 | eq("(a, b)") |
| 213 | eq("(a, b, c)") |
| 214 | eq("(*a, *b, *c)") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 215 | eq("[]") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 216 | eq("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or A, 11 or B, 12 or C]") |
| 217 | eq("[*a, *b, *c]") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 218 | eq("{i for i in (1, 2, 3)}") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 219 | eq("{i ** 2 for i in (1, 2, 3)}") |
| 220 | eq("{i ** 2 for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))}") |
| 221 | eq("{i ** 2 + j for i in (1, 2, 3) for j in (1, 2, 3)}") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 222 | eq("[i for i in (1, 2, 3)]") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 223 | eq("[i ** 2 for i in (1, 2, 3)]") |
| 224 | eq("[i ** 2 for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))]") |
| 225 | eq("[i ** 2 + j for i in (1, 2, 3) for j in (1, 2, 3)]") |
| 226 | eq("(i for i in (1, 2, 3))") |
| 227 | eq("(i ** 2 for i in (1, 2, 3))") |
| 228 | eq("(i ** 2 for i, _ in ((1, 'a'), (2, 'b'), (3, 'c')))") |
| 229 | eq("(i ** 2 + j for i in (1, 2, 3) for j in (1, 2, 3))") |
| 230 | eq("{i: 0 for i in (1, 2, 3)}") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 231 | eq("{i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 232 | eq("[(x, y) for x, y in (a, b)]") |
| 233 | eq("[(x,) for x, in (a,)]") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 234 | eq("Python3 > Python2 > COBOL") |
| 235 | eq("Life is Life") |
| 236 | eq("call()") |
| 237 | eq("call(arg)") |
| 238 | eq("call(kwarg='hey')") |
| 239 | eq("call(arg, kwarg='hey')") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 240 | eq("call(arg, *args, another, kwarg='hey')") |
| 241 | eq("call(arg, another, kwarg='hey', **kwargs, kwarg2='ho')") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 242 | eq("lukasz.langa.pl") |
| 243 | eq("call.me(maybe)") |
| 244 | eq("1 .real") |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 245 | eq("1.0.real") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 246 | eq("....__class__") |
| 247 | eq("list[str]") |
| 248 | eq("dict[str, int]") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 249 | eq("set[str,]") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 250 | eq("tuple[str, ...]") |
| 251 | eq("tuple[str, int, float, dict[str, int]]") |
| 252 | eq("slice[0]") |
| 253 | eq("slice[0:1]") |
| 254 | eq("slice[0:1:2]") |
| 255 | eq("slice[:]") |
| 256 | eq("slice[:-1]") |
| 257 | eq("slice[1:]") |
| 258 | eq("slice[::-1]") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 259 | eq("slice[()]") |
| 260 | eq("slice[a, b:c, d:e:f]") |
| 261 | eq("slice[(x for x in a)]") |
| 262 | eq('str or None if sys.version_info[0] > (3,) else str or bytes or None') |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 263 | eq("f'f-string without formatted values is just a string'") |
| 264 | eq("f'{{NOT a formatted value}}'") |
| 265 | eq("f'some f-string with {a} {few():.2f} {formatted.values!r}'") |
| 266 | eq('''f"{f'{nested} inner'} outer"''') |
| 267 | eq("f'space between opening braces: { {a for a in (1, 2, 3)}}'") |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 268 | eq("f'{(lambda x: x)}'") |
| 269 | eq("f'{(None if a else lambda x: x)}'") |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 270 | eq("f'{x}'") |
| 271 | eq("f'{x!r}'") |
| 272 | eq("f'{x!a}'") |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 273 | eq('(yield from outside_of_generator)') |
| 274 | eq('(yield)') |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 275 | eq('(yield a + b)') |
| 276 | eq('await some.complicated[0].call(with_args=True or 1 is not 1)') |
| 277 | eq('[x for x in (a if b else c)]') |
| 278 | eq('[x for x in a if (b if c else d)]') |
| 279 | eq('f(x for x in a)') |
| 280 | eq('f(1, (x for x in a))') |
| 281 | eq('f((x for x in a), 2)') |
| 282 | eq('(((a)))', 'a') |
| 283 | eq('(((a, b)))', '(a, b)') |
Batuhan Taşkaya | fa19a25 | 2019-05-19 01:10:20 +0300 | [diff] [blame] | 284 | eq("(x:=10)") |
| 285 | eq("f'{(x:=10):=10}'") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 286 | |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame^] | 287 | # f-strings with '=' don't round trip very well, so set the expected |
| 288 | # result explicitely. |
| 289 | self.assertAnnotationEqual("f'{x=!r}'", expected="f'x={x!r}'") |
| 290 | self.assertAnnotationEqual("f'{x=:}'", expected="f'x={x:}'") |
| 291 | self.assertAnnotationEqual("f'{x=:.2f}'", expected="f'x={x:.2f}'") |
| 292 | self.assertAnnotationEqual("f'{x=!r}'", expected="f'x={x!r}'") |
| 293 | self.assertAnnotationEqual("f'{x=!a}'", expected="f'x={x!a}'") |
| 294 | self.assertAnnotationEqual("f'{x=!s:*^20}'", expected="f'x={x!s:*^20}'") |
| 295 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 296 | |
| 297 | if __name__ == "__main__": |
Ezio Melotti | 1ed6be3 | 2013-02-27 10:00:03 +0200 | [diff] [blame] | 298 | unittest.main() |