diff --git a/src/monty/multiprocessing.py b/src/monty/multiprocessing.py index 4b7c720f2..26def1e8f 100644 --- a/src/monty/multiprocessing.py +++ b/src/monty/multiprocessing.py @@ -5,7 +5,10 @@ from __future__ import annotations from multiprocessing import Pool -from typing import Callable, Iterable +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Callable, Iterable try: from tqdm.autonotebook import tqdm diff --git a/src/monty/string.py b/src/monty/string.py index 1a56debca..1cc950afd 100644 --- a/src/monty/string.py +++ b/src/monty/string.py @@ -4,10 +4,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Iterable, cast +from typing import TYPE_CHECKING, cast if TYPE_CHECKING: - from typing import Any, Union + from typing import Any, Iterable, Union def remove_non_ascii(s: str) -> str: diff --git a/tests/test_design_patterns.py b/tests/test_design_patterns.py index 6e3766f4c..9f0068474 100644 --- a/tests/test_design_patterns.py +++ b/tests/test_design_patterns.py @@ -3,12 +3,15 @@ import gc import pickle import weakref -from typing import Any +from typing import TYPE_CHECKING import pytest from monty.design_patterns import cached_class, singleton +if TYPE_CHECKING: + from typing import Any + class TestSingleton: def test_singleton(self): diff --git a/tests/test_dev.py b/tests/test_dev.py index 109e7bc6e..866119e5b 100644 --- a/tests/test_dev.py +++ b/tests/test_dev.py @@ -3,12 +3,16 @@ import unittest import warnings from dataclasses import dataclass -from unittest.mock import patch import pytest from monty.dev import deprecated, install_excepthook, requires +try: + from IPython.core import ultratb +except ImportError: + ultratb = None + # Set all warnings to always be triggered. warnings.simplefilter("always") @@ -200,5 +204,6 @@ def use_import_error(): use_import_error() +@pytest.mark.skipif(ultratb is None, reason="ipython is not installed") def test_install_except_hook(): install_excepthook() diff --git a/tests/test_files/test_settings.yaml b/tests/test_files/settings_for_test.yaml similarity index 100% rename from tests/test_files/test_settings.yaml rename to tests/test_files/settings_for_test.yaml diff --git a/tests/test_files/test_settings.yaml.gz b/tests/test_files/test_settings.yaml.gz deleted file mode 100644 index c3926e151..000000000 Binary files a/tests/test_files/test_settings.yaml.gz and /dev/null differ diff --git a/tests/test_functools.py b/tests/test_functools.py index 120e6bd75..40c3f88ce 100644 --- a/tests/test_functools.py +++ b/tests/test_functools.py @@ -2,7 +2,6 @@ import platform import time -import unittest import pytest @@ -620,7 +619,7 @@ def return_none(self): class TestTimeout: - @unittest.skipIf(platform.system() == "Windows", "Skip on windows") + @pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows") def test_with(self): try: with timeout(1, "timeout!"): diff --git a/tests/test_itertools.py b/tests/test_itertools.py index 470db7c6e..89bcfa29b 100644 --- a/tests/test_itertools.py +++ b/tests/test_itertools.py @@ -1,5 +1,3 @@ -# Copyright (c) Materials Virtual Lab. -# Distributed under the terms of the BSD License. from __future__ import annotations from monty.itertools import chunks, iterator_from_slice diff --git a/tests/test_json.py b/tests/test_json.py index 2403c1649..48caa06bc 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -48,6 +48,12 @@ except ImportError: ObjectId = None +try: + from bson import json_util +except ImportError: + json_util = None + + TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_files") @@ -969,7 +975,7 @@ def test_redirect(self): json.loads(json.dumps(d2), cls=MontyDecoder) def test_redirect_settings_file(self): - data = _load_redirect(os.path.join(TEST_DIR, "test_settings.yaml")) + data = _load_redirect(os.path.join(TEST_DIR, "settings_for_test.yaml")) assert data == { "old_module": { "old_class": {"@class": "new_class", "@module": "new_module"} @@ -1255,9 +1261,8 @@ def test_pint(self): assert isinstance(qty, pint.Quantity) @pytest.mark.skipif(ObjectId is None, reason="bson not present") + @pytest.mark.skipif(json_util is None, reason="pymongo not present") def test_extended_json(self): - from bson import json_util - ext_json_dict = { "datetime": datetime.datetime.now(datetime.timezone.utc), "NaN": float("NaN"), diff --git a/tests/test_multiprocessing.py b/tests/test_multiprocessing.py index e507c321b..dd00cf486 100644 --- a/tests/test_multiprocessing.py +++ b/tests/test_multiprocessing.py @@ -2,9 +2,17 @@ from math import sqrt -from monty.multiprocessing import imap_tqdm +import pytest +try: + from tqdm.autonotebook import tqdm + from monty.multiprocessing import imap_tqdm +except ImportError: + tqdm = None + + +@pytest.mark.skipif(tqdm is None, reason="tqdm not installed") def test_imap_tqdm(): results = imap_tqdm(4, sqrt, range(10_000)) assert len(results) == 10_000 diff --git a/tests/test_os.py b/tests/test_os.py index f0225f70c..fea8648ff 100644 --- a/tests/test_os.py +++ b/tests/test_os.py @@ -1,13 +1,16 @@ from __future__ import annotations import os -from pathlib import Path +from typing import TYPE_CHECKING import pytest from monty.os import cd, makedirs_p from monty.os.path import find_exts, zpath +if TYPE_CHECKING: + from pathlib import Path + MODULE_DIR = os.path.dirname(__file__) TEST_DIR = os.path.join(MODULE_DIR, "test_files") diff --git a/tests/test_serialization.py b/tests/test_serialization.py index abe14e2a3..0a2d99969 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -3,7 +3,6 @@ import glob import json import os -import unittest import pytest @@ -66,7 +65,7 @@ def test_dumpfn_loadfn(self): with pytest.raises(TypeError): loadfn("monte_test.txt", fmt="garbage") - @unittest.skipIf(msgpack is None, "msgpack-python not installed.") + @pytest.mark.skipif(msgpack is None, reason="msgpack-python not installed.") def test_mpk(self): d = {"hello": "world"} diff --git a/tests/test_shutil.py b/tests/test_shutil.py index dc18c3588..4adc4804e 100644 --- a/tests/test_shutil.py +++ b/tests/test_shutil.py @@ -4,7 +4,6 @@ import platform import shutil import tempfile -import unittest from gzip import GzipFile from pathlib import Path @@ -189,7 +188,7 @@ def teardown_method(self): class TestRemove: - @unittest.skipIf(platform.system() == "Windows", "Skip on windows") + @pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows") def test_remove_file(self): tempdir = tempfile.mkdtemp(dir=TEST_DIR) tempf = tempfile.mkstemp(dir=tempdir)[1] @@ -197,13 +196,13 @@ def test_remove_file(self): assert not os.path.isfile(tempf) shutil.rmtree(tempdir) - @unittest.skipIf(platform.system() == "Windows", "Skip on windows") + @pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows") def test_remove_folder(self): tempdir = tempfile.mkdtemp(dir=TEST_DIR) remove(tempdir) assert not os.path.isdir(tempdir) - @unittest.skipIf(platform.system() == "Windows", "Skip on windows") + @pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows") def test_remove_symlink(self): tempdir = tempfile.mkdtemp(dir=TEST_DIR) tempf = tempfile.mkstemp(dir=tempdir)[1] @@ -216,7 +215,7 @@ def test_remove_symlink(self): assert not os.path.islink(templink) remove(tempdir) - @unittest.skipIf(platform.system() == "Windows", "Skip on windows") + @pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows") def test_remove_symlink_follow(self): tempdir = tempfile.mkdtemp(dir=TEST_DIR) tempf = tempfile.mkstemp(dir=tempdir)[1] diff --git a/tests/test_string.py b/tests/test_string.py index a015e5a3a..0167d7f20 100644 --- a/tests/test_string.py +++ b/tests/test_string.py @@ -1,7 +1,3 @@ -""" -TODO: Modify unittest doc. -""" - from __future__ import annotations import random @@ -10,7 +6,7 @@ def test_remove_non_ascii(): - s = "".join(chr(random.randint(0, 127)) for i in range(10)) - s += "".join(chr(random.randint(128, 150)) for i in range(10)) + s = "".join(chr(random.randint(0, 127)) for _ in range(10)) + s += "".join(chr(random.randint(128, 150)) for _ in range(10)) clean = remove_non_ascii(s) assert len(clean) == 10 diff --git a/tests/test_termcolor.py b/tests/test_termcolor.py index 6a54ed4ac..d0da543cc 100644 --- a/tests/test_termcolor.py +++ b/tests/test_termcolor.py @@ -1,7 +1,3 @@ -""" -TODO: Modify unittest doc. -""" - from __future__ import annotations import os