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