astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32k stars 1.07k forks source link

fmt: off doesn't work in `parametrized` decorator #9875

Open mdczaplicki opened 8 months ago

mdczaplicki commented 8 months ago

I have a test that is heavily parametrized. Like this:

@pytest.mark.parametrize(
    (
        "business_x_y_fully_doing_something",
        "business_x_y_not_doing_something",
        "business_v_y_who_knows_doing_something",
        "business_v_y_fully_doing_something",
        "business_z_y_fully_doing_something",
        "business_z_y_not_doing_something",
        "expected_business_x_starting_something",
        "expected_business_x_stopping_something",
        "expected_business_v_starting_something",
        "expected_business_v_stopping_something",
        "expected_business_z_fully_doing_something",
        "expected_business_z_not_doing_something",
        "business_z_doing_threshold",
        "business_z_doing_limit",
    ),
    [
        # fmt: off
        (0 , 29, 0 , 0 , 0 , 0 , 27, 0 , 0 , 0 , 0 , 0 , None, 100),
        (5 , 24, 0 , 0 , 0 , 0 , 22, 0 , 0 , 0 , 0 , 0 , None, 100),
        (30, 0 , 0 , 0 , 0 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , None, 100),
        (28, 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , None, 100),
        (0 , 29, 10, 0 , 0 , 0 , 25, 0 , 10, 0 , 0 , 0 , None, 100),
        (30, 0 , 0 , 10, 0 , 0 , 0 , 3 , 0 , 10, 0 , 0 , None, 100),
        (27, 0 , 0 , 4 , 0 , 0 , 0 , 0 , 0 , 4 , 0 , 0 , None, 100),
        (10, 0 , 0 , 0 , 0 , 10, 0 , 0 , 0 , 0 , 10, 0 , None, 100),
        (15, 5 , 0 , 0 , 4 , 5 , 5 , 0 , 0 , 0 , 7 , 2 , None, 100),
        (25, 0 , 0 , 0 , 0 , 10, 0 , 0 , 0 , 0 , 0 , 10, 50  , 100),
        (15, 5 , 0 , 0 , 4 , 5 , 5 , 0 , 0 , 0 , 5 , 4 , None, 1  ),
        # fmt: on
    ],
)

I want to maintain the readability of this by having exactly the same amount of distance between each value. I doesn't work with # fmt: off inside the decorator. In order for this to work - I'd have to surround whole test in a fmt: off, which I don't want to do.

MichaReiser commented 8 months ago

Thank's @mdczaplicki for opening the issue. I understand that you don't like Ruff splitting the array after each element. I, unfortunately, haven't had a lot of time working on it but we've started a discussion around formatting arrays in https://github.com/astral-sh/ruff/discussions/8452 I want to keep this issue open because it is slightly different in that the list elements aren't sub-lists but tuples.

One escape hatch for now is that you can add a fmt: skip comment at the end of the decorator. It's not semantically identical to using a fmt:off - fmt:on region because it also disables formatting of the rest of the decorator. But it might help unblock you for now. See this playground

mdczaplicki commented 8 months ago

Thanks @MichaReiser, fmt: skip works just fine. Now I can switch from black 😁