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