Skip to content

Commit 210a8e7

Browse files
authored
fix(cli): stop treating subcommand -v as version flag (#1082)
## Summary Fix `_is_help_or_version_command` so subcommand `-v` (the CSV output alias on commands like `quota`) no longer skips authentication. Top-level `kaggle -v` / `kaggle --version` still skip auth as before. The duration parsing crash from #1064 is handled separately in Kaggle/kaggle-sdk-python#58 (needs kapigen sync and a new kagglesdk release). ## Root cause `authenticate()` runs before argparse and joins `sys.argv[1:]` into a string. `"quota -v".endswith("-v")` matched the global version check even though `-v` here is the CSV flag, causing unauthenticated API calls and 401s. ## Tests Added `tests/unit/test_help_version_auth.py` covering top-level version/help, subcommand CSV `-v`, and subcommand help. ## Test plan - [x] `pytest tests/unit/test_help_version_auth.py` - [ ] Maintainer: please run `/gcbrun` for Cloud Build (fork PR) Related to #1064
1 parent bb1228c commit 210a8e7

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/kaggle/api/kaggle_api_extended.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,15 @@ def _is_help_or_version_command(self, api_command: str) -> bool:
11671167
Returns:
11681168
bool: True if valid
11691169
"""
1170-
return api_command.endswith(("-h", "--help", "-v", "--version"))
1170+
argv = sys.argv[1:]
1171+
if not argv:
1172+
return False
1173+
# Top-level only: kaggle -v, kaggle --version, kaggle -h, kaggle --help
1174+
if len(argv) == 1 and argv[0] in ("-h", "--help", "-v", "--version"):
1175+
return True
1176+
# Subcommand help only. Do not treat trailing -v as version: many commands use
1177+
# -v as the --csv output-format alias (e.g. kaggle quota -v).
1178+
return api_command.endswith(("-h", "--help"))
11711179

11721180
def read_config_environment(self, config_data: Optional[Dict[str, str]] = None) -> Dict[str, str]:
11731181
"""Reads config values from environment variables.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# coding=utf-8
2+
import sys
3+
import unittest
4+
from unittest.mock import patch
5+
6+
sys.path.insert(0, "../../src")
7+
8+
from kaggle.api.kaggle_api_extended import KaggleApi
9+
10+
11+
class TestHelpOrVersionAuth(unittest.TestCase):
12+
"""Tests for help/version detection used during authenticate()."""
13+
14+
def setUp(self):
15+
self.api = KaggleApi.__new__(KaggleApi)
16+
17+
def _assert_help_or_version(self, argv, expected):
18+
with patch.object(sys, "argv", argv):
19+
api_command = " ".join(argv[1:])
20+
self.assertEqual(self.api._is_help_or_version_command(api_command), expected)
21+
22+
def test_top_level_version_and_help(self):
23+
self._assert_help_or_version(["kaggle", "-v"], True)
24+
self._assert_help_or_version(["kaggle", "--version"], True)
25+
self._assert_help_or_version(["kaggle", "-h"], True)
26+
self._assert_help_or_version(["kaggle", "--help"], True)
27+
28+
def test_subcommand_csv_flag_is_not_version(self):
29+
self._assert_help_or_version(["kaggle", "quota", "-v"], False)
30+
self._assert_help_or_version(["kaggle", "datasets", "list", "-v"], False)
31+
32+
def test_subcommand_help_still_skips_auth(self):
33+
self._assert_help_or_version(["kaggle", "quota", "-h"], True)
34+
self._assert_help_or_version(["kaggle", "datasets", "list", "--help"], True)
35+
36+
def test_quota_csv_flag_does_not_allow_logged_out(self):
37+
with patch.object(sys, "argv", ["kaggle", "quota", "-v"]):
38+
self.assertFalse(self.api._command_allows_logged_out("quota -v"))
39+
40+
41+
if __name__ == "__main__":
42+
unittest.main()

0 commit comments

Comments
 (0)