Repository: tinosulzer/PyBaMM

Issue:
Domains as strings
**Describe the bug**
The `Domain` class checks that `domain` is an iterable and raises a `TypeError` if it isn't.
However, a `str` domain is not caught by this as `iter("a string") == True`.
This isn't caught by the test in `TestIndependentVariable`; test should be
```python
with self.assertRaises(TypeError):
   pybamm.IndependentVariable("a", domain="test")
```

**Suggested fix**
If `domain` is a string, replace with `list(domain)`.



Discussion:

Patch:
diff --git a/pybamm/expression_tree/domain.py b/pybamm/expression_tree/domain.py
index 5ca645b2..75be730f 100644
--- a/pybamm/expression_tree/domain.py
+++ b/pybamm/expression_tree/domain.py
@@ -13,17 +13,19 @@ class Domain(object):
 
     name: str
         the name of the node
-    domain : iterable of str
+    domain : iterable of str, or str
         the list of domains
 
     """
 
     def __init__(self, name, domain=[]):
         super().__init__(name)
+        if isinstance(domain, str):
+            domain = [domain]
         try:
             iter(domain)
         except TypeError:
-            raise TypeError('Domain: argument domain is not iterable')
+            raise TypeError("Domain: argument domain is not iterable")
         else:
             self.domain = domain
 


Test patch:
diff --git a/tests/test_expression_tree/test_independent_variable.py b/tests/test_expression_tree/test_independent_variable.py
index 7ce7b560..22db0db9 100644
--- a/tests/test_expression_tree/test_independent_variable.py
+++ b/tests/test_expression_tree/test_independent_variable.py
@@ -13,9 +13,12 @@ class TestIndependentVariable(unittest.TestCase):
         a = pybamm.IndependentVariable("a")
         self.assertEqual(a.name, "a")
         self.assertEqual(a.domain, [])
-        a = pybamm.IndependentVariable("a", domain=['test'])
-        self.assertEqual(a.domain[0], 'test')
-        self.assertRaises(TypeError, pybamm.IndependentVariable("a", domain='test'))
+        a = pybamm.IndependentVariable("a", domain=["test"])
+        self.assertEqual(a.domain[0], "test")
+        a = pybamm.IndependentVariable("a", domain="test")
+        self.assertEqual(a.domain[0], "test")
+        with self.assertRaises(TypeError):
+            pybamm.IndependentVariable("a", domain=1)
 
 
 if __name__ == "__main__":