Skip to content
New issue

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

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

Already on GitHub? # to your account

lambda arguments have the wrong range and cause unstable formatting #5896

Closed
konstin opened this issue Jul 19, 2023 · 3 comments
Closed

lambda arguments have the wrong range and cause unstable formatting #5896

konstin opened this issue Jul 19, 2023 · 3 comments
Assignees
Labels
bug Something isn't working formatter Related to the formatter

Comments

@konstin
Copy link
Member

konstin commented Jul 19, 2023

This example causes unstable formatting:

y=lambda:{
#
}
--- Formatted once
+++ Formatted twice
@@ -1,3 +1,3 @@
 y = (
-    lambda: {}#
+    lambda: {}  #
 )
---

Formatted once:
---
y = (
    lambda: {}#
)
---

Formatted twice:
---
y = (
    lambda: {}  #
)
---

The comment gets attached to the lambda arguments instead of the body expression where it should be:

{
    Node {
        kind: Arguments,
        range: 2..14,
        source: `lambda:{⏎`,
    }: {
        "leading": [],
        "dangling": [
            SourceComment {
                text: "#",
                position: OwnLine,
                formatted: true,
            },
        ],
        "trailing": [],
    },
}
@konstin konstin added bug Something isn't working formatter Related to the formatter labels Jul 19, 2023
@MichaReiser
Copy link
Member

MichaReiser commented Jul 19, 2023

Huh, that's strange. The comment is clearly inside of the dictionary.

Playground

@konstin
Copy link
Member Author

konstin commented Jul 20, 2023

i found this again through a different case:

(lambda:(#
),)

The lambda arguments here are empty, yet they span the whole lambda range

$ cargo run --bin ruff_dev print-ast scratch.py 
[
    Expr(
        StmtExpr {
            range: 0..14,
            value: Tuple(
                ExprTuple {
                    range: 0..14,
                    elts: [
                        Lambda(
                            ExprLambda {
                                range: 1..12,
                                args: Arguments {
                                    range: 1..12,
                                    posonlyargs: [],
                                    args: [],
                                    vararg: None,
                                    kwonlyargs: [],
                                    kwarg: None,
                                },
                                body: Tuple(
                                    ExprTuple {
                                        range: 8..12,
                                        elts: [],
                                        ctx: Load,
                                    },
                                ),
                            },
                        ),
                    ],
                    ctx: Load,
                },
            ),
        },
    ),
]

This causes unstable formatting (handled by default comment placement):

---
--- Formatted once
+++ Formatted twice
@@ -1,4 +1,3 @@
 (
-    lambda: ()  #
-    ,
+    lambda: (),  #
 )
---

Formatted once:
---
(
    lambda: ()  #
    ,
)
---

Formatted twice:
---
(
    lambda: (),  #
)
---
{
    Node {
        kind: Arguments,
        range: 1..12,
        source: `lambda:(#⏎`,
    }: {
        "leading": [],
        "dangling": [
            SourceComment {
                text: "#",
                position: EndOfLine,
                formatted: true,
            },
        ],
        "trailing": [],
    },
}

@konstin konstin self-assigned this Jul 20, 2023
@konstin konstin changed the title Formatter: dangling own line comment in lambda body causes unstable formatting lambda arguments have the wrong range and cause unstable formatting Jul 20, 2023
@konstin
Copy link
Member Author

konstin commented Jul 21, 2023

Fixed in #5944

@konstin konstin closed this as completed Jul 21, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working formatter Related to the formatter
Projects
None yet
Development

No branches or pull requests

2 participants