baron icon indicating copy to clipboard operation
baron copied to clipboard

Support for PEP-570: positional-only arguments

Open pbsds opened this issue 3 years ago • 0 comments

PEP-570 adds support for positional-only arguments. Baron seem to support keyword-only arguments, as shown in test_case_1 below, which is represented by a kwargs_only_marker node. Following this convention i propose creating a posargs_only_marker node for the test_case_2 example.

Testcase:

from redbaron import RedBaron
import ast, json

test_case_1 = "def foo(a, *, b, c): pass"  # kwarg only
test_case_2 = "def foo(a, /, b, c): pass"  # pos only

print(ast.dump(ast.parse(test_case_1), indent=4))        # dump python ast
print("=" * 60)
print(json.dumps(RedBaron(test_case_1).fst(), indent=4)) # dump redbaron ast
print("=" * 60)
print(ast.dump(ast.parse(test_case_2), indent=4))        # dump python ast
print("=" * 60)
print(json.dumps(RedBaron(test_case_2).fst(), indent=4)) # dump redbaron ast
Expand this to view output
Module(
    body=[
        FunctionDef(
            name='foo',
            args=arguments(
                posonlyargs=[],
                args=[
                    arg(arg='a')],
                kwonlyargs=[
                    arg(arg='b'),
                    arg(arg='c')],
                kw_defaults=[
                    None,
                    None],
                defaults=[]),
            body=[
                Pass()],
            decorator_list=[])],
    type_ignores=[])
============================================================
[
    {
        "type": "def",
        "async": false,
        "name": "foo",
        "decorators": [],
        "async_formatting": [],
        "first_formatting": [
            {
                "type": "space",
                "value": " "
            }
        ],
        "second_formatting": [],
        "third_formatting": [],
        "arguments": [
            {
                "type": "def_argument",
                "annotation_first_formatting": [],
                "annotation_second_formatting": [],
                "first_formatting": [],
                "second_formatting": [],
                "target": {
                    "type": "name",
                    "value": "a"
                },
                "annotation": {},
                "value": {}
            },
            {
                "type": "comma",
                "first_formatting": [],
                "second_formatting": [
                    {
                        "type": "space",
                        "value": " "
                    }
                ]
            },
            {
                "type": "kwargs_only_marker",
                "formatting": []
            },
            {
                "type": "comma",
                "first_formatting": [],
                "second_formatting": [
                    {
                        "type": "space",
                        "value": " "
                    }
                ]
            },
            {
                "type": "def_argument",
                "annotation_first_formatting": [],
                "annotation_second_formatting": [],
                "first_formatting": [],
                "second_formatting": [],
                "target": {
                    "type": "name",
                    "value": "b"
                },
                "annotation": {},
                "value": {}
            },
            {
                "type": "comma",
                "first_formatting": [],
                "second_formatting": [
                    {
                        "type": "space",
                        "value": " "
                    }
                ]
            },
            {
                "type": "def_argument",
                "annotation_first_formatting": [],
                "annotation_second_formatting": [],
                "first_formatting": [],
                "second_formatting": [],
                "target": {
                    "type": "name",
                    "value": "c"
                },
                "annotation": {},
                "value": {}
            }
        ],
        "fourth_formatting": [],
        "return_annotation_first_formatting": [],
        "return_annotation_second_formatting": [],
        "fifth_formatting": [],
        "sixth_formatting": [
            {
                "type": "space",
                "value": " "
            }
        ],
        "value": [
            {
                "type": "pass"
            },
            {
                "type": "endl",
                "value": "\n",
                "indent": "",
                "formatting": []
            }
        ],
        "return_annotation": {}
    }
]
============================================================
Module(
    body=[
        FunctionDef(
            name='foo',
            args=arguments(
                posonlyargs=[
                    arg(arg='a')],
                args=[
                    arg(arg='b'),
                    arg(arg='c')],
                kwonlyargs=[],
                kw_defaults=[],
                defaults=[]),
            body=[
                Pass()],
            decorator_list=[])],
    type_ignores=[])
============================================================
Traceback (most recent call last):
  File "/home/pbsds/.cache/pypoetry/virtualenvs/fix-my-functions-Moge_7ev-py3.9/lib/python3.9/site-packages/baron/baron.py", line 20, in _parse
    return parser(tokens)
  File "/home/pbsds/.cache/pypoetry/virtualenvs/fix-my-functions-Moge_7ev-py3.9/lib/python3.9/site-packages/baron/grammator.py", line 836, in parse
    return parser.parse(iter(tokens))
  File "/home/pbsds/.cache/pypoetry/virtualenvs/fix-my-functions-Moge_7ev-py3.9/lib/python3.9/site-packages/baron/parser.py", line 165, in parse
    raise ParsingError(debug_output)
baron.parser.ParsingError: Error, got an unexpected token SLASH here:

   1 def foo(a, /<---- here

The token SLASH should be one of those: COMMA, DOUBLE_STAR, LEFT_PARENTHESIS, NAME, RIGHT_PARENTHESIS, STAR

pbsds avatar Dec 28 '21 01:12 pbsds