test_param_splitting.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. '''
  2. To make the life of any analysis easier, we are generating Param objects
  3. instead of simple parser objects.
  4. '''
  5. from textwrap import dedent
  6. from parso import parse
  7. def assert_params(param_string, **wanted_dct):
  8. source = dedent('''
  9. def x(%s):
  10. pass
  11. ''') % param_string
  12. module = parse(source)
  13. funcdef = next(module.iter_funcdefs())
  14. dct = dict((p.name.value, p.default and p.default.get_code())
  15. for p in funcdef.get_params())
  16. assert dct == wanted_dct
  17. assert module.get_code() == source
  18. def test_split_params_with_separation_star():
  19. assert_params('x, y=1, *, z=3', x=None, y='1', z='3')
  20. assert_params('*, x', x=None)
  21. assert_params('*')
  22. def test_split_params_with_stars():
  23. assert_params('x, *args', x=None, args=None)
  24. assert_params('**kwargs', kwargs=None)
  25. assert_params('*args, **kwargs', args=None, kwargs=None)
  26. def test_kw_only_no_kw(works_in_py):
  27. """
  28. Parsing this should be working. In CPython the parser also parses this and
  29. in a later step the AST complains.
  30. """
  31. module = works_in_py.parse('def test(arg, *):\n pass')
  32. if module is not None:
  33. func = module.children[0]
  34. open_, p1, asterisk, close = func._get_param_nodes()
  35. assert p1.get_code('arg,')
  36. assert asterisk.value == '*'