Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions elpy/blackutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def fix_code(code, directory):
# Get black config from pyproject.toml
line_length = black.DEFAULT_LINE_LENGTH
string_normalization = True
target_versions = set()
pyproject_path = os.path.join(directory, "pyproject.toml")
if toml is not None and os.path.exists(pyproject_path):
pyproject_config = toml.load(pyproject_path)
Expand All @@ -51,16 +52,25 @@ def fix_code(code, directory):
line_length = black_config["line-length"]
if "skip-string-normalization" in black_config:
string_normalization = not black_config["skip-string-normalization"]
if "target-version" in black_config:
target_versions = {
black.TargetVersion[v.upper()] for v in black_config["target-version"]
}

try:
if parse_version(black.__version__) < parse_version("19.0"):
reformatted_source = black.format_file_contents(
src_contents=code, line_length=line_length, fast=False)
src_contents=code, line_length=line_length, fast=False
)
else:
fm = black.FileMode(
line_length=line_length,
string_normalization=string_normalization)
string_normalization=string_normalization,
target_versions=target_versions,
)
reformatted_source = black.format_file_contents(
src_contents=code, fast=False, mode=fm)
src_contents=code, fast=False, mode=fm
)
return reformatted_source
except black.NothingChanged:
return code
Expand Down