snakemake / snakefmt

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

Else branch after conditional include breaks depending on subsequent code #239

Open lczech opened 4 months ago

lczech commented 4 months ago

I think this is a regression of https://github.com/snakemake/snakefmt/issues/115, or an extension of the issue reported there.

Minimal example, using snakefmt v0.10.2:

if condition:
    include: "my_rule.smk"
else:
    raise Exception("Condition not given")

if other_condition:
    rule other_rule:
        input:
            somefile

The error is:

snakefmt.exceptions.InvalidPython: Black error:
Cannot parse: 3:0: else:

Removing the second conditional part however solves this, despite that part not having anything to do with the condition that causes the problem.

My current workaround is to rewrite the else branch as

if not condition:
    raise Exception("Condition not given")

but since in my actual code I have a few elif befor that, this is quite cumbersome.

lczech commented 4 months ago

Just ran into a similar, but slightly different variant of this while trying to format another file.

Minimal example:

if condition_1:

    include: "rule_1.smk"

elif condition_2:

    pass

else:

    raise Exception("Invalid conditions")

Fails with

snakefmt.exceptions.InvalidPython: Black error:
Cannot parse: 1:0: elif condition_2:
(Note reported line number may be incorrect, as snakefmt could not determine the true line number)

Here, removing either the elif, or the else branch then works. However, interestingly, it also works to replace the pass under condition 2 by another include statement:

if condition_1:

    include: "rule_1.smk"

elif condition_2:

    include: "dummy.smk"

else:

    raise Exception("Invalid conditions")

This works.

mbhall88 commented 4 months ago

Oh, very weird. I'll take a look soon hopefully.