Skip to content

Commit 2263682

Browse files
author
Gaetano Guerriero
committed
black: propagate target_versions from pyproject.toml config
1 parent 2203597 commit 2263682

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

elpy/blackutil.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def fix_code(code, directory):
4343
# Get black config from pyproject.toml
4444
line_length = black.DEFAULT_LINE_LENGTH
4545
string_normalization = True
46+
target_versions = set()
4647
pyproject_path = os.path.join(directory, "pyproject.toml")
4748
if toml is not None and os.path.exists(pyproject_path):
4849
pyproject_config = toml.load(pyproject_path)
@@ -51,16 +52,25 @@ def fix_code(code, directory):
5152
line_length = black_config["line-length"]
5253
if "skip-string-normalization" in black_config:
5354
string_normalization = not black_config["skip-string-normalization"]
55+
if "target-version" in black_config:
56+
target_versions = {
57+
black.TargetVersion[v.upper()] for v in black_config["target-version"]
58+
}
59+
5460
try:
5561
if parse_version(black.__version__) < parse_version("19.0"):
5662
reformatted_source = black.format_file_contents(
57-
src_contents=code, line_length=line_length, fast=False)
63+
src_contents=code, line_length=line_length, fast=False
64+
)
5865
else:
5966
fm = black.FileMode(
6067
line_length=line_length,
61-
string_normalization=string_normalization)
68+
string_normalization=string_normalization,
69+
target_versions=target_versions,
70+
)
6271
reformatted_source = black.format_file_contents(
63-
src_contents=code, fast=False, mode=fm)
72+
src_contents=code, fast=False, mode=fm
73+
)
6474
return reformatted_source
6575
except black.NothingChanged:
6676
return code

0 commit comments

Comments
 (0)