diff --git a/monty/itertools.py b/monty/itertools.py index 5d17c2fcb..6980db7f7 100644 --- a/monty/itertools.py +++ b/monty/itertools.py @@ -1,6 +1,7 @@ """ Additional tools for iteration. """ + import itertools try: diff --git a/monty/json.py b/monty/json.py index ea1f6e3e9..f8cae7506 100644 --- a/monty/json.py +++ b/monty/json.py @@ -713,8 +713,11 @@ def jsanitize( except TypeError: pass - if recursive_msonable and isinstance(obj, MSONable): - return obj.as_dict() + if recursive_msonable: + try: + return obj.as_dict() + except AttributeError: + pass if not strict: return str(obj) diff --git a/monty/operator.py b/monty/operator.py index d153208c3..45dfeb6f1 100644 --- a/monty/operator.py +++ b/monty/operator.py @@ -1,6 +1,7 @@ """ Useful additional functions for operators """ + import operator diff --git a/monty/os/path.py b/monty/os/path.py index 65e628ca6..8ffec3606 100644 --- a/monty/os/path.py +++ b/monty/os/path.py @@ -1,6 +1,7 @@ """ Path based methods, e.g., which, zpath, etc. """ + import os from monty.fnmatch import WildCard diff --git a/monty/serialization.py b/monty/serialization.py index 5acbc43c8..d2025c03e 100644 --- a/monty/serialization.py +++ b/monty/serialization.py @@ -2,6 +2,7 @@ This module implements serialization support for common formats such as json and yaml. """ + import json import os diff --git a/monty/shutil.py b/monty/shutil.py index c008a6013..1e19db3a4 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -1,4 +1,5 @@ """Copying and zipping utilities. Works on directories mostly.""" + from __future__ import annotations import os diff --git a/monty/subprocess.py b/monty/subprocess.py index 869dfec1c..98cb8edf0 100644 --- a/monty/subprocess.py +++ b/monty/subprocess.py @@ -1,6 +1,7 @@ """ Calling shell processes. """ + import shlex import threading import traceback diff --git a/monty/termcolor.py b/monty/termcolor.py index 55a8541d0..c3b0398b0 100644 --- a/monty/termcolor.py +++ b/monty/termcolor.py @@ -20,6 +20,7 @@ ANSII Color formatting for output in terminal. """ + import os try: diff --git a/tasks.py b/tasks.py index 5c2e2a94f..26078c092 100755 --- a/tasks.py +++ b/tasks.py @@ -4,7 +4,6 @@ Deployment file to facilitate releases of monty. """ - import datetime import glob import json diff --git a/tests/test_json.py b/tests/test_json.py index a6571a17b..4e19911e5 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -600,7 +600,7 @@ def test_jsanitize(self): assert clean["world"] is None assert json.loads(json.dumps(d)) == json.loads(json.dumps(clean)) - d = {"hello": GoodMSONClass(1, 2, 3)} + d = {"hello": GoodMSONClass(1, 2, 3), "test": "hi"} with pytest.raises(TypeError): json.dumps(d) clean = jsanitize(d) @@ -608,9 +608,11 @@ def test_jsanitize(self): clean_strict = jsanitize(d, strict=True) assert clean_strict["hello"]["a"] == 1 assert clean_strict["hello"]["b"] == 2 + assert clean_strict["test"] == "hi" clean_recursive_msonable = jsanitize(d, recursive_msonable=True) assert clean_recursive_msonable["hello"]["a"] == 1 assert clean_recursive_msonable["hello"]["b"] == 2 + assert clean_recursive_msonable["test"] == "hi" d = {"dt": datetime.datetime.now()} clean = jsanitize(d)