diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index 8bad810d..e57c4af2 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -263,7 +263,8 @@ def __getstate__(self) -> Dict[str, Any]: state = self.__dict__.copy() # don't pickle client - del state["_client"] + if "_client" in state: + del state["_client"] return state diff --git a/tests/test_cloudpath_serialize.py b/tests/test_cloudpath_serialize.py new file mode 100644 index 00000000..ae8857af --- /dev/null +++ b/tests/test_cloudpath_serialize.py @@ -0,0 +1,14 @@ +import pickle + +from cloudpathlib import CloudPath + + +def test_pickle_roundtrip(): + path1 = CloudPath("s3://bucket/key") + pkl1 = pickle.dumps(path1) + + path2 = pickle.loads(pkl1) + pkl2 = pickle.dumps(path2) + + assert path1 == path2 + assert pkl1 == pkl2