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

bug: Comments determine indent level of function body #210

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

bug: Comments determine indent level of function body #210

pbsds opened this issue Dec 20, 2021 · 0 comments

Comments

@pbsds
Copy link

pbsds commented Dec 20, 2021

Version: 0.9.2

Test case:

import ast, json
from redbaron import RedBaron

test_case = """
def foo(
        bar,
        baz,
        ): # test
           # break
    pass
""".lstrip()

print(ast.dump(ast.parse(test_case), indent=4))  # dump python ast
print("-"*60)
print(json.dumps(RedBaron(test_case).fst(), indent=4)) # dump redbaron ast
print("-"*60)
print(RedBaron(test_case).dumps() == test_case)  # is reconstruction equal?
Click here for the output
Module(
    body=[
        FunctionDef(
            name='foo',
            args=arguments(
                posonlyargs=[],
                args=[
                    arg(arg='bar'),
                    arg(arg='baz')],
                kwonlyargs=[],
                kw_defaults=[],
                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": [
            {
                "type": "endl",
                "value": "\n",
                "indent": "        ",
                "formatting": []
            }
        ],
        "arguments": [
            {
                "type": "def_argument",
                "annotation_first_formatting": [],
                "annotation_second_formatting": [],
                "first_formatting": [],
                "second_formatting": [],
                "target": {
                    "type": "name",
                    "value": "bar"
                },
                "annotation": {},
                "value": {}
            },
            {
                "type": "comma",
                "first_formatting": [],
                "second_formatting": [
                    {
                        "type": "endl",
                        "value": "\n",
                        "indent": "        ",
                        "formatting": []
                    }
                ]
            },
            {
                "type": "def_argument",
                "annotation_first_formatting": [],
                "annotation_second_formatting": [],
                "first_formatting": [],
                "second_formatting": [],
                "target": {
                    "type": "name",
                    "value": "baz"
                },
                "annotation": {},
                "value": {}
            },
            {
                "type": "comma",
                "first_formatting": [],
                "second_formatting": [
                    {
                        "type": "endl",
                        "value": "\n",
                        "indent": "        ",
                        "formatting": []
                    }
                ]
            }
        ],
        "fourth_formatting": [],
        "return_annotation_first_formatting": [],
        "return_annotation_second_formatting": [],
        "fifth_formatting": [],
        "sixth_formatting": [
            {
                "type": "space",
                "value": " "
            },
            {
                "type": "comment",
                "value": "# test",
                "formatting": []
            }
        ],
        "value": [
            {
                "type": "endl",
                "value": "\n",
                "indent": "           ",
                "formatting": []
            },
            {
                "type": "comment",
                "value": "# break",
                "formatting": []
            },
            {
                "type": "endl",
                "value": "\n",
                "indent": "    ",
                "formatting": []
            }
        ],
        "return_annotation": {}
    },
    {
        "type": "pass"
    },
    {
        "type": "endl",
        "value": "\n",
        "indent": "",
        "formatting": []
    }
]
------------------------------------------------------------
True

Of note: ast correctly puts the pass in the body, while redbaron correctly stores the # test comment into sixth_formatting, but it puts the # break comment into the function body (value), and leaves the pass statement as a statement outside of the function definition.

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