snakemake / snakefmt

The uncompromising Snakemake code formatter
MIT License
153 stars 29 forks source link

Incorrect shell indenting with snakefmt 0.8.2 and black 23.1.0 #186

Closed GianlucaFicarelli closed 10 months ago

GianlucaFicarelli commented 1 year ago

Hello, I get some unexpected formatting when using the current versions:

See these examples, produced after calling snakefmt Snakefile:

rule test1:
    input:
        "...",
    output:
        "...",
    shell:
        myfunc(
            "param1",
            [
                "item1",
            f"very_long_item2_{very_long_function(other_param)}"
                if some_very_long_condition
                else "",
            ],
        )

rule test2:
    input:
        "...",
    output:
        "...",
    shell:
        myfunc(
            "param1",
            [
                "item1",
            f"very_long_item2_{very_long_function(other_param)}"
                if some_very_long_condition
                else "",
                "item10",
                    "item11",
                    "item12",
                ]
                + [
                f"sub_{item}"
                for item in [
                    "item2",
                    "item3",
                ]
            ],
        )

rule test3:
    input:
        "...",
    output:
        "...",
    shell:
        myfunc(
            "item1",
            [
                "sub_item1",
            "sub_item2"
                if some_very_very_very_very_very_very_very_very_long_condition
                else "sub_item3",
                "sub_item4",
            ],
        )

For comparison, the same function calls, indented at the same level and formatted with black, are:

def test_comparison_with_black():
    def inner():
        myfunc(
            "param1",
            [
                "item1",
                f"very_long_item2_{very_long_function(other_param)}"
                if some_very_long_condition
                else "",
            ],
        )
        myfunc(
            "param1",
            [
                "item1",
                f"very_long_item2_{very_long_function(other_param)}"
                if some_very_long_condition
                else "",
                "item10",
                "item11",
                "item12",
            ]
            + [
                f"sub_{item}"
                for item in [
                    "item2",
                    "item3",
                ]
            ],
        )
        myfunc(
            "item1",
            [
                "sub_item1",
                "sub_item2"
                if some_very_very_very_very_very_very_very_very_long_condition
                else "sub_item3",
                "sub_item4",
            ],
        )

Instead, the format is correct when using these old versions:

mbhall88 commented 1 year ago

Could you please post an example before formatting?

GianlucaFicarelli commented 1 year ago

Could you please post an example before formatting?

Sure, the content before formatting can be the already formatted code as in the second snippet, that will be "unformatted" after processing it.

Here is the corresponding Snakefile that can be used as input:

rule test1:
    input:
        "...",
    output:
        "...",
    shell:
        myfunc(
            "param1",
            [
                "item1",
                f"very_long_item2_{very_long_function(other_param)}"
                if some_very_long_condition
                else "",
            ],
        )

rule test2:
    input:
        "...",
    output:
        "...",
    shell:
        myfunc(
            "param1",
            [
                "item1",
                f"very_long_item2_{very_long_function(other_param)}"
                if some_very_long_condition
                else "",
                "item10",
                "item11",
                "item12",
            ]
            + [
                f"sub_{item}"
                for item in [
                    "item2",
                    "item3",
                ]
            ],
        )

rule test3:
    input:
        "...",
    output:
        "...",
    shell:
        myfunc(
            "item1",
            [
                "sub_item1",
                "sub_item2"
                if some_very_very_very_very_very_very_very_very_long_condition
                else "sub_item3",
                "sub_item4",
            ],
        )
mbhall88 commented 10 months ago

This is fixed in v0.9.0