Is it possible to reformat python __all__ statement with ruff to make it multiline?

53 views Asked by At

I'm using ruff (https://docs.astral.sh/ruff/). Is it possible to format the code from this:

__all__ = ["Model", "User", "Account"]

Into this?

__all__ = [
    "Model",
    "User",
    "Account"
]
1

There are 1 answers

1
Михаил Павлов On

So I found the answer. ruff has amazing feature called magic-trailing-comma. If you want your list, no matter how long is it, to be one line just don't put comma after the last element:

def some_func(a, b, c):
    ...
__all__ = ["a", "b", "c"]

In case you want your function args or list elements to be one per line just put a magic trailing comma after the last element:

def some_func(
    a, 
    b, 
    c,
):
    ...
__all__ = [
    "a",
    "b",
    "c",
]