snakemake / snakefmt

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

snakefmt adds wrong indentation #124

Closed FelixMoelder closed 1 year ago

FelixMoelder commented 2 years ago

It looks like snakefmt 0.4.4 adds wrong indentations when formatting a snakemake rule containing a if-else-closure.

As an example the following output will be created for a proper formatted rule:

--- original
+++ new
     params:
         datasources=(
             "-s {}".format(" ".join(config["annotations"]["dgidb"]["datasources"]))
-            if config["annotations"]["dgidb"].get("datasources", "")
-            else ""
+        if config["annotations"]["dgidb"].get("datasources", "")
+        else ""
         ),
     output:
         "results/calls/{prefix}.dgidb.bcf",

A minimal example is attached. Snakefile.zip

johanneskoester commented 2 years ago

This is the full rule:

rule annotate_dgidb:
    input:
        "results/calls/{prefix}.bcf",
    params:
        datasources=(
            "-s {}".format(" ".join(config["annotations"]["dgidb"]["datasources"]))
            if config["annotations"]["dgidb"].get("datasources", "")
            else ""
        ),
    output:
        "results/calls/{prefix}.dgidb.bcf",
    log:
        "logs/annotate-dgidb/{prefix}.log",
    conda:
        "../envs/rbt.yaml"
    resources:
        dgidb_requests=1,
    shell:
        "rbt vcf-annotate-dgidb {input} {params.datasources} > {output} 2> {log}"
johanneskoester commented 2 years ago

The part with the if config else is moved to the left.

mbhall88 commented 2 years ago

I think this is potentially connected to #115. I'm pretty vague on how the if-else parsing works. @bricoletc might be better able to figure this out.

bricoletc commented 2 years ago

Apologies I've got low bandwith at the moment, with my last year of thesis, (and @mbhall88 is defending his!) however i will take a look at it in the next few weeks

siebrenf commented 2 years ago

This also happens with nesting outside (but in combination with) rules. Here is a minimal example:

if True:
    def func1():
        """this function will stay indented"""
        pass

    rule break_snakefmt_044:
        shell: ""

    def func2():
        """this function will be unindented"""
        pass
corneliusroemer commented 2 years ago

I think this label could get a help wanted label? Assuming it's doable for a non-maintainer to tackle.

mbhall88 commented 2 years ago

Feel free to tackle this @corneliusroemer. It has been bugging me too. I'm still trying to figure out a couple of issues on #151 and then I was going to try and tackle this. But feel free to have a go 👍

mbhall88 commented 1 year ago

I wonder if anyone would like to test out #164 as that fixes the two examples in this issue?