Repository: janehmueller/python-json-config

Issue:
Enable pickling of Config
Pickle tries to call `__getstate__` which fails since it is not handled in the custom `__getattr__` method.

Use [this solution](https://stackoverflow.com/a/50888571).

Discussion:

Patch:
diff --git a/python_json_config/__version__.py b/python_json_config/__version__.py
index 1f356cc..cd7ca49 100644
--- a/python_json_config/__version__.py
+++ b/python_json_config/__version__.py
@@ -1,1 +1,1 @@
-__version__ = '1.0.0'
+__version__ = '1.0.1'
diff --git a/python_json_config/config_node.py b/python_json_config/config_node.py
index 6b63473..0b23352 100644
--- a/python_json_config/config_node.py
+++ b/python_json_config/config_node.py
@@ -47,6 +47,15 @@ class ConfigNode(object):
 
     __repr__ = __str__
 
+    """
+    These two methods are needed to enable pickling since this class overwrites __getattr__.
+    """
+    def __getstate__(self):
+        return vars(self)
+
+    def __setstate__(self, state):
+        vars(self).update(state)
+
 
 class Config(ConfigNode):
     def __init__(self, config_dict: dict):


Test patch:
diff --git a/tests/config_node_test.py b/tests/config_node_test.py
index 422901e..86521fc 100644
--- a/tests/config_node_test.py
+++ b/tests/config_node_test.py
@@ -1,3 +1,4 @@
+import pickle
 from unittest import TestCase
 
 from python_json_config.config_node import ConfigNode
@@ -61,3 +62,10 @@ class ConfigNodeTest(TestCase):
         node = ConfigNode({1: 2, 3: 4})
         node_str = 'ConfigNode(path=[], values={1: 2, 3: 4})'
         self.assertEqual(str(node), node_str)
+
+    def test_pickle(self):
+        config = ConfigNode(self.config_dict)
+        pickle_conf = pickle.loads(pickle.dumps(config))
+        self.assertEqual(pickle_conf.key1, 1)
+        self.assertEqual(pickle_conf.key2.key3, 3)
+        self.assertEqual(pickle_conf.key2.key4.key5, 5)