Linting & Formatting
Python1
We use the Ruff2 formatter to both lint and format our Python code. This is the same system that is used in Databricks, though the globally applied settings are currently unknown to us. Custom settings can be applied using the pyproject.toml file under the [tool.ruff] section. For example:
[tool.ruff]
line-length = 88
builtins = ["dbutils", "spark"]
exclude = [
"path/to/file.py"
]
Currently, custom settings are not working on Databricks, but they should still apply when working locally in your IDE.
To install and use ruff locally, you can use the following steps:
- Make sure you have
poetryinstalled on your system (documentation forthcoming) poetry add ruff- Edit your
pyproject.tomlfile to include a[tool.ruff]section like that above^ poetry run ruff checkto see whether your file formats are correct.- This is what's used as part of our current CI build process
poetry run ruff check --fixto letruffapply any easy fixes that it can find- Examples might include unused imports or extra whitespace at the end of lines
Note
To run ruff on Databricks, use the Edit > Format document option or press Ctrl + Shift + F. This may take a while (up to a minute) to run, so be patient. For notebooks, you can use Edit > Format notebook
SQL3
All Databricks SQL formatting settings must be stored in a file under your Workspace's home directory called .dbsql-formatter-config.json. The settings will apply globally across all SQL scripts you create. Here is a list of options that can be set:
{
"printWidth": 80,
"indentationStyle": "spaces",
"indentationWidth": 4,
"keywordCasing": "uppercase",
"functionNameCasing": "lowercase",
"commaPosition": "end",
"numNewLinesBetweenStatements": 1,
"numNewLinesBetweenClauses": 0,
"shouldExpandExpressions": true,
"shouldExpandCaseStatements": true,
"shouldExpandInStatements": false,
"shouldExpandBetweenConditions": false,
"shouldBreakOnJoinSections": true
}
- Fields:
printWidth: Max line length, defaults to 100indentationStyle:"tabs"or"spaces"for indentation, default"spaces"indentationWidth: Number of spaces when indentationStyle is set to"spaces", default2keywordCasing: SQL keywords in"uppercase"or"lowercase"functionNameCasing: SQL functions casing as"uppercase"or"lowercase"commaPosition: Comma placement in lists at the"beginning"or"end", default"end"numNewLinesBetweenStatements: Number of lines between separate statements, default1numNewLinesBetweenClauses: Number of lines between clauses within a statement, default0shouldExpandExpressions:trueif boolean expressions should be on separate lines, defaults totrueshouldExpandCaseStatements:trueif CASE statements should be on separate lines, defaults totrueshouldExpandInStatements:trueif items in anINselection should be on separate lines, defaults tofalseshouldExpandBetweenConditions:trueif items in aBETWEENselection should be on separate lines, defaults tofalseshouldBreakOnJoinSections:trueifJOIN'sONconditions should be on separate lines, defaults totrue