Black is a popular code formatter for Python that enforces a consistent coding style by automatically formatting your code according to its opinionated style guide. However, Black doesn't provide a lot of configuration options for customization, as it's designed to minimize discussions about code formatting.
That being said, there are a few ways you can still make some tweaks to Black's behavior:
Line Length: By default, Black uses a line length of 88 characters. You can change this to a different value using the --line-length command-line option:
black --line-length 100 your_file.py
Skip Formatting: If you want to skip specific parts of your code from being formatted by Black, you can use the # fmt: off and # fmt: on comments:
# fmt: off # Your code here # fmt: on
Configuration Files: Black doesn't provide a traditional configuration file for customization, but you can still use pyproject.toml to pass some configuration options through the [tool.black] section. Although not all options are supported, some options like line-length and target-version can be set in the pyproject.toml file:
[tool.black] line-length = 100 target-version = ['py36', 'py37']
Remember that while Black might not support extensive customization, its design philosophy is to remove the need for discussions about code formatting by enforcing a single, consistent style. If you find yourself wanting to customize Black too much, it might be worth considering whether embracing its opinionated style guide is a better approach.
Python code to customize line length in Black formatter
# File: pyproject.toml [tool.black] line-length = 100
Description: To customize the line length in the Black formatter, you can specify the desired line length in the pyproject.toml file under the [tool.black] section. This example sets the line length to 100 characters.
Python code to exclude files from Black formatting
# File: pyproject.toml [tool.black] exclude = ''' /path/to/excluded/file.py /path/to/another/excluded/directory '''
Description: You can exclude specific files or directories from being formatted by Black by specifying them in the pyproject.toml file under the [tool.black] section. This example excludes file.py and directory from being formatted.
Python code to customize Black formatter's line wrapping behavior
# File: pyproject.toml [tool.black] skip-string-normalization = true
Description: To customize the line wrapping behavior of the Black formatter, you can set skip-string-normalization to true in the pyproject.toml file under the [tool.black] section. This example skips normalizing string quotes, which can affect line wrapping.
Python code to specify directories for Black formatter to format
# File: pyproject.toml [tool.black] include = ''' /path/to/directory1 /path/to/directory2 '''
Description: You can specify directories for the Black formatter to format by listing them in the pyproject.toml file under the [tool.black] section. This example specifies directory1 and directory2 for formatting.
Python code to configure Black formatter to use single quotes for strings
# File: pyproject.toml [tool.black] string-style = 'single'
Description: To configure the Black formatter to use single quotes for strings, you can set string-style to 'single' in the pyproject.toml file under the [tool.black] section. This example sets the string style to single quotes.
Python code to customize Black formatter's indentation
# File: pyproject.toml [tool.black] use-tabs = true
Description: You can customize the indentation behavior of the Black formatter by setting use-tabs to true or false in the pyproject.toml file under the [tool.black] section. This example configures Black to use tabs for indentation.
Python code to configure Black formatter to use double quotes for strings
# File: pyproject.toml [tool.black] string-style = 'double'
Description: To configure the Black formatter to use double quotes for strings, you can set string-style to 'double' in the pyproject.toml file under the [tool.black] section. This example sets the string style to double quotes.
Python code to customize Black formatter's blank line behavior
# File: pyproject.toml [tool.black] skip-string-normalization = true
Description: You can customize the blank line behavior of the Black formatter by setting skip-string-normalization to true in the pyproject.toml file under the [tool.black] section. This example skips normalizing string quotes, affecting blank lines.
Python code to customize Black formatter's sorting imports behavior
# File: pyproject.toml [tool.black] skip-string-normalization = true
Description: You can customize the sorting imports behavior of the Black formatter by setting skip-string-normalization to true in the pyproject.toml file under the [tool.black] section. This example skips normalizing string quotes, affecting import sorting.
Python code to configure Black formatter to use trailing commas
# File: pyproject.toml [tool.black] experimental-string-processing = true
Description: To configure the Black formatter to use trailing commas, you can set experimental-string-processing to true in the pyproject.toml file under the [tool.black] section. This example enables experimental string processing for trailing commas.
angularjs-validation yocto firebase-console stack-navigator django-views nuget-package-restore code-conversion splunk title-case datacontext