pythonruff

How to prevent ruff from formatting arguments of a function into separate lines?


I have a function like so:

def get_foo(a: object, b: tuple, c: int,) -> dict:
    .....

When I do $ ruff format myfile.py, my function is changed to

def get_foo(
    a: object,
    b: tuple,
    c: int,
) -> dict:
    ....

How do I stop this behaviour?

Update: @STerliakov I implemented your solution but received this warning.

$ ruff format test.py
warning: The isort option `isort.split-on-trailing-comma` is incompatible with the formatter `format.skip-magic-trailing-comma=true` option. To avoid unexpected behavior, we recommend either setting `isort.split-on-trailing-comma=false` or `format.skip-magic-trailing-comma=false`.
1 file reformatted

I can't locate isort.split-on-trailing-comma to set it to false to avoid the conflict. How do i fix this issue?


Solution

  • That happens due to the trailing comma in your arguments list. This behavior is intentional. You can disable it globally using skip-magic-trailing-comma.

    E.g. in pyproject.toml that would be

    [tool.ruff.format]
    skip-magic-trailing-comma = true
    

    Enabling this setting is incompatible with import sorter default configuration. To ignore trailing commas in your imports as well, set isort.split-on-trailing-comma to false:

    [tool.ruff.lint.isort]
    split-on-trailing-comma = false