snakefmt icon indicating copy to clipboard operation
snakefmt copied to clipboard

snakefmt adds wrong indentation

Open FelixMoelder opened this issue 3 years ago • 7 comments
trafficstars

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

FelixMoelder avatar Nov 22 '21 10:11 FelixMoelder

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 avatar Nov 30 '21 14:11 johanneskoester

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

johanneskoester avatar Nov 30 '21 14:11 johanneskoester

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.

mbhall88 avatar Dec 01 '21 22:12 mbhall88

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

bricoletc avatar Dec 02 '21 09:12 bricoletc

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

siebrenf avatar Dec 06 '21 14:12 siebrenf

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

corneliusroemer avatar Sep 06 '22 20:09 corneliusroemer

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 avatar Sep 06 '22 23:09 mbhall88

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

mbhall88 avatar Dec 08 '22 02:12 mbhall88