Skip to content

Commit

Permalink
More error message tweaks (#67)
Browse files Browse the repository at this point in the history
* More error message tweaks

* Comments, subcommand verbosity

* mypy + black fixes

* Nits, version bump
  • Loading branch information
brentyi authored Sep 6, 2023
1 parent 5498eec commit 4d5f058
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 145 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "tyro"
authors = [
{name = "brentyi", email = "[email protected]"},
]
version = "0.5.6"
version = "0.5.7"
description = "Strongly typed, zero-effort CLI interfaces"
readme = "README.md"
license = { text="MIT" }
Expand Down
26 changes: 14 additions & 12 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ class Class:
assert "Similar arguments" in error

# --reward.track should appear in both the usage string and as a similar argument.
assert error.count("--reward.track") == 2
assert error.count("--help") == 0
assert error.count("--reward.track") == 1
assert error.count("--help") == 1


def test_similar_arguments_subcommands() -> None:
Expand All @@ -195,7 +195,7 @@ class ClassB:
assert "Unrecognized argument" in error
assert "Similar arguments:" in error
assert error.count("--reward.track") == 1
assert error.count("--help") == 2
assert error.count("--help") == 3


def test_similar_arguments_subcommands_multiple() -> None:
Expand All @@ -221,7 +221,7 @@ class ClassB:
assert "Arguments similar to --reward.trac" in error
assert error.count("--reward.track {True,False}") == 1
assert error.count("--reward.trace INT") == 1
assert error.count("--help") == 4
assert error.count("--help") == 5


def test_similar_arguments_subcommands_multiple_contains_match() -> None:
Expand All @@ -247,7 +247,7 @@ class ClassB:
assert "Similar arguments" in error
assert error.count("--reward.track {True,False}") == 1
assert error.count("--reward.trace INT") == 1
assert error.count("--help") == 4 # 2 subcommands * 2 arguments.
assert error.count("--help") == 5 # 2 subcommands * 2 arguments + usage hint.


def test_similar_arguments_subcommands_multiple_contains_match_alt() -> None:
Expand All @@ -272,7 +272,9 @@ class ClassB:
assert "Unrecognized argument" in error
assert "Similar arguments" in error
assert error.count("--reward.track {True,False}") == 1
assert error.count("--help") == 2 # Should show two possible subcommands.
assert (
error.count("--help") == 3
) # Should show two possible subcommands + usage hint.


def test_similar_arguments_subcommands_overflow_different() -> None:
Expand Down Expand Up @@ -313,7 +315,7 @@ class ClassB:
assert "Similar arguments" in error
assert error.count("--reward.track") == 10
assert "[...]" not in error
assert error.count("--help") == 20
assert error.count("--help") == 21

target = io.StringIO()
with pytest.raises(SystemExit), contextlib.redirect_stdout(target):
Expand All @@ -323,7 +325,7 @@ class ClassB:

# Usage print should be clipped.
error = target.getvalue()
assert "help:" in error
assert "For full helptext, run" in error


def test_similar_arguments_subcommands_overflow_same() -> None:
Expand Down Expand Up @@ -381,7 +383,7 @@ class ClassI:
assert "Similar arguments" in error
assert error.count("--reward.track") == 1
assert "[...]" in error
assert error.count("--help") == 4
assert error.count("--help") == 5


def test_similar_arguments_subcommands_overflow_same_startswith_multiple() -> None:
Expand Down Expand Up @@ -441,7 +443,7 @@ class ClassI:
assert error.count("--rewar") == 1
assert "rewarde" not in error
assert "[...]" in error
assert error.count("--help") == 4
assert error.count("--help") == 5


def test_similar_flag() -> None:
Expand All @@ -458,8 +460,8 @@ class Args:

error = target.getvalue()

# Printed in the usage message.
assert error.count("--flag | --no-flag") == 1
# We don't print usage text anymore.
assert error.count("--flag | --no-flag") == 0

# Printed in the similar argument list.
assert error.count("--flag, --no-flag") == 1
Loading

0 comments on commit 4d5f058

Please sign in to comment.