From 5dd37dcdce72a8f2f7f7bf13f7a219ec1606b3c9 Mon Sep 17 00:00:00 2001 From: Thomas H Date: Thu, 30 May 2024 10:26:20 -0400 Subject: [PATCH 1/8] Add preview tests --- src/darker/tests/test_command_line.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/darker/tests/test_command_line.py b/src/darker/tests/test_command_line.py index 9a16a6a99..b0a5e6965 100644 --- a/src/darker/tests/test_command_line.py +++ b/src/darker/tests/test_command_line.py @@ -207,6 +207,12 @@ def get_darker_help_output(capsys): expect_config=None, expect_modified=None, ), + dict( + argv=["--preview", "."], + expect_value=("preview", True), + expect_config=("preview", True), + expect_modified=("preview", True), + ), environ={}, ) def test_parse_command_line( @@ -444,6 +450,12 @@ def test_help_with_flynt_package(capsys): target_versions={TargetVersion.PY39}, ), ), + dict( + options=["--preview"], + expect=call( + preview=True, + ), + ), ) def test_black_options(monkeypatch, tmpdir, git_repo, options, expect): """Black options from the command line are passed correctly to Black""" @@ -553,6 +565,11 @@ def test_black_options(monkeypatch, tmpdir, git_repo, options, expect): options=["-t", "py39"], expect=call(target_versions={TargetVersion.PY39}), ), + dict( + config=["preview = true"], + options=["--preview"], + expect=call(target_versions={TargetVersion.PY39}), + ), ) def test_black_config_file_and_options(git_repo, config, options, expect): """Black configuration file and command line options are combined correctly""" @@ -644,6 +661,16 @@ def test_black_config_file_and_options(git_repo, config, options, expect): {"target_version": {"py39"}}, ), ), + dict( + options=["--preview", "a.py"], + expect=( + Path("git_root"), + {Path("a.py")}, + Exclusions(isort={"**/*"}, flynt={"**/*"}), + RevisionRange("HEAD", ":WORKTREE:"), + {"preview": True}, + ), + ), ) def test_options(git_repo, options, expect): """The main engine is called with correct parameters based on the command line From 15dc69e325a45f4dc595924e098cc68f15341e38 Mon Sep 17 00:00:00 2001 From: Thomas H Date: Thu, 30 May 2024 11:17:06 -0400 Subject: [PATCH 2/8] Fix test expectation --- src/darker/tests/test_command_line.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/darker/tests/test_command_line.py b/src/darker/tests/test_command_line.py index b0a5e6965..fb6eb5a3b 100644 --- a/src/darker/tests/test_command_line.py +++ b/src/darker/tests/test_command_line.py @@ -568,7 +568,7 @@ def test_black_options(monkeypatch, tmpdir, git_repo, options, expect): dict( config=["preview = true"], options=["--preview"], - expect=call(target_versions={TargetVersion.PY39}), + expect=call(preview=True), ), ) def test_black_config_file_and_options(git_repo, config, options, expect): From b26f2c468d94e9c79184f10025ab9d448715ec77 Mon Sep 17 00:00:00 2001 From: Thomas H Date: Thu, 30 May 2024 11:18:12 -0400 Subject: [PATCH 3/8] Implement --preview --- src/darker/__main__.py | 2 ++ src/darker/black_diff.py | 4 ++++ src/darker/command_line.py | 1 + src/darker/config.py | 1 + src/darker/help.py | 4 ++++ 5 files changed, 12 insertions(+) diff --git a/src/darker/__main__.py b/src/darker/__main__.py index cf4b72cad..252e74a7b 100644 --- a/src/darker/__main__.py +++ b/src/darker/__main__.py @@ -522,6 +522,8 @@ def main( # noqa: C901,PLR0912,PLR0915 black_config["skip_string_normalization"] = args.skip_string_normalization if args.skip_magic_trailing_comma is not None: black_config["skip_magic_trailing_comma"] = args.skip_magic_trailing_comma + if args.preview: + black_config["preview"] = args.preview paths, common_root = resolve_paths(args.stdin_filename, args.src) # `common_root` is now the common root of given paths, diff --git a/src/darker/black_diff.py b/src/darker/black_diff.py index dbb9ed644..d918dd288 100644 --- a/src/darker/black_diff.py +++ b/src/darker/black_diff.py @@ -76,6 +76,7 @@ class BlackConfig(TypedDict, total=False): line_length: int skip_string_normalization: bool skip_magic_trailing_comma: bool + preview: bool class BlackModeAttributes(TypedDict, total=False): @@ -109,6 +110,7 @@ def read_black_config(src: Tuple[str, ...], value: Optional[str]) -> BlackConfig "line_length", "skip_magic_trailing_comma", "skip_string_normalization", + "preview", ]: if key in raw_config: config[key] = raw_config[key] # type: ignore @@ -203,6 +205,8 @@ def run_black(src_contents: TextDocument, black_config: BlackConfig) -> TextDocu # ``black.Mode`` needs to be the opposite boolean of # ``skip-string-normalization``, hence the inverse boolean mode["string_normalization"] = not black_config["skip_string_normalization"] + if "preview" in black_config: + mode["preview"] = black_config["preview"] # The custom handling of empty and all-whitespace files below will be unnecessary if # https://github.com/psf/black/pull/2484 lands in Black. diff --git a/src/darker/command_line.py b/src/darker/command_line.py index 919826835..f6bc28980 100644 --- a/src/darker/command_line.py +++ b/src/darker/command_line.py @@ -39,6 +39,7 @@ def make_argument_parser(require_src: bool) -> ArgumentParser: add_arg(hlp.CHECK, "--check", action="store_true") add_arg(hlp.FLYNT, "-f", "--flynt", action="store_true") add_arg(hlp.ISORT, "-i", "--isort", action="store_true") + add_arg(hlp.PREVIEW, "--preview", action="store_true") add_lint_arg(parser) add_arg( hlp.SKIP_STRING_NORMALIZATION, diff --git a/src/darker/config.py b/src/darker/config.py index 1324901b0..8d7806cde 100644 --- a/src/darker/config.py +++ b/src/darker/config.py @@ -24,6 +24,7 @@ class DarkerConfig(BaseConfig, total=False): skip_magic_trailing_comma: bool line_length: int target_version: str + preview: bool class OutputMode: diff --git a/src/darker/help.py b/src/darker/help.py index 380681ae3..3b3f886be 100644 --- a/src/darker/help.py +++ b/src/darker/help.py @@ -133,6 +133,10 @@ def get_extra_instruction(dependency: str) -> str: " `skip-magic-trailing-comma = true` from a Black configuration file." ) +PREVIEW = ( + "Use Black's experimental preview style" +) + LINE_LENGTH = "How many characters per line to allow [default: 88]" TARGET_VERSION = ( From ae9b9a8a64042207193ac729a3602c864a1c289c Mon Sep 17 00:00:00 2001 From: Thomas H Date: Thu, 30 May 2024 11:20:08 -0400 Subject: [PATCH 4/8] Apply black formatting --- src/darker/help.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/darker/help.py b/src/darker/help.py index 3b3f886be..24c5290f2 100644 --- a/src/darker/help.py +++ b/src/darker/help.py @@ -133,9 +133,7 @@ def get_extra_instruction(dependency: str) -> str: " `skip-magic-trailing-comma = true` from a Black configuration file." ) -PREVIEW = ( - "Use Black's experimental preview style" -) +PREVIEW = "Use Black's experimental preview style" LINE_LENGTH = "How many characters per line to allow [default: 88]" From 87b30ed687cd5d333b8c7172cc3e37f08da7cb1b Mon Sep 17 00:00:00 2001 From: Thomas H Date: Thu, 30 May 2024 11:29:38 -0400 Subject: [PATCH 5/8] Update project information --- CHANGES.rst | 2 ++ README.rst | 5 +++++ contributors.yaml | 2 ++ 3 files changed, 9 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 13b40b9a0..285ecfc21 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,6 +10,8 @@ Added - Display exit code in parentheses after error message. - Do not reformat renamed files. - CI workflow to post recent project activity in a discussion. Triggered manually. +- The `--preview` configuration flag is now supported in Darker CLI and Black + configuration files. Fixed ----- diff --git a/README.rst b/README.rst index 39b191e0f..1cff3204b 100644 --- a/README.rst +++ b/README.rst @@ -358,6 +358,8 @@ The following `command line arguments`_ can also be used to modify the defaults: Also convert string formatting to use f-strings using the ``flynt`` package -i, --isort Also sort imports using the ``isort`` package +--preview + Use Black's experimental preview style -L CMD, --lint CMD Run a linter on changed files. ``CMD`` can be a name or path of the linter binary, or a full quoted command line with the command and options. Linters read @@ -413,6 +415,7 @@ An example ``pyproject.toml`` configuration file: line-length = 80 # Passed to isort and Black, override their config target-version = ["py312"] # Passed to Black, overriding its config log_level = "INFO" + preview = false # Passed to Black, overriding its config [tool.black] line-length = 88 # Overridden by [tool.darker] above @@ -422,6 +425,8 @@ An example ``pyproject.toml`` configuration file: exclude = "test_*\.py" extend_exclude = "/generated/" force_exclude = ".*\.pyi" + preview = true # Overridden above + [tool.isort] profile = "black" diff --git a/contributors.yaml b/contributors.yaml index 16f13d158..850a1b193 100644 --- a/contributors.yaml +++ b/contributors.yaml @@ -203,6 +203,8 @@ talhajunaidd: - {link_type: commits, type: Code} tapted: - {link_type: issues, type: Bug reports} +tehunter: + - {link_type: pulls-author, type: Code} tgross35: - {link_type: issues, type: Bug reports} tkolleh: From 62fab63c28986848a5edca3a6cda8bf7b071ef4c Mon Sep 17 00:00:00 2001 From: Thomas H Date: Thu, 1 Aug 2024 11:41:47 -0400 Subject: [PATCH 6/8] Apply suggestions from code review Co-authored-by: Antti Kaihola <13725+akaihola@users.noreply.github.com> --- CHANGES.rst | 4 ++-- README.rst | 3 ++- contributors.yaml | 2 -- src/darker/help.py | 5 ++++- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 285ecfc21..141a7510d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,8 +10,8 @@ Added - Display exit code in parentheses after error message. - Do not reformat renamed files. - CI workflow to post recent project activity in a discussion. Triggered manually. -- The `--preview` configuration flag is now supported in Darker CLI and Black - configuration files. +- The ``--preview`` configuration flag is now supported in the configuration files for + Darker and Black Fixed ----- diff --git a/README.rst b/README.rst index 1cff3204b..d25a07e20 100644 --- a/README.rst +++ b/README.rst @@ -359,7 +359,8 @@ The following `command line arguments`_ can also be used to modify the defaults: -i, --isort Also sort imports using the ``isort`` package --preview - Use Black's experimental preview style + In Black, enable potentially disruptive style changes that may be added to Black + in the future -L CMD, --lint CMD Run a linter on changed files. ``CMD`` can be a name or path of the linter binary, or a full quoted command line with the command and options. Linters read diff --git a/contributors.yaml b/contributors.yaml index 850a1b193..16f13d158 100644 --- a/contributors.yaml +++ b/contributors.yaml @@ -203,8 +203,6 @@ talhajunaidd: - {link_type: commits, type: Code} tapted: - {link_type: issues, type: Bug reports} -tehunter: - - {link_type: pulls-author, type: Code} tgross35: - {link_type: issues, type: Bug reports} tkolleh: diff --git a/src/darker/help.py b/src/darker/help.py index 24c5290f2..96890c638 100644 --- a/src/darker/help.py +++ b/src/darker/help.py @@ -133,7 +133,10 @@ def get_extra_instruction(dependency: str) -> str: " `skip-magic-trailing-comma = true` from a Black configuration file." ) -PREVIEW = "Use Black's experimental preview style" +PREVIEW = ( + "In Black, enable potentially disruptive style changes that may be added to Black" + " in the future" +) LINE_LENGTH = "How many characters per line to allow [default: 88]" From a3b647eaea6e9f2dd6b1395e0a64b0bc8f8d5cdd Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Thu, 8 Aug 2024 22:33:52 +0300 Subject: [PATCH 7/8] feat: only support `preview =` in `[tool.black]` Avoids confusion about precendence. --- README.rst | 3 +-- src/darker/config.py | 1 - src/darker/tests/test_command_line.py | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index d25a07e20..5121952ce 100644 --- a/README.rst +++ b/README.rst @@ -416,7 +416,6 @@ An example ``pyproject.toml`` configuration file: line-length = 80 # Passed to isort and Black, override their config target-version = ["py312"] # Passed to Black, overriding its config log_level = "INFO" - preview = false # Passed to Black, overriding its config [tool.black] line-length = 88 # Overridden by [tool.darker] above @@ -426,7 +425,7 @@ An example ``pyproject.toml`` configuration file: exclude = "test_*\.py" extend_exclude = "/generated/" force_exclude = ".*\.pyi" - preview = true # Overridden above + preview = true # Only supported in [tool.black] [tool.isort] diff --git a/src/darker/config.py b/src/darker/config.py index 8d7806cde..1324901b0 100644 --- a/src/darker/config.py +++ b/src/darker/config.py @@ -24,7 +24,6 @@ class DarkerConfig(BaseConfig, total=False): skip_magic_trailing_comma: bool line_length: int target_version: str - preview: bool class OutputMode: diff --git a/src/darker/tests/test_command_line.py b/src/darker/tests/test_command_line.py index fb6eb5a3b..e8940afda 100644 --- a/src/darker/tests/test_command_line.py +++ b/src/darker/tests/test_command_line.py @@ -305,6 +305,19 @@ def test_parse_command_line_deprecated_option( assert {c.args[0] for c in warn.call_args_list} == expect_warn +def test_parse_command_line_unknown_conffile_option(tmp_path, monkeypatch): + """`parse_command_line` warns about deprecated configuration options.""" + monkeypatch.chdir(tmp_path) + config = {"unknown": "value", "preview": "true"} + (tmp_path / "pyproject.toml").write_text(toml.dumps({"tool": {"darker": config}})) + with pytest.raises( + ConfigurationError, + match=r"Invalid \[tool.darker\] keys in pyproject.toml: preview, unknown", + ): + + parse_command_line(["-"]) + + def test_help_description_without_isort_package(capsys): """``darker --help`` description shows how to add ``isort`` if it's not present""" with isort_present(False): @@ -565,6 +578,16 @@ def test_black_options(monkeypatch, tmpdir, git_repo, options, expect): options=["-t", "py39"], expect=call(target_versions={TargetVersion.PY39}), ), + dict( + config=["preview = true"], + options=[], + expect=call(preview=True), + ), + dict( + config=["preview = false"], + options=["--preview"], + expect=call(preview=True), + ), dict( config=["preview = true"], options=["--preview"], From 15c3ff0f774b0ca3abf3a6b92dab19212c3f2b72 Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Sun, 25 Aug 2024 22:10:41 +0300 Subject: [PATCH 8/8] fix: add preview in BlackModeAttributes TypedDict --- src/darker/black_diff.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/darker/black_diff.py b/src/darker/black_diff.py index d918dd288..b9c106648 100644 --- a/src/darker/black_diff.py +++ b/src/darker/black_diff.py @@ -87,6 +87,7 @@ class BlackModeAttributes(TypedDict, total=False): string_normalization: bool is_pyi: bool magic_trailing_comma: bool + preview: bool def read_black_config(src: Tuple[str, ...], value: Optional[str]) -> BlackConfig: