Ruff code formatter and pre-commit hooks

Dear Foundry community & Palantir engineers

Through our CI/CD pipelines we want to implement a way to automatically ensure that all PRs are following a defined code formatting principle. We are interrested to implement a code formatting and check through Ruff.

Does a template or any example of Ruff setup over a code repository exist ? Does AIP assist would be able to bring some formatting rules through code sample ?

Does pre-commit hooks can be implemented to ensure that a raised PR have passed the formatting expectations ?

4 Likes

Hello!

You are able to leverage a gradle plugin which will run the Ruff formatter during checks. You can enable this by adding the following to the build.gradle file in the root of your code repository (You may have to enable “Show hidden files and folders”):

apply plugin: 'com.palantir.transforms.lang.format-code-ruff'

1 Like

Hi Felix,

This mean adding as well ruff library from conda or this is already available through Foundry libs ?
Is there any way to custom the “default” ruff formatting by adding rules ? It seems too but do you have example of ruff customization rules as we have some code formatting concepts define that we want to keep implementend but taking advantage of a ruff formatter.

Hi,

You can customise Ruff in any of the standard ways (e.g. pyproject.toml, ruff.toml) and Checks will respect the configuration. See the Ruff documentation (https://docs.astral.sh/ruff/configuration/) for details.

1 Like

Hello everyone,

we encountered a problem, while setting up a common formatting guideline for code repositories in our group.

While many of us are used to format all files before committing using the shortcut Alt+Shift+F, others use the “Format before Committing” option. We now realized that those are using different formatting rules, no matter whether we choose ruff oder black in build.gradle.

Is there a way to configure the formatting rules of the Alt+Shift+F command?

Best,
Alex

1 Like

+1 to @A_Klapp 's point. It would be nice to configure what happens during “Alt-Shift-F”. in my experience, I found that Ruff or Black formatters mismatch what Alt-Shift-F does in major two ways:

  • only Alt-Shift-F does the import sorting (it seems to be isort)
  • Ruff/Black uses 88 character limit for the line lengths (quite common for python), whereas Alt-Shift-F uses 120

@felix , is there a way we could configure this behaviour? or maybe at least have an import sorting available through a different shortcut?

1 Like