blob: 29c4632e7e504406eda68b75a85a12e6fa82c90f [file] [log] [blame]
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +00001# Test various flavors of legal and illegal future statements
2
Guido van Rossum95e4d582018-01-26 08:20:18 -08003from functools import partial
Neal Norwitz328f3382003-12-13 22:43:34 +00004import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Guido van Rossum95e4d582018-01-26 08:20:18 -08006from textwrap import dedent
Serhiy Storchaka8b583392016-12-11 14:39:01 +02007import os
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +00008import re
9
R David Murray44b548d2016-09-08 13:59:53 -040010rx = re.compile(r'\((\S+).py, line (\d+)')
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000011
Neal Norwitz328f3382003-12-13 22:43:34 +000012def get_error_location(msg):
13 mo = rx.search(str(msg))
14 return mo.group(1, 2)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000015
Neal Norwitz328f3382003-12-13 22:43:34 +000016class FutureTest(unittest.TestCase):
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000017
Serhiy Storchaka8b583392016-12-11 14:39:01 +020018 def check_syntax_error(self, err, basename, lineno, offset=0):
19 self.assertIn('%s.py, line %d' % (basename, lineno), str(err))
20 self.assertEqual(os.path.basename(err.filename), basename + '.py')
21 self.assertEqual(err.lineno, lineno)
22 self.assertEqual(err.offset, offset)
23
Neal Norwitz328f3382003-12-13 22:43:34 +000024 def test_future1(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020025 with support.CleanImport('future_test1'):
26 from test import future_test1
27 self.assertEqual(future_test1.result, 6)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000028
Neal Norwitz328f3382003-12-13 22:43:34 +000029 def test_future2(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020030 with support.CleanImport('future_test2'):
31 from test import future_test2
32 self.assertEqual(future_test2.result, 6)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000033
Neal Norwitz328f3382003-12-13 22:43:34 +000034 def test_future3(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020035 with support.CleanImport('test_future3'):
36 from test import test_future3
Jeremy Hylton8471a352001-08-20 20:33:42 +000037
Neal Norwitz328f3382003-12-13 22:43:34 +000038 def test_badfuture3(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020039 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000040 from test import badsyntax_future3
Serhiy Storchaka8b583392016-12-11 14:39:01 +020041 self.check_syntax_error(cm.exception, "badsyntax_future3", 3)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000042
Neal Norwitz328f3382003-12-13 22:43:34 +000043 def test_badfuture4(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020044 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000045 from test import badsyntax_future4
Serhiy Storchaka8b583392016-12-11 14:39:01 +020046 self.check_syntax_error(cm.exception, "badsyntax_future4", 3)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000047
Neal Norwitz328f3382003-12-13 22:43:34 +000048 def test_badfuture5(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020049 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000050 from test import badsyntax_future5
Serhiy Storchaka8b583392016-12-11 14:39:01 +020051 self.check_syntax_error(cm.exception, "badsyntax_future5", 4)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000052
Neal Norwitz328f3382003-12-13 22:43:34 +000053 def test_badfuture6(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020054 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000055 from test import badsyntax_future6
Serhiy Storchaka8b583392016-12-11 14:39:01 +020056 self.check_syntax_error(cm.exception, "badsyntax_future6", 3)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000057
Neal Norwitz328f3382003-12-13 22:43:34 +000058 def test_badfuture7(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020059 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000060 from test import badsyntax_future7
Serhiy Storchaka8b583392016-12-11 14:39:01 +020061 self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 53)
Neal Norwitz328f3382003-12-13 22:43:34 +000062
63 def test_badfuture8(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020064 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000065 from test import badsyntax_future8
Serhiy Storchaka8b583392016-12-11 14:39:01 +020066 self.check_syntax_error(cm.exception, "badsyntax_future8", 3)
Neal Norwitz328f3382003-12-13 22:43:34 +000067
68 def test_badfuture9(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020069 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000070 from test import badsyntax_future9
Serhiy Storchaka8b583392016-12-11 14:39:01 +020071 self.check_syntax_error(cm.exception, "badsyntax_future9", 3, 0)
Neal Norwitz328f3382003-12-13 22:43:34 +000072
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070073 def test_badfuture10(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020074 with self.assertRaises(SyntaxError) as cm:
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070075 from test import badsyntax_future10
Serhiy Storchaka8b583392016-12-11 14:39:01 +020076 self.check_syntax_error(cm.exception, "badsyntax_future10", 3, 0)
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070077
Thomas Wouters89f507f2006-12-13 04:49:30 +000078 def test_parserhack(self):
79 # test that the parser.c::future_hack function works as expected
80 # Note: although this test must pass, it's not testing the original
81 # bug as of 2.6 since the with statement is not optional and
82 # the parser hack disabled. If a new keyword is introduced in
83 # 2.6, change this to refer to the new future import.
84 try:
Benjamin Peterson9aebc612008-10-26 20:58:53 +000085 exec("from __future__ import print_function; print 0")
Thomas Wouters89f507f2006-12-13 04:49:30 +000086 except SyntaxError:
87 pass
88 else:
89 self.fail("syntax error didn't occur")
90
91 try:
Benjamin Peterson9aebc612008-10-26 20:58:53 +000092 exec("from __future__ import (print_function); print 0")
Thomas Wouters89f507f2006-12-13 04:49:30 +000093 except SyntaxError:
94 pass
95 else:
96 self.fail("syntax error didn't occur")
97
Benjamin Peterson9aebc612008-10-26 20:58:53 +000098 def test_multiple_features(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020099 with support.CleanImport("test.test_future5"):
100 from test import test_future5
Benjamin Peterson9aebc612008-10-26 20:58:53 +0000101
Benjamin Petersonf216c942008-10-31 02:28:05 +0000102 def test_unicode_literals_exec(self):
103 scope = {}
104 exec("from __future__ import unicode_literals; x = ''", {}, scope)
Ezio Melottie9615932010-01-24 19:26:24 +0000105 self.assertIsInstance(scope["x"], str)
Benjamin Petersonf216c942008-10-31 02:28:05 +0000106
Guido van Rossum95e4d582018-01-26 08:20:18 -0800107class AnnotationsFutureTestCase(unittest.TestCase):
108 template = dedent(
109 """
110 from __future__ import annotations
111 def f() -> {ann}:
112 ...
113 def g(arg: {ann}) -> None:
114 ...
115 var: {ann}
116 var2: {ann} = None
117 """
118 )
119
120 def getActual(self, annotation):
121 scope = {}
122 exec(self.template.format(ann=annotation), {}, scope)
123 func_ret_ann = scope['f'].__annotations__['return']
124 func_arg_ann = scope['g'].__annotations__['arg']
125 var_ann1 = scope['__annotations__']['var']
126 var_ann2 = scope['__annotations__']['var2']
127 self.assertEqual(func_ret_ann, func_arg_ann)
128 self.assertEqual(func_ret_ann, var_ann1)
129 self.assertEqual(func_ret_ann, var_ann2)
130 return func_ret_ann
131
132 def assertAnnotationEqual(
133 self, annotation, expected=None, drop_parens=False, is_tuple=False,
134 ):
135 actual = self.getActual(annotation)
136 if expected is None:
137 expected = annotation if not is_tuple else annotation[1:-1]
138 if drop_parens:
139 self.assertNotEqual(actual, expected)
140 actual = actual.replace("(", "").replace(")", "")
141
142 self.assertEqual(actual, expected)
143
144 def test_annotations(self):
145 eq = self.assertAnnotationEqual
146 eq('...')
147 eq("'some_string'")
148 eq("b'\\xa3'")
149 eq('Name')
150 eq('None')
151 eq('True')
152 eq('False')
153 eq('1')
154 eq('1.0')
155 eq('1j')
156 eq('True or False')
157 eq('True or False or None')
158 eq('True and False')
159 eq('True and False and None')
160 eq('(Name1 and Name2) or Name3')
161 eq('Name1 or (Name2 and Name3)')
162 eq('(Name1 and Name2) or (Name3 and Name4)')
163 eq('Name1 or (Name2 and Name3) or Name4')
164 eq('v1 << 2')
165 eq('1 >> v2')
166 eq(r'1 % finished')
167 eq('((1 + v2) - (v3 * 4)) ^ (((5 ** v6) / 7) // 8)')
168 eq('not great')
169 eq('~great')
170 eq('+value')
171 eq('-1')
172 eq('(~int) and (not ((v1 ^ (123 + v2)) | True))')
173 eq('lambda arg: None')
174 eq('lambda a=True: a')
175 eq('lambda a, b, c=True: a')
176 eq("lambda a, b, c=True, *, d=(1 << v2), e='str': a")
177 eq("lambda a, b, c=True, *vararg, d=(v1 << 2), e='str', **kwargs: a + b")
178 eq('1 if True else 2')
179 eq('(str or None) if True else (str or bytes or None)')
180 eq('(str or None) if (1 if True else 2) else (str or bytes or None)')
181 eq("{'2.7': dead, '3.7': (long_live or die_hard)}")
182 eq("{'2.7': dead, '3.7': (long_live or die_hard), **{'3.6': verygood}}")
183 eq("{**a, **b, **c}")
184 eq("{'2.7', '3.6', '3.7', '3.8', '3.9', ('4.0' if gilectomy else '3.10')}")
185 eq("({'a': 'b'}, (True or False), (+value), 'string', b'bytes') or None")
186 eq("()")
187 eq("(1,)")
188 eq("(1, 2)")
189 eq("(1, 2, 3)")
190 eq("[]")
191 eq("[1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)]")
192 eq("{i for i in (1, 2, 3)}")
193 eq("{(i ** 2) for i in (1, 2, 3)}")
194 eq("{(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))}")
195 eq("{((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)}")
196 eq("[i for i in (1, 2, 3)]")
197 eq("[(i ** 2) for i in (1, 2, 3)]")
198 eq("[(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))]")
199 eq("[((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]")
200 eq(r"{i: 0 for i in (1, 2, 3)}")
201 eq("{i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}")
202 eq("Python3 > Python2 > COBOL")
203 eq("Life is Life")
204 eq("call()")
205 eq("call(arg)")
206 eq("call(kwarg='hey')")
207 eq("call(arg, kwarg='hey')")
208 eq("call(arg, another, kwarg='hey', **kwargs)")
209 eq("lukasz.langa.pl")
210 eq("call.me(maybe)")
211 eq("1 .real")
212 eq("1.0 .real")
213 eq("....__class__")
214 eq("list[str]")
215 eq("dict[str, int]")
216 eq("tuple[str, ...]")
217 eq("tuple[str, int, float, dict[str, int]]")
218 eq("slice[0]")
219 eq("slice[0:1]")
220 eq("slice[0:1:2]")
221 eq("slice[:]")
222 eq("slice[:-1]")
223 eq("slice[1:]")
224 eq("slice[::-1]")
225 eq('(str or None) if (sys.version_info[0] > (3,)) else (str or bytes or None)')
226 eq("f'f-string without formatted values is just a string'")
227 eq("f'{{NOT a formatted value}}'")
228 eq("f'some f-string with {a} {few():.2f} {formatted.values!r}'")
229 eq('''f"{f'{nested} inner'} outer"''')
230 eq("f'space between opening braces: { {a for a in (1, 2, 3)}}'")
231
232 def test_annotations_inexact(self):
233 """Source formatting is not always preserved
234
235 This is due to reconstruction from AST. We *need to* put the parens
236 in nested expressions because we don't know if the source code
237 had them in the first place or not.
238 """
239 eq = partial(self.assertAnnotationEqual, drop_parens=True)
240 eq('Name1 and Name2 or Name3')
241 eq('Name1 or Name2 and Name3')
242 eq('Name1 and Name2 or Name3 and Name4')
243 eq('Name1 or Name2 and Name3 or Name4')
244 eq('1 + v2 - v3 * 4 ^ v5 ** 6 / 7 // 8')
245 eq('~int and not v1 ^ 123 + v2 | True')
246 eq('str or None if True else str or bytes or None')
247 eq("{'2.7': dead, '3.7': long_live or die_hard}")
248 eq("{'2.7', '3.6', '3.7', '3.8', '3.9', '4.0' if gilectomy else '3.10'}")
249 eq("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or A, 11 or B, 12 or C]")
250 # Consequently, we always drop unnecessary parens if they were given in
251 # the outer scope:
252 some_name = self.getActual("(SomeName)")
253 self.assertEqual(some_name, 'SomeName')
254 # Interestingly, in the case of tuples (and generator expressions) the
255 # parens are *required* by the Python syntax in the annotation context.
256 # But there's no point storing that detail in __annotations__ so we're
257 # fine with the parens-less form.
258 eq = partial(self.assertAnnotationEqual, is_tuple=True)
259 eq("(Good, Bad, Ugly)")
260 eq("(i for i in (1, 2, 3))")
261 eq("((i ** 2) for i in (1, 2, 3))")
262 eq("((i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c')))")
263 eq("(((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3))")
264 eq("(*starred)")
265 eq('(yield from outside_of_generator)')
266 eq('(yield)')
267 eq('(await some.complicated[0].call(with_args=(True or (1 is not 1))))')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000268
Neal Norwitz328f3382003-12-13 22:43:34 +0000269
270if __name__ == "__main__":
Ezio Melotti1ed6be32013-02-27 10:00:03 +0200271 unittest.main()