redbaron icon indicating copy to clipboard operation
redbaron copied to clipboard

bug: Comments determine indent level of function body

Open pbsds opened this issue 4 years ago • 0 comments

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.

pbsds avatar Dec 20 '21 14:12 pbsds