From 322151f08467499755827d82ff3e7cf542b1115a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alja=C5=BE=20Mur=20Er=C5=BEen?= Date: Wed, 4 Jan 2023 14:58:11 +0200 Subject: [PATCH] Add `target-version` configuration file option --- src/darker/black_diff.py | 19 +++++++++++++++++++ src/darker/help.py | 2 ++ src/darker/tests/test_black_diff.py | 4 +++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/darker/black_diff.py b/src/darker/black_diff.py index 4db6bd0a5..d320f1fc5 100644 --- a/src/darker/black_diff.py +++ b/src/darker/black_diff.py @@ -72,10 +72,12 @@ 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 @@ -83,6 +85,7 @@ class BlackConfig(TypedDict, total=False): class BlackModeAttributes(TypedDict, total=False): """Type definition for items accepted by ``black.Mode``""" + target_versions: Set[TargetVersion] line_length: int string_normalization: bool @@ -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 @@ -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: diff --git a/src/darker/help.py b/src/darker/help.py index 411f280b0..762820693 100644 --- a/src/darker/help.py +++ b/src/darker/help.py @@ -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]" diff --git a/src/darker/tests/test_black_diff.py b/src/darker/tests/test_black_diff.py index dac35f256..713a9886a 100644 --- a/src/darker/tests/test_black_diff.py +++ b/src/darker/tests/test_black_diff.py @@ -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$'"],