Skip to content

Commit

Permalink
Update ruff and its config
Browse files Browse the repository at this point in the history
  • Loading branch information
ulgens committed Jul 28, 2024
1 parent f314580 commit d200cd0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
5 changes: 4 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
14 changes: 10 additions & 4 deletions src/click_option_group/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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)

Expand Down
9 changes: 4 additions & 5 deletions src/click_option_group/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 7 additions & 16 deletions tests/test_click_option_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@


def test_basic_functionality_first_api(runner):

@click.command()
@click.option("--hello")
@optgroup("Group 1", help="Group 1 description")
Expand Down Expand Up @@ -45,7 +44,6 @@ def cli(hello, foo1, bar1, lol, foo2, bar2, goodbye):


def test_noname_group(runner):

@click.command()
@optgroup()
@optgroup.option("--foo")
Expand Down Expand Up @@ -99,7 +97,6 @@ def cli3(**params):


def test_missing_group_decl_first_api(runner):

@click.command()
@click.option("--hello1")
@optgroup.option("--foo")
Expand All @@ -111,31 +108,31 @@ def cli(**params):
result = runner.invoke(cli, ["--help"])

assert result.exception
assert TypeError == result.exc_info[0]
assert isinstance(result.exc_info[1], 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])

result = runner.invoke(cli, [])

assert result.exception
assert TypeError == result.exc_info[0]
assert isinstance(result.exc_info[1], 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])

result = runner.invoke(cli, ["--hello1", "hello1"])

assert result.exception
assert TypeError == result.exc_info[0]
assert isinstance(result.exc_info[1], 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])

result = runner.invoke(cli, ["--foo", "foo"])

assert result.exception
assert TypeError == result.exc_info[0]
assert isinstance(result.exc_info[1], 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])
Expand Down Expand Up @@ -184,7 +181,6 @@ def cli(**params):


def test_incorrect_grouped_option_cls():

@click.command()
@optgroup()
@optgroup.option("--foo", cls=GroupedOption)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -677,7 +672,6 @@ def command2(**params):


def test_command_first_api(runner):

@optgroup("Group 1")
@optgroup.option("--foo")
@optgroup.option("--bar")
Expand All @@ -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")
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d200cd0

Please sign in to comment.