-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(cli): make errors and help friendlier for new users #1171
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
Merged
stevemessick
merged 2 commits into
Kaggle:main
from
latiefdole:feat/friendlier-cli-errors
Aug 11, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| # coding=utf-8 | ||
| import argparse | ||
| import io | ||
| import sys | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| from requests.exceptions import HTTPError | ||
| from requests.models import Response | ||
|
|
||
| import kaggle.cli as cli | ||
| from kaggle.api.kaggle_api_extended import format_http_error | ||
|
|
||
|
|
||
| def _http_error(status_code): | ||
| response = Response() | ||
| response.status_code = status_code | ||
| response.url = "https://www.kaggle.com/api/v1/competitions/nope" | ||
| return HTTPError(f"{status_code} Client Error for url: {response.url}", response=response) | ||
|
|
||
|
|
||
| def _run_main(argv, func): | ||
| """Runs cli.main() with a stubbed-out command implementation.""" | ||
| parse_args = argparse.ArgumentParser.parse_args | ||
|
|
||
| def parse_args_with_stub(self, *args, **kwargs): | ||
| namespace = parse_args(self, *args, **kwargs) | ||
| namespace.func = func | ||
| return namespace | ||
|
|
||
| stdout, stderr = io.StringIO(), io.StringIO() | ||
| with patch.object(sys, "argv", argv): | ||
| with patch("kaggle.cli.api") as mock_api: | ||
| mock_api._authenticated = True | ||
| with patch.object(argparse.ArgumentParser, "parse_args", parse_args_with_stub): | ||
| with patch("sys.stdout", stdout), patch("sys.stderr", stderr): | ||
| try: | ||
| cli.main() | ||
| exit_code = 0 | ||
| except SystemExit as e: | ||
| exit_code = e.code | ||
| return exit_code, stdout.getvalue(), stderr.getvalue() | ||
|
|
||
|
|
||
| class TestFormatHttpError(unittest.TestCase): | ||
| def test_keeps_the_original_message(self): | ||
| self.assertIn("404 Client Error", format_http_error(_http_error(404))) | ||
|
|
||
| def test_404_suggests_checking_the_reference(self): | ||
| self.assertIn("kaggle search", format_http_error(_http_error(404))) | ||
|
|
||
| def test_403_mentions_competition_rules(self): | ||
| self.assertIn("rules", format_http_error(_http_error(403))) | ||
|
|
||
| def test_429_explains_rate_limiting(self): | ||
| self.assertIn("rate limited", format_http_error(_http_error(429))) | ||
|
|
||
| def test_5xx_is_attributed_to_kaggle(self): | ||
| self.assertIn("Kaggle's side", format_http_error(_http_error(503))) | ||
|
|
||
| def test_status_without_a_hint_is_left_alone(self): | ||
| self.assertEqual( | ||
| "400 Client Error for url: " + _http_error(400).response.url, format_http_error(_http_error(400)) | ||
| ) | ||
|
|
||
| def test_error_without_a_response_is_left_alone(self): | ||
| self.assertEqual("boom", format_http_error(HTTPError("boom"))) | ||
|
|
||
|
|
||
| class TestMainErrorHandling(unittest.TestCase): | ||
| def test_http_error_prints_hint_and_exits_nonzero(self): | ||
| def raise_404(**kwargs): | ||
| raise _http_error(404) | ||
|
|
||
| exit_code, _, stderr = _run_main(["kaggle", "quota"], raise_404) | ||
|
|
||
| self.assertEqual(1, exit_code) | ||
| self.assertIn("404 Client Error", stderr) | ||
| self.assertIn("kaggle search", stderr) | ||
|
|
||
| def test_401_still_prints_auth_help(self): | ||
| def raise_401(**kwargs): | ||
| raise _http_error(401) | ||
|
|
||
| exit_code, stdout, _ = _run_main(["kaggle", "quota"], raise_401) | ||
|
|
||
| self.assertEqual(1, exit_code) | ||
| self.assertIn("Authentication required", stdout) | ||
|
|
||
| def test_unexpected_error_reports_a_bug_instead_of_a_traceback(self): | ||
| def raise_bug(**kwargs): | ||
| raise KeyError("some_missing_field") | ||
|
|
||
| exit_code, _, stderr = _run_main(["kaggle", "quota"], raise_bug) | ||
|
|
||
| self.assertEqual(1, exit_code) | ||
| self.assertIn("KeyError", stderr) | ||
| self.assertIn("--debug", stderr) | ||
| self.assertIn("github.com/Kaggle/kaggle-cli/issues", stderr) | ||
| self.assertNotIn("Traceback", stderr) | ||
|
|
||
| def test_debug_flag_lets_the_traceback_through(self): | ||
| def raise_bug(**kwargs): | ||
| raise KeyError("some_missing_field") | ||
|
|
||
| with self.assertRaises(KeyError): | ||
| _run_main(["kaggle", "--debug", "quota"], raise_bug) | ||
|
|
||
| def test_debug_flag_is_not_passed_to_the_command(self): | ||
| received = {} | ||
|
|
||
| def record(**kwargs): | ||
| received.update(kwargs) | ||
| return None | ||
|
|
||
| _run_main(["kaggle", "--debug", "quota"], record) | ||
|
|
||
| self.assertNotIn("debug", received) | ||
|
|
||
| def test_value_error_is_still_reported_without_a_bug_notice(self): | ||
| def raise_value_error(**kwargs): | ||
| raise ValueError("bad input") | ||
|
|
||
| exit_code, _, stderr = _run_main(["kaggle", "quota"], raise_value_error) | ||
|
|
||
| self.assertEqual(1, exit_code) | ||
| self.assertIn("bad input", stderr) | ||
| self.assertNotIn("If you think this is a bug", stderr) | ||
|
|
||
|
|
||
| class TestHelpExamples(unittest.TestCase): | ||
| def test_examples_cover_the_common_first_commands(self): | ||
| examples = cli.Help.examples | ||
|
|
||
| self.assertIn("kaggle auth login", examples) | ||
| self.assertIn("kaggle competitions download -c titanic", examples) | ||
| self.assertIn("kaggle competitions submit", examples) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want better log/debug support. See line 967 of
kaggle_api_extended.py.