From f3145802fba7d96c56be665e2c05731c2394cda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Clgen=20Sar=C4=B1kavak?= Date: Sun, 28 Jul 2024 10:20:50 +0300 Subject: [PATCH 1/2] Remove yapf All formatting & linting will be handled by ruff --- .pre-commit-config.yaml | 5 ----- pyproject.toml | 10 ---------- 2 files changed, 15 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f5d3b9f..377d687 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,11 +12,6 @@ repos: - id: mixed-line-ending - id: trailing-whitespace - - repo: https://github.com/google/yapf - rev: v0.40.1 - hooks: - - id: yapf - - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.0.284 hooks: diff --git a/pyproject.toml b/pyproject.toml index b1a6d52..4daffc9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -106,16 +106,6 @@ force_sort_within_sections = true force_to_top = ["typing", "pytest"] line_length = 120 -[tool.yapf] -based_on_style = "pep8" -column_limit = 120 -blank_line_before_module_docstring = true -coalesce_brackets = false -dedent_closing_brackets = true -split_arguments_when_comma_terminated = true -split_before_dict_set_generator = true -allow_split_before_dict_value = false - [tool.pytest.ini_options] testpaths = ["tests"] From 7963bf898765262679af8ace948a23d28c5dafca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Clgen=20Sar=C4=B1kavak?= Date: Sun, 28 Jul 2024 10:22:21 +0300 Subject: [PATCH 2/2] Update ruff and its config --- .pre-commit-config.yaml | 5 ++++- pyproject.toml | 9 ++++++--- src/click_option_group/_core.py | 14 ++++++++++---- src/click_option_group/_decorators.py | 9 ++++----- tests/test_click_option_group.py | 23 +++++++---------------- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 377d687..6f7736f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,7 +13,10 @@ repos: - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.0.284 + rev: v0.5.5 hooks: - id: ruff + name: ruff lint args: ["--fix", "--show-fixes"] + - id: ruff-format + name: ruff format diff --git a/pyproject.toml b/pyproject.toml index 4daffc9..364bdc4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,7 +65,11 @@ version.source = "vcs" build.hooks.vcs.version-file = "src/click_option_group/_version.py" [tool.ruff] +exclude = [] line-length = 120 +src = ["src"] + +[tool.ruff.lint] select = [ "E", "F", "W", # flake8 "B", # flake8-bugbear @@ -95,16 +99,15 @@ extend-ignore = [ "PT012", # Block should contain a single simple statement "RUF009", # Too easy to get a false positive ] -src = ["src"] unfixable = ["T20", "F841"] -exclude = [] [tool.isort] +# Even though ruff is used as formatter, isort profile name is "black". +profile = "black" multi_line_output = 3 include_trailing_comma = true force_sort_within_sections = true force_to_top = ["typing", "pytest"] -line_length = 120 [tool.pytest.ini_options] testpaths = ["tests"] diff --git a/src/click_option_group/_core.py b/src/click_option_group/_core.py index f87a356..0778ac4 100644 --- a/src/click_option_group/_core.py +++ b/src/click_option_group/_core.py @@ -61,8 +61,12 @@ def group(self) -> "OptionGroup": """ return self.__group - def handle_parse_result(self, ctx: click.Context, opts: Mapping[str, Any], - args: List[str]) -> Tuple[Any, List[str]]: + def handle_parse_result( + self, + ctx: click.Context, + opts: Mapping[str, Any], + args: List[str], + ) -> Tuple[Any, List[str]]: with augment_usage_errors(ctx, param=self): if not ctx.resilient_parsing: self.group.handle_parse_result(self, ctx, opts) @@ -390,7 +394,9 @@ def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: M group_name = self._group_name_str() option_info = self.get_error_hint(ctx) - msg = f"Missing one of the required mutually exclusive options from {group_name} option group:\n{option_info}" + msg = ( + f"Missing one of the required mutually exclusive options from {group_name} option group:\n{option_info}" + ) raise click.UsageError( msg, ctx=ctx, @@ -415,7 +421,7 @@ def name_extra(self) -> List[str]: def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: Mapping[str, Any]) -> None: option_names = set(self.get_options(ctx)) - if (not option_names.isdisjoint(opts) and option_names.intersection(opts) != option_names): + if not option_names.isdisjoint(opts) and option_names.intersection(opts) != option_names: group_name = self._group_name_str() option_info = self.get_error_hint(ctx) diff --git a/src/click_option_group/_decorators.py b/src/click_option_group/_decorators.py index df20345..864d64a 100644 --- a/src/click_option_group/_decorators.py +++ b/src/click_option_group/_decorators.py @@ -111,10 +111,9 @@ def group( if not cls: cls = OptionGroup - else: - if not issubclass(cls, OptionGroup): - msg = "'cls' must be a subclass of 'OptionGroup' class." - raise TypeError(msg) + elif not issubclass(cls, OptionGroup): + msg = "'cls' must be a subclass of 'OptionGroup' class." + raise TypeError(msg) def decorator(func): callback, params = get_callback_and_params(func) @@ -183,7 +182,7 @@ def help_option(self, *param_decls, **attrs) -> Decorator: the command's help text and exits. """ if not param_decls: - param_decls = ("--help", ) + param_decls = ("--help",) attrs.setdefault("is_flag", True) attrs.setdefault("is_eager", True) diff --git a/tests/test_click_option_group.py b/tests/test_click_option_group.py index e58f0d1..805eca4 100644 --- a/tests/test_click_option_group.py +++ b/tests/test_click_option_group.py @@ -16,7 +16,6 @@ def test_basic_functionality_first_api(runner): - @click.command() @click.option("--hello") @optgroup("Group 1", help="Group 1 description") @@ -45,7 +44,6 @@ def cli(hello, foo1, bar1, lol, foo2, bar2, goodbye): def test_noname_group(runner): - @click.command() @optgroup() @optgroup.option("--foo") @@ -99,7 +97,6 @@ def cli3(**params): def test_missing_group_decl_first_api(runner): - @click.command() @click.option("--hello1") @optgroup.option("--foo") @@ -111,7 +108,7 @@ def cli(**params): result = runner.invoke(cli, ["--help"]) assert result.exception - assert TypeError == result.exc_info[0] + assert isinstance(result.exception, TypeError) assert "Missing option group decorator" in str(result.exc_info[1]) assert "--foo" in str(result.exc_info[1]) assert "--bar" in str(result.exc_info[1]) @@ -119,7 +116,7 @@ def cli(**params): result = runner.invoke(cli, []) assert result.exception - assert TypeError == result.exc_info[0] + assert isinstance(result.exception, TypeError) assert "Missing option group" in str(result.exc_info[1]) assert "--foo" in str(result.exc_info[1]) assert "--bar" in str(result.exc_info[1]) @@ -127,7 +124,7 @@ def cli(**params): result = runner.invoke(cli, ["--hello1", "hello1"]) assert result.exception - assert TypeError == result.exc_info[0] + assert isinstance(result.exception, TypeError) assert "Missing option group" in str(result.exc_info[1]) assert "--foo" in str(result.exc_info[1]) assert "--bar" in str(result.exc_info[1]) @@ -135,7 +132,7 @@ def cli(**params): result = runner.invoke(cli, ["--foo", "foo"]) assert result.exception - assert TypeError == result.exc_info[0] + assert isinstance(result.exception, TypeError) assert "Missing option group" in str(result.exc_info[1]) assert "--foo" in str(result.exc_info[1]) assert "--bar" in str(result.exc_info[1]) @@ -184,7 +181,6 @@ def cli(**params): def test_incorrect_grouped_option_cls(): - @click.command() @optgroup() @optgroup.option("--foo", cls=GroupedOption) @@ -472,7 +468,6 @@ def cli(foo): def test_subcommand_first_api(runner): - @click.group() @optgroup("Group 1", help="Group 1 description") @optgroup.option("--foo") @@ -677,7 +672,6 @@ def command2(**params): def test_command_first_api(runner): - @optgroup("Group 1") @optgroup.option("--foo") @optgroup.option("--bar") @@ -697,7 +691,6 @@ def cli(foo, bar): def test_hidden_option(runner): - @click.command() @click.option("--hello") @optgroup("Group 1", help="Group 1 description") @@ -746,7 +739,9 @@ def cli(foo, bar): def cli(foo, bar): click.echo(f"{foo},{bar}") - result = runner.invoke(cli, ) + result = runner.invoke( + cli, + ) assert isinstance(result.exception, TypeError) assert "Need at least one non-hidden" in str(result.exception) @@ -797,7 +792,6 @@ def cli(foo, bar): ], ) def test_help_option(runner, param_decls, options, output): - @click.command() @optgroup("Help Options") @optgroup.help_option(*param_decls) @@ -817,12 +811,10 @@ def cli() -> None: def test_wrapped_functions(runner): - def make_z(): """A unified option interface for making a `z`.""" def decorator(f): - @optgroup.group("Group xyz") @optgroup.option("-x", type=int) @optgroup.option("-y", type=int) @@ -839,7 +831,6 @@ def make_c(): """A unified option interface for making a `c`.""" def decorator(f): - @optgroup.group("Group abc") @optgroup.option("-a", type=int) @optgroup.option("-b", type=int)