Black
Black, the Python formatter.
- Formats
- Python
- In
ur.toml [format.python] style = "black"- Its options
- https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html
Formatted
cart.py, as written# What a cart costs.
def subtotal(items,tax = 0.2):
return sum( item.price*item.quantity for item in items )*(1+tax)
def label(item): return item.name+' x'+str(item.quantity)
ur format --style black cart.py# What a cart costs.
def subtotal(items, tax=0.2):
return sum(item.price * item.quantity for item in items) * (1 + tax)
def label(item):
return item.name + " x" + str(item.quantity)
Defaults
The shared options, as Black sets them when nothing configures it. Its own names for them are read from its configuration files, but ur.toml sets them by the shared name only.
| Option | Default | Black calls it |
|---|---|---|
width | 88 | line-length |
indent.width | 4 | |
indent.style | ||
line-ending | "auto" | |
trailing-comma.add | "multiline" | |
trailing-comma.forces-break | true | skip-magic-trailing-comma |
quotes | "double" | skip-string-normalization |
target | target-version |
Its own options
Set in a table named for it, such as [format.black], or in a language's table, as black = { ... }.
| Option | Default | What it does |
|---|---|---|
pyi | false | Format every file as a typing stub, as .pyi files are formatted. Takes true or false. |
ipynb | false | Format every file as a Jupyter notebook. Takes true or false. |
python-cell-magics | [] | Notebook cell magics, such as %%time, whose cells are formatted as Python. Takes a list of strings. |
skip-source-first-line | false | Leave each file's first line as written, for a first line that is not Python. Takes true or false. |
preview | false | Format in Black's preview style, the changes planned for its next stable style. Takes true or false. |
unstable | false | Format in the preview style with its unstable features as well. Takes true or false. |
required-version | | The Black version the project requires, which Black checks before it formats. Takes any value. |
enable-unstable-feature | [] | Unstable preview features to turn on one at a time. Takes a list of "string_processing", "hug_parens_with_braces_and_square_brackets", "wrap_comprehension_in", "simplify_power_operator_hugging", "wrap_long_dict_values_in_parens", "fix_if_guard_explosion_in_case_statement", "pyi_overload_group_blank_lines", "pyi_blank_line_before_decorated_class". |