Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for PEP-570: positional-only arguments #170

Open
pbsds opened this issue Dec 28, 2021 · 0 comments
Open

Support for PEP-570: positional-only arguments #170

pbsds opened this issue Dec 28, 2021 · 0 comments

Comments

@pbsds
Copy link

pbsds commented Dec 28, 2021

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant