snakefmt icon indicating copy to clipboard operation
snakefmt copied to clipboard

Comment in multi-line parenthesis breaks Black

Open lczech opened this issue 1 year ago • 4 comments

This might be an issue with Black, but not sure.

Minimal example:

rule my_rule:
    input:
        data=(
            # a comment on the below
            "input_1.txt"
            if condition
            else "input_2.txt"
        )

Obviously, this is not good style, and the comment should go before the the data line anyway - which is what I'll do to fix this. However, it still should not break the formatting.

Error:

snakefmt.exceptions.InvalidPython: Black error:
Cannot parse: 2:0: EOF in multi-line statement

Python itself does not complain about this, and so I think neither should do the formatter, despite the bad style, and instead just simply move the comment up, as it does in similar situations without parenthesis.

lczech avatar Jul 11 '24 16:07 lczech

I suspect this is not a black issue, but an issue with the way we pass it to black.

mbhall88 avatar Jul 11 '24 22:07 mbhall88

Another example of the same problem:

rule a:
    input:
        expand(
            "foo",
            test=[1, 2, 3], # comment
        )

johanneskoester avatar Mar 31 '25 10:03 johanneskoester

As this is quite a common case, @mbhall88 do you have a pointer for us how to fix this?

johanneskoester avatar Mar 31 '25 10:03 johanneskoester

I think @bricoletc did most of the work dealing with comments…

Off the top,of my head I wouldn’t know. Essentially what I normally do to fix these things is add a failing minimal test case and then walk through with the debugger to find where in the code the comment is causing issues.

Sorry, am on parental leave so don’t have time to debug this myself at the moment

mbhall88 avatar Apr 02 '25 20:04 mbhall88