Debian Codegraph @

Lib/test/test_positional_only_arg.py

raw
1 """Unit tests for the positional only argument syntax specified in PEP 570."""
2
3 import dis
4 import pickle
5 import types
6 import unittest
7
8 from test.support import check_syntax_error
9
10
11 def global_pos_only_f(a, b, /):
12 return a, b
13
14 def global_pos_only_and_normal(a, /, b):
15 return a, b
16
17 def global_pos_only_defaults(a=1, /, b=2):
18 return a, b
19
20 class PositionalOnlyTestCase(unittest.TestCase):
21
22 def assertRaisesSyntaxError(self, codestr, regex="invalid syntax"):
23 with self.assertRaisesRegex(SyntaxError, regex):
24 compile(codestr + "\n", "<test>", "single")
25
26 def test_invalid_syntax_errors(self):
27 check_syntax_error(self, "def f(a, b = 5, /, c): pass", "parameter without a default follows parameter with a default")
28 check_syntax_error(self, "def f(a = 5, b, /, c): pass", "parameter without a default follows parameter with a default")
29 check_syntax_error(self, "def f(a = 5, b=1, /, c, *, d=2): pass", "parameter without a default follows parameter with a default")
30 check_syntax_error(self, "def f(a = 5, b, /): pass", "parameter without a default follows parameter with a default")
31 check_syntax_error(self, "def f(a, /, b = 5, c): pass", "parameter without a default follows parameter with a default")
32 check_syntax_error(self, "def f(*args, /): pass")
33 check_syntax_error(self, "def f(*args, a, /): pass")
34 check_syntax_error(self, "def f(**kwargs, /): pass")
35 check_syntax_error(self, "def f(/, a = 1): pass")
36 check_syntax_error(self, "def f(/, a): pass")
37 check_syntax_error(self, "def f(/): pass")
38 check_syntax_error(self, "def f(*, a, /): pass")
39 check_syntax_error(self, "def f(*, /, a): pass")
40 check_syntax_error(self, "def f(a, /, a): pass", "duplicate argument 'a' in function definition")
41 check_syntax_error(self, "def f(a, /, *, a): pass", "duplicate argument 'a' in function definition")
42 check_syntax_error(self, "def f(a, b/2, c): pass")
43 check_syntax_error(self, "def f(a, /, c, /): pass")
44 check_syntax_error(self, "def f(a, /, c, /, d): pass")
45 check_syntax_error(self, "def f(a, /, c, /, d, *, e): pass")
46 check_syntax_error(self, "def f(a, *, c, /, d, e): pass")
47
48 def test_invalid_syntax_errors_async(self):
49 check_syntax_error(self, "async def f(a, b = 5, /, c): pass", "parameter without a default follows parameter with a default")
50 check_syntax_error(self, "async def f(a = 5, b, /, c): pass", "parameter without a default follows parameter with a default")
51 check_syntax_error(self, "async def f(a = 5, b=1, /, c, d=2): pass", "parameter without a default follows parameter with a default")
52 check_syntax_error(self, "async def f(a = 5, b, /): pass", "parameter without a default follows parameter with a default")
53 check_syntax_error(self, "async def f(a, /, b = 5, c): pass", "parameter without a default follows parameter with a default")
54 check_syntax_error(self, "async def f(*args, /): pass")
55 check_syntax_error(self, "async def f(*args, a, /): pass")
56 check_syntax_error(self, "async def f(**kwargs, /): pass")
57 check_syntax_error(self, "async def f(/, a = 1): pass")
58 check_syntax_error(self, "async def f(/, a): pass")
59 check_syntax_error(self, "async def f(/): pass")
60 check_syntax_error(self, "async def f(*, a, /): pass")
61 check_syntax_error(self, "async def f(*, /, a): pass")
62 check_syntax_error(self, "async def f(a, /, a): pass", "duplicate argument 'a' in function definition")
63 check_syntax_error(self, "async def f(a, /, *, a): pass", "duplicate argument 'a' in function definition")
64 check_syntax_error(self, "async def f(a, b/2, c): pass")
65 check_syntax_error(self, "async def f(a, /, c, /): pass")
66 check_syntax_error(self, "async def f(a, /, c, /, d): pass")
67 check_syntax_error(self, "async def f(a, /, c, /, d, *, e): pass")
68 check_syntax_error(self, "async def f(a, *, c, /, d, e): pass")
69
70 def test_optional_positional_only_args(self):
71 def f(a, b=10, /, c=100):
72 return a + b + c
73
74 self.assertEqual(f(1, 2, 3), 6)
75 self.assertEqual(f(1, 2, c=3), 6)
76 with self.assertRaisesRegex(TypeError, r"f\(\) got some positional-only arguments passed as keyword arguments: 'b'"):
77 f(1, b=2, c=3)
78
79 self.assertEqual(f(1, 2), 103)
80 with self.assertRaisesRegex(TypeError, r"f\(\) got some positional-only arguments passed as keyword arguments: 'b'"):
81 f(1, b=2)
82 self.assertEqual(f(1, c=2), 13)
83
84 def f(a=1, b=10, /, c=100):
85 return a + b + c
86
87 self.assertEqual(f(1, 2, 3), 6)
88 self.assertEqual(f(1, 2, c=3), 6)
89 with self.assertRaisesRegex(TypeError, r"f\(\) got some positional-only arguments passed as keyword arguments: 'b'"):
90 f(1, b=2, c=3)
91
92 self.assertEqual(f(1, 2), 103)
93 with self.assertRaisesRegex(TypeError, r"f\(\) got some positional-only arguments passed as keyword arguments: 'b'"):
94 f(1, b=2)
95 self.assertEqual(f(1, c=2), 13)
96
97 def test_syntax_for_many_positional_only(self):
98 # more than 255 positional only arguments, should compile ok
99 fundef = "def f(%s, /):\n pass\n" % ', '.join('i%d' % i for i in range(300))
100 compile(fundef, "<test>", "single")
101
102 def test_pos_only_definition(self):
103 def f(a, b, c, /, d, e=1, *, f, g=2):
104 pass
105
106 self.assertEqual(5, f.__code__.co_argcount) # 3 posonly + 2 "standard args"
107 self.assertEqual(3, f.__code__.co_posonlyargcount)
108 self.assertEqual((1,), f.__defaults__)
109
110 def f(a, b, c=1, /, d=2, e=3, *, f, g=4):
111 pass
112
113 self.assertEqual(5, f.__code__.co_argcount) # 3 posonly + 2 "standard args"
114 self.assertEqual(3, f.__code__.co_posonlyargcount)
115 self.assertEqual((1, 2, 3), f.__defaults__)
116
117 def test_pos_only_call_via_unpacking(self):
118 def f(a, b, /):
119 return a + b
120
121 self.assertEqual(f(*[1, 2]), 3)
122
123 def test_use_positional_as_keyword(self):
124 def f(a, /):
125 pass
126 expected = r"f\(\) got some positional-only arguments passed as keyword arguments: 'a'"
127 with self.assertRaisesRegex(TypeError, expected):
128 f(a=1)
129
130 def f(a, /, b):
131 pass
132 expected = r"f\(\) got some positional-only arguments passed as keyword arguments: 'a'"
133 with self.assertRaisesRegex(TypeError, expected):
134 f(a=1, b=2)
135
136 def f(a, b, /):
137 pass
138 expected = r"f\(\) got some positional-only arguments passed as keyword arguments: 'a, b'"
139 with self.assertRaisesRegex(TypeError, expected):
140 f(a=1, b=2)
141
142 def test_positional_only_and_arg_invalid_calls(self):
143 def f(a, b, /, c):
144 pass
145 with self.assertRaisesRegex(TypeError, r"f\(\) missing 1 required positional argument: 'c'"):
146 f(1, 2)
147 with self.assertRaisesRegex(TypeError, r"f\(\) missing 2 required positional arguments: 'b' and 'c'"):
148 f(1)
149 with self.assertRaisesRegex(TypeError, r"f\(\) missing 3 required positional arguments: 'a', 'b', and 'c'"):
150 f()
151 with self.assertRaisesRegex(TypeError, r"f\(\) takes 3 positional arguments but 4 were given"):
152 f(1, 2, 3, 4)
153
154 def test_positional_only_and_optional_arg_invalid_calls(self):
155 def f(a, b, /, c=3):
156 pass
157 f(1, 2) # does not raise
158 with self.assertRaisesRegex(TypeError, r"f\(\) missing 1 required positional argument: 'b'"):
159 f(1)
160 with self.assertRaisesRegex(TypeError, r"f\(\) missing 2 required positional arguments: 'a' and 'b'"):
161 f()
162 with self.assertRaisesRegex(TypeError, r"f\(\) takes from 2 to 3 positional arguments but 4 were given"):
163 f(1, 2, 3, 4)
164
165 def test_positional_only_and_kwonlyargs_invalid_calls(self):
166 def f(a, b, /, c, *, d, e):
167 pass
168 f(1, 2, 3, d=1, e=2) # does not raise
169 with self.assertRaisesRegex(TypeError, r"missing 1 required keyword-only argument: 'd'"):
170 f(1, 2, 3, e=2)
171 with self.assertRaisesRegex(TypeError, r"missing 2 required keyword-only arguments: 'd' and 'e'"):
172 f(1, 2, 3)
173 with self.assertRaisesRegex(TypeError, r"f\(\) missing 1 required positional argument: 'c'"):
174 f(1, 2)
175 with self.assertRaisesRegex(TypeError, r"f\(\) missing 2 required positional arguments: 'b' and 'c'"):
176 f(1)
177 with self.assertRaisesRegex(TypeError, r" missing 3 required positional arguments: 'a', 'b', and 'c'"):
178 f()
179 with self.assertRaisesRegex(TypeError, r"f\(\) takes 3 positional arguments but 6 positional arguments "
180 r"\(and 2 keyword-only arguments\) were given"):
181 f(1, 2, 3, 4, 5, 6, d=7, e=8)
182 with self.assertRaisesRegex(TypeError, r"f\(\) got an unexpected keyword argument 'f'"):
183 f(1, 2, 3, d=1, e=4, f=56)
184
185 def test_positional_only_invalid_calls(self):
186 def f(a, b, /):
187 pass
188 f(1, 2) # does not raise
189 with self.assertRaisesRegex(TypeError, r"f\(\) missing 1 required positional argument: 'b'"):
190 f(1)
191 with self.assertRaisesRegex(TypeError, r"f\(\) missing 2 required positional arguments: 'a' and 'b'"):
192 f()
193 with self.assertRaisesRegex(TypeError, r"f\(\) takes 2 positional arguments but 3 were given"):
194 f(1, 2, 3)
195
196 def test_positional_only_with_optional_invalid_calls(self):
197 def f(a, b=2, /):
198 pass
199 f(1) # does not raise
200 with self.assertRaisesRegex(TypeError, r"f\(\) missing 1 required positional argument: 'a'"):
201 f()
202
203 with self.assertRaisesRegex(TypeError, r"f\(\) takes from 1 to 2 positional arguments but 3 were given"):
204 f(1, 2, 3)
205
206 def test_no_standard_args_usage(self):
207 def f(a, b, /, *, c):
208 pass
209
210 f(1, 2, c=3)
211 with self.assertRaises(TypeError):
212 f(1, b=2, c=3)
213
214 def test_change_default_pos_only(self):
215 def f(a, b=2, /, c=3):
216 return a + b + c
217
218 self.assertEqual((2,3), f.__defaults__)
219 f.__defaults__ = (1, 2, 3)
220 self.assertEqual(f(1, 2, 3), 6)
221
222 def test_lambdas(self):
223 x = lambda a, /, b: a + b
224 self.assertEqual(x(1,2), 3)
225 self.assertEqual(x(1,b=2), 3)
226
227 x = lambda a, /, b=2: a + b
228 self.assertEqual(x(1), 3)
229
230 x = lambda a, b, /: a + b
231 self.assertEqual(x(1, 2), 3)
232
233 x = lambda a, b, /, : a + b
234 self.assertEqual(x(1, 2), 3)
235
236 def test_invalid_syntax_lambda(self):
237 check_syntax_error(self, "lambda a, b = 5, /, c: None", "parameter without a default follows parameter with a default")
238 check_syntax_error(self, "lambda a = 5, b, /, c: None", "parameter without a default follows parameter with a default")
239 check_syntax_error(self, "lambda a = 5, b=1, /, c, *, d=2: None", "parameter without a default follows parameter with a default")
240 check_syntax_error(self, "lambda a = 5, b, /: None", "parameter without a default follows parameter with a default")
241 check_syntax_error(self, "lambda a, /, b = 5, c: None", "parameter without a default follows parameter with a default")
242 check_syntax_error(self, "lambda *args, /: None")
243 check_syntax_error(self, "lambda *args, a, /: None")
244 check_syntax_error(self, "lambda **kwargs, /: None")
245 check_syntax_error(self, "lambda /, a = 1: None")
246 check_syntax_error(self, "lambda /, a: None")
247 check_syntax_error(self, "lambda /: None")
248 check_syntax_error(self, "lambda *, a, /: None")
249 check_syntax_error(self, "lambda *, /, a: None")
250 check_syntax_error(self, "lambda a, /, a: None", "duplicate argument 'a' in function definition")
251 check_syntax_error(self, "lambda a, /, *, a: None", "duplicate argument 'a' in function definition")
252 check_syntax_error(self, "lambda a, /, b, /: None")
253 check_syntax_error(self, "lambda a, /, b, /, c: None")
254 check_syntax_error(self, "lambda a, /, b, /, c, *, d: None")
255 check_syntax_error(self, "lambda a, *, b, /, c: None")
256
257 def test_posonly_methods(self):
258 class Example:
259 def f(self, a, b, /):
260 return a, b
261
262 self.assertEqual(Example().f(1, 2), (1, 2))
263 self.assertEqual(Example.f(Example(), 1, 2), (1, 2))
264 self.assertRaises(TypeError, Example.f, 1, 2)
265 expected = r"f\(\) got some positional-only arguments passed as keyword arguments: 'b'"
266 with self.assertRaisesRegex(TypeError, expected):
267 Example().f(1, b=2)
268
269 def test_module_function(self):
270 with self.assertRaisesRegex(TypeError, r"f\(\) missing 2 required positional arguments: 'a' and 'b'"):
271 global_pos_only_f()
272
273
274 def test_closures(self):
275 def f(x,y):
276 def g(x2,/,y2):
277 return x + y + x2 + y2
278 return g
279
280 self.assertEqual(f(1,2)(3,4), 10)
281 with self.assertRaisesRegex(TypeError, r"g\(\) missing 1 required positional argument: 'y2'"):
282 f(1,2)(3)
283 with self.assertRaisesRegex(TypeError, r"g\(\) takes 2 positional arguments but 3 were given"):
284 f(1,2)(3,4,5)
285
286 def f(x,/,y):
287 def g(x2,y2):
288 return x + y + x2 + y2
289 return g
290
291 self.assertEqual(f(1,2)(3,4), 10)
292
293 def f(x,/,y):
294 def g(x2,/,y2):
295 return x + y + x2 + y2
296 return g
297
298 self.assertEqual(f(1,2)(3,4), 10)
299 with self.assertRaisesRegex(TypeError, r"g\(\) missing 1 required positional argument: 'y2'"):
300 f(1,2)(3)
301 with self.assertRaisesRegex(TypeError, r"g\(\) takes 2 positional arguments but 3 were given"):
302 f(1,2)(3,4,5)
303
304 def test_annotations_in_closures(self):
305
306 def inner_has_pos_only():
307 def f(x: int, /): ...
308 return f
309
310 assert inner_has_pos_only().__annotations__ == {'x': int}
311
312 class Something:
313 def method(self):
314 def f(x: int, /): ...
315 return f
316
317 assert Something().method().__annotations__ == {'x': int}
318
319 def multiple_levels():
320 def inner_has_pos_only():
321 def f(x: int, /): ...
322 return f
323 return inner_has_pos_only()
324
325 assert multiple_levels().__annotations__ == {'x': int}
326
327 def test_same_keyword_as_positional_with_kwargs(self):
328 def f(something,/,**kwargs):
329 return (something, kwargs)
330
331 self.assertEqual(f(42, something=42), (42, {'something': 42}))
332
333 with self.assertRaisesRegex(TypeError, r"f\(\) missing 1 required positional argument: 'something'"):
334 f(something=42)
335
336 self.assertEqual(f(42), (42, {}))
337
338 def test_mangling(self):
339 class X:
340 def f(self, __a=42, /):
341 return __a
342
343 def f2(self, __a=42, /, __b=43):
344 return (__a, __b)
345
346 def f3(self, __a=42, /, __b=43, *, __c=44):
347 return (__a, __b, __c)
348
349 self.assertEqual(X().f(), 42)
350 self.assertEqual(X().f2(), (42, 43))
351 self.assertEqual(X().f3(), (42, 43, 44))
352
353 def test_too_many_arguments(self):
354 # more than 255 positional-only arguments, should compile ok
355 fundef = "def f(%s, /):\n pass\n" % ', '.join('i%d' % i for i in range(300))
356 compile(fundef, "<test>", "single")
357
358 def test_serialization(self):
359 pickled_posonly = pickle.dumps(global_pos_only_f)
360 pickled_optional = pickle.dumps(global_pos_only_and_normal)
361 pickled_defaults = pickle.dumps(global_pos_only_defaults)
362
363 unpickled_posonly = pickle.loads(pickled_posonly)
364 unpickled_optional = pickle.loads(pickled_optional)
365 unpickled_defaults = pickle.loads(pickled_defaults)
366
367 self.assertEqual(unpickled_posonly(1,2), (1,2))
368 expected = r"global_pos_only_f\(\) got some positional-only arguments "\
369 r"passed as keyword arguments: 'a, b'"
370 with self.assertRaisesRegex(TypeError, expected):
371 unpickled_posonly(a=1,b=2)
372
373 self.assertEqual(unpickled_optional(1,2), (1,2))
374 expected = r"global_pos_only_and_normal\(\) got some positional-only arguments "\
375 r"passed as keyword arguments: 'a'"
376 with self.assertRaisesRegex(TypeError, expected):
377 unpickled_optional(a=1,b=2)
378
379 self.assertEqual(unpickled_defaults(), (1,2))
380 expected = r"global_pos_only_defaults\(\) got some positional-only arguments "\
381 r"passed as keyword arguments: 'a'"
382 with self.assertRaisesRegex(TypeError, expected):
383 unpickled_defaults(a=1,b=2)
384
385 def test_async(self):
386
387 async def f(a=1, /, b=2):
388 return a, b
389
390 with self.assertRaisesRegex(TypeError, r"f\(\) got some positional-only arguments passed as keyword arguments: 'a'"):
391 f(a=1, b=2)
392
393 def _check_call(*args, **kwargs):
394 try:
395 coro = f(*args, **kwargs)
396 coro.send(None)
397 except StopIteration as e:
398 result = e.value
399 self.assertEqual(result, (1, 2))
400
401 _check_call(1, 2)
402 _check_call(1, b=2)
403 _check_call(1)
404 _check_call()
405
406 def test_generator(self):
407
408 def f(a=1, /, b=2):
409 yield a, b
410
411 with self.assertRaisesRegex(TypeError, r"f\(\) got some positional-only arguments passed as keyword arguments: 'a'"):
412 f(a=1, b=2)
413
414 gen = f(1, 2)
415 self.assertEqual(next(gen), (1, 2))
416 gen = f(1, b=2)
417 self.assertEqual(next(gen), (1, 2))
418 gen = f(1)
419 self.assertEqual(next(gen), (1, 2))
420 gen = f()
421 self.assertEqual(next(gen), (1, 2))
422
423 def test_super(self):
424
425 sentinel = object()
426
427 class A:
428 def method(self):
429 return sentinel
430
431 class C(A):
432 def method(self, /):
433 return super().method()
434
435 self.assertEqual(C().method(), sentinel)
436
437 def test_annotations_constant_fold(self):
438 def g():
439 def f(x: not (int is int), /): ...
440
441 # without constant folding we end up with
442 # COMPARE_OP(is), IS_OP (0)
443 # with constant folding we should expect a IS_OP (1)
444 code_obj = next(const for const in g.__code__.co_consts
445 if isinstance(const, types.CodeType) and const.co_name == "__annotate__")
446 codes = [(i.opname, i.argval) for i in dis.get_instructions(code_obj)]
447 self.assertNotIn(('UNARY_NOT', None), codes)
448 self.assertIn(('IS_OP', 1), codes)
449
450
451 if __name__ == "__main__":
452 unittest.main()