Skip to content
Open
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
6 changes: 3 additions & 3 deletions kt-kernel/python/cli/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ def model_path_remove(

def _parse_value(value: str):
"""Parse a string value into appropriate Python type."""
# Try boolean
if value.lower() in ("true", "yes", "on", "1"):
# Try boolean ("1"/"0" fall through to int so e.g. CUDA_VISIBLE_DEVICES=1 stays "1")
if value.lower() in ("true", "yes", "on"):
return True
if value.lower() in ("false", "no", "off", "0"):
if value.lower() in ("false", "no", "off"):
return False

# Try integer
Expand Down
44 changes: 44 additions & 0 deletions kt-kernel/test/per_commit/test_config_parse_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import tempfile
import unittest
from pathlib import Path

import yaml

from ci.ci_register import register_cpu_ci
from kt_kernel.cli.commands.config import _parse_value
from kt_kernel.cli.config.settings import Settings


register_cpu_ci(est_time=0.1, suite="default")


class TestConfigParseValue(unittest.TestCase):
def test_numeric_strings_stay_integers(self):
for raw, expected in (("0", 0), ("1", 1), ("2", 2), ("30000", 30000)):
value = _parse_value(raw)
self.assertIs(type(value), int, raw)
self.assertEqual(value, expected)

def test_boolean_words_still_parse_as_booleans(self):
for raw in ("true", "True", "yes", "on"):
self.assertIs(_parse_value(raw), True, raw)
for raw in ("false", "False", "no", "off"):
self.assertIs(_parse_value(raw), False, raw)

def test_env_var_set_to_one_is_exported_as_one(self):
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
config_path = tmp / "config.yaml"
# Keep every directory Settings creates inside the temp dir.
config_path.write_text(
yaml.safe_dump({"paths": {"models": str(tmp / "models"), "cache": str(tmp / "cache")}}),
encoding="utf-8",
)

Settings(config_path=config_path).set("advanced.env.CUDA_VISIBLE_DEVICES", _parse_value("1"))

self.assertEqual(Settings(config_path=config_path).get_env_vars(), {"CUDA_VISIBLE_DEVICES": "1"})


if __name__ == "__main__":
unittest.main()