Skip to content

Commit

Permalink
Add target-version configuration file option
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen authored and akaihola committed Jan 6, 2023
1 parent b8c02b7 commit 322151f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/darker/black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,20 @@

class BlackConfig(TypedDict, total=False):
"""Type definition for Black configuration dictionaries"""

config: str
exclude: Pattern[str]
extend_exclude: Pattern[str]
force_exclude: Pattern[str]
target_version: str
line_length: int
skip_string_normalization: bool
skip_magic_trailing_comma: bool


class BlackModeAttributes(TypedDict, total=False):
"""Type definition for items accepted by ``black.Mode``"""

target_versions: Set[TargetVersion]
line_length: int
string_normalization: bool
Expand Down Expand Up @@ -114,6 +117,17 @@ def read_black_config(src: Tuple[str, ...], value: Optional[str]) -> BlackConfig
]:
if key in raw_config:
config[key] = raw_config[key] # type: ignore
if "target_version" in raw_config:
target_version = raw_config["target_version"]
if isinstance(target_version, str):
config["target_version"] = target_version
elif isinstance(target_version, list):
# Convert TOML list to a Python set
config["target_version"] = set(target_version)
else:
raise ConfigurationError(
f"Invalid target-version = {target_version!r} in {value}"
)
for key in ["exclude", "extend_exclude", "force_exclude"]:
if key in raw_config:
config[key] = re_compile_maybe_verbose(raw_config[key]) # type: ignore
Expand Down Expand Up @@ -174,6 +188,11 @@ def run_black(src_contents: TextDocument, black_config: BlackConfig) -> TextDocu
mode = BlackModeAttributes()
if "line_length" in black_config:
mode["line_length"] = black_config["line_length"]
if "target_version" in black_config:
ver_str = black_config["target_version"]
ver = next((v for v in TargetVersion if v.name.lower() == ver_str), None)
if ver:
mode["target_versions"] = {ver}
if "skip_magic_trailing_comma" in black_config:
mode["magic_trailing_comma"] = not black_config["skip_magic_trailing_comma"]
if "skip_string_normalization" in black_config:
Expand Down
2 changes: 2 additions & 0 deletions src/darker/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@

LINE_LENGTH = "How many characters per line to allow [default: 88]"

TARGET_VERSION = "Python versions that should be supported. [default: py37]"

WORKERS = "How many parallel workers to allow, or `0` for one per core [default: 1]"
4 changes: 3 additions & 1 deletion src/darker/tests/test_black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def __eq__(self, other):
config_lines=["skip-magic-trailing-comma = false"],
expect={"skip_magic_trailing_comma": False},
),
dict(config_lines=["target-version = ['py37']"], expect={}),
dict(
config_lines=["target-version = ['py37']"], expect={"target_version": ["py37"]}
),
dict(config_lines=[r"include = '\.pyi$'"], expect={}),
dict(
config_lines=[r"exclude = '\.pyx$'"],
Expand Down

0 comments on commit 322151f

Please sign in to comment.