Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

def test_no_arg():
result = runner.invoke(app)
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands" in result.output
assert "create" in result.output

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be good to add a assert not result.stderr to ensure the doubled output isn't displayed

Expand Down
12 changes: 12 additions & 0 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,19 @@ def _main(
raise click.Abort() from e
except KeyboardInterrupt as e:
raise click.exceptions.Exit(130) from e
# TODO: When deprecating Click < 8.2 uncomment the next two lines
# except click.exceptions.NoArgsIsHelpError as e:
# raise click.exceptions.Exit(0) from e
except click.ClickException as e:
# TODO: When deprecating Click < 8.2 remove this section [start]
_no_args_is_help_error = getattr(
Copy link

@diablodale diablodale Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than probing attrs and instance, I suggest using the exception we want to catch.
Click's init.py doesn't pull in the NoArgsIsHelpError exception into itself. So instead, we can easily do the same at the top of this file with;

from click.exceptions import NoArgsIsHelpError

and then here simply the code to be

...
except KeyboardInterrupt as e:
    raise click.exceptions.Exit(130) from e
except NoArgsIsHelpError as e:
    raise click.exceptions.Exit(0) from e
except click.ClickException as e:
...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To continue supporting older versions of click (which don't define the exception), the exception clause can be made conditional with code like:

CatchClickNoArgsIsHelpError: Exception|tuple[()]
try:
    # click >= 8.2.0
    CatchClickNoArgsIsHelpError = click.exceptions.NoArgsIsHelpError
except AttributeError:
    # click < 8.2.0
    CatchClickNoArgsIsHelpError = ()

And then:

    except CatchClickNoArgsIsHelpError as e:
        raise click.exceptions.Exit(0) from e

Empty tuples are permitted in except clauses, and mean that the clause will never actually trigger at runtime.

click.exceptions, "NoArgsIsHelpError", None
)
if _no_args_is_help_error is not None and isinstance(
e, _no_args_is_help_error
):
raise click.exceptions.Exit(0) from e
# TODO: When deprecating Click < 8.2 remove this section [end]
if not standalone_mode:
raise
# Typer override
Expand Down
Loading