33f5a3354dc7 — David Keijser 4 years ago
Return None when trying to get missing sub-directive

This aligns with the behaviour of get on the top-level directive, previously it
would instead raise an exception.
2 files changed, 10 insertions(+), 2 deletions(-)

M scfg/__init__.py
M tests.py
M scfg/__init__.py +1 -2
@@ 23,8 23,7 @@ class Directive:
     def __init__(self, name, params, children=None):
         self.name = name
         self.params = params
-        if children is not None:
-            self.children = children
+        self.children = children or []
 
     def __str__(self):
         return f"{self.name}: {self.params}"

          
M tests.py +9 -0
@@ 26,6 26,15 @@ class SCFGTest(unittest.TestCase):
             train.get_all("model")[1].get("weight").params[0], "540t"
         )
 
+    def test_access_missing(self):
+        train = self.config.get("train")
+        self.assertEqual(
+            train.get("missing"), None
+        )
+        self.assertEqual(
+            train.get_all("model")[0].get("lines-served").get("missing"), None
+        )
+
 
 if __name__ == "__main__":
     unittest.main()