Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tighten mypy: globus_cli.parsing #922

Merged
merged 12 commits into from
Dec 20, 2023
28 changes: 7 additions & 21 deletions src/globus_cli/parsing/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
def invoke(self, ctx: click.Context) -> t.Any:
log.debug("command invoke start")
try:
# this isn't forcing interactive mode, it's just checking to see that
# the GLOBUS_CLI_INTERACTIVE valude is *valid*
sirosen marked this conversation as resolved.
Show resolved Hide resolved
env_interactive(raising=True)
return super().invoke(ctx)
finally:
log.debug("command invoke exit")
Expand Down Expand Up @@ -128,12 +131,6 @@ def parse_args(self, ctx: click.Context, args: list[str]) -> list[str]:
raise


class GlobusCommandEnvChecks(GlobusCommand):
def invoke(self, ctx: click.Context) -> t.Any:
env_interactive(raising=True)
return super().invoke(ctx)


class GlobusCommandGroup(click.Group):
"""
This is a click.Group with any customizations which we deem necessary
Expand Down Expand Up @@ -229,32 +226,21 @@ def decorator(f: C) -> TopLevelGroup:
return decorator


def command(
*args: t.Any, **kwargs: t.Any
) -> t.Callable[[C], GlobusCommand | GlobusCommandEnvChecks]:
def command(*args: t.Any, **kwargs: t.Any) -> t.Callable[[C], GlobusCommand]:
"""
A helper for decorating commands a-la `click.command`, but pulling the help string
from `<function>.__doc__` by default.

Also allows the use of custom arguments, which are stored on the command, as in
"adoc_examples".

`skip_env_checks` is used to disable environment variable validation prior to
running a command, but is ignored when a specific `cls` argument is passed.
"""
disable_opts = kwargs.pop("disable_options", [])

def _inner_decorator(func: C) -> GlobusCommand | GlobusCommandEnvChecks:
if "cls" not in kwargs:
if kwargs.get("skip_env_checks", False) is True:
kwargs["cls"] = GlobusCommand
else:
kwargs["cls"] = GlobusCommandEnvChecks

def _inner_decorator(func: C) -> GlobusCommand:
kwargs["globus_disable_opts"] = disable_opts

return common_options(disable_options=disable_opts)( # type: ignore[no-any-return] # noqa: E501
click.command(*args, **kwargs)(func)
return common_options(disable_options=disable_opts)(
click.command(*args, cls=GlobusCommand, **kwargs)(func)
)

return _inner_decorator
Expand Down