Repository: ipython/traitlets (python)

Issue:
Single list element interpreted as string => error
Reported on JupyterLab Gitter by @mlucool

> Hello. What's the correct way to pass --LabApp.extra_labextensions_path=/path/to/dir on the CLI when there is exactly one extra path? Passing that gives "The 'app_dir' trait of a LabApp instance expected a unicode string, not the NoneType None.", but if I do --LabApp.extra_labextensions_path=/path/to/dir --LabApp.extra_labextensions_path=/dev/null it works as expected. More generally, how do you pass a traitlet list of a single value via CLI?

I opened #667 with a unit test that fails.

Pull request description:
Fixes #668 ( passing a single element for a list on CLI)
cc @fcollonval 

Patch:
diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py
index f441550..6bdf741 100644
--- a/traitlets/traitlets.py
+++ b/traitlets/traitlets.py
@@ -2644,6 +2644,12 @@ class List(Container):
 
         return super(List, self).validate_elements(obj, value)
 
+    def set(self, obj, value):
+        if isinstance(value, str):
+            return super().set(obj, [value])
+        else:
+            return super().set(obj, value)
+
 
 class Set(List):
     """An instance of a Python set."""


Test patch:
diff --git a/traitlets/config/tests/test_loader.py b/traitlets/config/tests/test_loader.py
index 74289dc..103eeef 100644
--- a/traitlets/config/tests/test_loader.py
+++ b/traitlets/config/tests/test_loader.py
@@ -327,6 +327,13 @@ class TestKeyValueCL(TestCase):
         c = C(config=config)
         assert c.list_trait == ["x", "y"]
 
+    def test_list_single_item(self):
+        cl = self.klass(log=log)
+        argv = ["--C.list_trait", "x"]
+        config = cl.load_config(argv)
+        c = C(config=config)
+        assert c.list_trait == ["x"]
+
     def test_dict(self):
         cl = self.klass(log=log)
         argv = ["--C.dict_trait", "x=5", "--C.dict_trait", "y=10"]