From 37852d562de85ed63e6858a8bec1016e9b4b63b1 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:49:15 +0000 Subject: [PATCH 01/38] Bump rexml from 3.3.2 to 3.3.6 in /docs Bumps [rexml](https://github.com/ruby/rexml) from 3.3.2 to 3.3.6. - [Release notes](https://github.com/ruby/rexml/releases) - [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md) - [Commits](https://github.com/ruby/rexml/compare/v3.3.2...v3.3.6) --- updated-dependencies: - dependency-name: rexml dependency-type: indirect ... Signed-off-by: dependabot[bot] --- docs/Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Gemfile.lock b/docs/Gemfile.lock index 9ecbb76b..cb2f0c29 100644 --- a/docs/Gemfile.lock +++ b/docs/Gemfile.lock @@ -224,7 +224,7 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) - rexml (3.3.2) + rexml (3.3.6) strscan rouge (3.26.0) ruby2_keywords (0.0.5) From a7752ee0e7429541e53d24d820ecfd048ae67809 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 10:42:32 +0800 Subject: [PATCH 02/38] avoid import torch for type check in MontyEncoder --- src/monty/json.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index f9899097..e05aab0b 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -586,8 +586,8 @@ def default(self, o) -> dict: if isinstance(o, Path): return {"@module": "pathlib", "@class": "Path", "string": str(o)} - if torch is not None and isinstance(o, torch.Tensor): - # Support for Pytorch Tensors. + # Support for Pytorch Tensors + if _check_type(o, "torch.Tensor"): d: dict[str, Any] = { "@module": "torch", "@class": "Tensor", From 196dcf9eabcc1c870c5c93b82a5eab0d039b35b7 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 11:02:11 +0800 Subject: [PATCH 03/38] lazily import torch for MontyDecoder --- src/monty/json.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index e05aab0b..25ae42a5 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -52,11 +52,6 @@ orjson = None -try: - import torch -except ImportError: - torch = None - __version__ = "3.0.0" @@ -803,15 +798,21 @@ def process_decoded(self, d): d = {k: self.process_decoded(v) for k, v in data.items()} return cls_(**d) - elif torch is not None and modname == "torch" and classname == "Tensor": - if "Complex" in d["dtype"]: - return torch.tensor( # pylint: disable=E1101 - [ - np.array(r) + np.array(i) * 1j - for r, i in zip(*d["data"]) - ], - ).type(d["dtype"]) - return torch.tensor(d["data"]).type(d["dtype"]) # pylint: disable=E1101 + elif modname == "torch" and classname == "Tensor": + try: + import torch + + if "Complex" in d["dtype"]: + return torch.tensor( # pylint: disable=E1101 + [ + np.array(r) + np.array(i) * 1j + for r, i in zip(*d["data"]) + ], + ).type(d["dtype"]) + return torch.tensor(d["data"]).type(d["dtype"]) # pylint: disable=E1101 + + except ImportError: + pass elif np is not None and modname == "numpy" and classname == "array": if d["dtype"].startswith("complex"): From f389e249ef591bffa6a8b0b4bed55e9ca7987aa3 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 11:26:49 +0800 Subject: [PATCH 04/38] also lazily import numpy --- src/monty/json.py | 83 +++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index 25ae42a5..acf3420d 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -21,11 +21,6 @@ from typing import Any from uuid import UUID, uuid4 -try: - import numpy as np -except ImportError: - np = None - try: import pydantic except ImportError: @@ -594,7 +589,9 @@ def default(self, o) -> dict: d["data"] = o.numpy().tolist() return d - if np is not None: + try: + import numpy as np + if isinstance(o, np.ndarray): if str(o.dtype).startswith("complex"): return { @@ -611,6 +608,8 @@ def default(self, o) -> dict: } if isinstance(o, np.generic): return o.item() + except ImportError: + pass if _check_type(o, "pandas.core.frame.DataFrame"): return { @@ -800,6 +799,7 @@ def process_decoded(self, d): elif modname == "torch" and classname == "Tensor": try: + import numpy as np import torch if "Complex" in d["dtype"]: @@ -814,16 +814,22 @@ def process_decoded(self, d): except ImportError: pass - elif np is not None and modname == "numpy" and classname == "array": - if d["dtype"].startswith("complex"): - return np.array( - [ - np.array(r) + np.array(i) * 1j - for r, i in zip(*d["data"]) - ], - dtype=d["dtype"], - ) - return np.array(d["data"], dtype=d["dtype"]) + elif modname == "numpy" and classname == "array": + try: + import numpy as np + + if d["dtype"].startswith("complex"): + return np.array( + [ + np.array(r) + np.array(i) * 1j + for r, i in zip(*d["data"]) + ], + dtype=d["dtype"], + ) + return np.array(d["data"], dtype=d["dtype"]) + + except ImportError: + pass elif modname == "pandas": import pandas as pd @@ -926,6 +932,7 @@ def jsanitize( or (bson is not None and isinstance(obj, bson.objectid.ObjectId)) ): return obj + if isinstance(obj, (list, tuple)): return [ jsanitize( @@ -937,22 +944,30 @@ def jsanitize( ) for i in obj ] - if np is not None and isinstance(obj, np.ndarray): - try: - return [ - jsanitize( - i, - strict=strict, - allow_bson=allow_bson, - enum_values=enum_values, - recursive_msonable=recursive_msonable, - ) - for i in obj.tolist() - ] - except TypeError: - return obj.tolist() - if np is not None and isinstance(obj, np.generic): - return obj.item() + + try: + import numpy as np + + if isinstance(obj, np.ndarray): + try: + return [ + jsanitize( + i, + strict=strict, + allow_bson=allow_bson, + enum_values=enum_values, + recursive_msonable=recursive_msonable, + ) + for i in obj.tolist() + ] + except TypeError: + return obj.tolist() + + if isinstance(obj, np.generic): + return obj.item() + except ImportError: + pass + if _check_type( obj, ( @@ -962,6 +977,7 @@ def jsanitize( ), ): return obj.to_dict() + if isinstance(obj, dict): return { str(k): jsanitize( @@ -973,10 +989,13 @@ def jsanitize( ) for k, v in obj.items() } + if isinstance(obj, (int, float)): return obj + if obj is None: return None + if isinstance(obj, (pathlib.Path, datetime.datetime)): return str(obj) From 2bcc6cd5839c2b7127ce2ee6ca20aea41d242e4d Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 11:55:30 +0800 Subject: [PATCH 05/38] lazily import pydantic --- src/monty/json.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index acf3420d..d65adbec 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -21,16 +21,6 @@ from typing import Any from uuid import UUID, uuid4 -try: - import pydantic -except ImportError: - pydantic = None - -try: - from pydantic_core import core_schema -except ImportError: - core_schema = None - try: import bson except ImportError: @@ -328,8 +318,11 @@ def __get_pydantic_core_schema__(cls, source_type, handler): """ pydantic v2 core schema definition """ - if core_schema is None: - raise RuntimeError("Pydantic >= 2.0 is required for validation") + try: + from pydantic_core import core_schema + + except ImportError as exc: + raise RuntimeError("Pydantic >= 2.0 is required for validation") from exc s = core_schema.with_info_plain_validator_function(cls.validate_monty_v2) @@ -654,7 +647,7 @@ def default(self, o) -> dict: raise AttributeError(e) try: - if pydantic is not None and isinstance(o, pydantic.BaseModel): + if _check_type(o, "pydantic.main.BaseModel"): d = o.model_dump() elif ( dataclasses is not None @@ -784,11 +777,18 @@ def process_decoded(self, d): return cls_.from_dict(data) if issubclass(cls_, Enum): return cls_(d["value"]) - if pydantic is not None and issubclass( - cls_, pydantic.BaseModel - ): # pylint: disable=E1101 - d = {k: self.process_decoded(v) for k, v in data.items()} - return cls_(**d) + + try: + import pydantic + + if issubclass(cls_, pydantic.BaseModel): # pylint: disable=E1101 + d = { + k: self.process_decoded(v) for k, v in data.items() + } + return cls_(**d) + except ImportError: + pass + if ( dataclasses is not None and (not issubclass(cls_, MSONable)) @@ -1017,7 +1017,7 @@ def jsanitize( if isinstance(obj, str): return obj - if pydantic is not None and isinstance(obj, pydantic.BaseModel): # pylint: disable=E1101 + if _check_type(obj, "pydantic.main.BaseModel"): return jsanitize( MontyEncoder().default(obj), strict=strict, From acfeb0037a9b8f8fab76b5b6ae7b6e8799d337aa Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 12:02:48 +0800 Subject: [PATCH 06/38] lazily load ruamel.yaml --- src/monty/json.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index d65adbec..28311176 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -26,11 +26,6 @@ except ImportError: bson = None -try: - from ruamel.yaml import YAML -except ImportError: - YAML = None - try: import orjson except ImportError: @@ -42,6 +37,8 @@ def _load_redirect(redirect_file): try: + from ruamel.yaml import YAML + with open(redirect_file) as f: yaml = YAML() d = yaml.load(f) From 49cfde8a6057d5f7e1badaf026f7306077f4353a Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 12:36:30 +0800 Subject: [PATCH 07/38] fix typo and tweak warn msg --- src/monty/dev.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monty/dev.py b/src/monty/dev.py index 353674e4..36bd0ab7 100644 --- a/src/monty/dev.py +++ b/src/monty/dev.py @@ -232,7 +232,7 @@ def install_excepthook(hook_type: str = "color", **kwargs) -> int: try: from IPython.core import ultratb # pylint: disable=import-outside-toplevel except ImportError: - warnings.warn("Cannot install excepthook, IPyhon.core.ultratb not available") + warnings.warn("Cannot install excepthook, IPython not installed") return 1 # Select the hook. From 9efb43ccc0002e7fe6cf59b9565fd40fd0d7d3c4 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 12:43:50 +0800 Subject: [PATCH 08/38] declare optional dependencies --- pyproject.toml | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b0b8683a..fdd2f436 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,26 +19,38 @@ classifiers = [ "Topic :: Software Development :: Libraries :: Python Modules", ] dependencies = [ - + "ruamel.yaml", + "numpy<2.0.0", ] version = "2024.7.30" [project.optional-dependencies] +json = [ + "pydantic", + "orjson", + "bson", + "pandas", + "torch", + "pint", +] +serialization = [ + "msgpack", + ] +multiprocessing = [ + "tqdm", +] +dev = [ + "ipython", +] + ci = [ + "monty[json,serialization,multiprocessing,dev]", "pytest>=8", "pytest-cov>=4", "coverage", - "numpy<2.0.0", - "ruamel.yaml", - "msgpack", - "tqdm", - "pymongo", - "pandas", - "pint", - "orjson", + "pymongo", # TODO: not used? "types-orjson", "types-requests", - "torch" ] docs = [ "sphinx", From cf8a26088cd12127934474d2223c0f683c3d2f92 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 12:47:07 +0800 Subject: [PATCH 09/38] remove seeming unused pymongo --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index fdd2f436..16d7ea11 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,7 +48,6 @@ ci = [ "pytest>=8", "pytest-cov>=4", "coverage", - "pymongo", # TODO: not used? "types-orjson", "types-requests", ] From 473dbc453cfcad629de3f76052361a0aaa72a820 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 12:53:29 +0800 Subject: [PATCH 10/38] sort dependencies by alphabet order --- pyproject.toml | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 16d7ea11..2a219a6e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,36 +25,30 @@ dependencies = [ version = "2024.7.30" [project.optional-dependencies] -json = [ - "pydantic", - "orjson", - "bson", - "pandas", - "torch", - "pint", -] -serialization = [ - "msgpack", - ] -multiprocessing = [ - "tqdm", -] -dev = [ - "ipython", -] - ci = [ - "monty[json,serialization,multiprocessing,dev]", - "pytest>=8", - "pytest-cov>=4", - "coverage", - "types-orjson", - "types-requests", + "coverage", + "monty[optional]", + "pytest>=8", + "pytest-cov>=4", + "types-orjson", + "types-requests", ] +dev = ["ipython"] docs = [ "sphinx", "sphinx_rtd_theme", ] +json = [ + "bson", + "orjson", + "pandas", + "pydantic", + "pint", + "torch", +] +multiprocessing = ["tqdm"] +optional = ["monty[dev,json,multiprocessing,serialization]"] +serialization = ["msgpack"] [tool.setuptools.packages.find] where = ["src"] From 3449b4e4886e5e64edf7c977cf2120b5f4dc5548 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 12:57:46 +0800 Subject: [PATCH 11/38] add missing io --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 2a219a6e..3ab2a684 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,7 @@ docs = [ "sphinx", "sphinx_rtd_theme", ] +io = ["lzma"] json = [ "bson", "orjson", @@ -47,7 +48,7 @@ json = [ "torch", ] multiprocessing = ["tqdm"] -optional = ["monty[dev,json,multiprocessing,serialization]"] +optional = ["monty[dev,io,json,multiprocessing,serialization]"] serialization = ["msgpack"] [tool.setuptools.packages.find] From 6f7fdbd41c0bf712658202d45fe02b79cefc1866 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 13:01:03 +0800 Subject: [PATCH 12/38] remove lzma as its builtin after python 3.3 --- pyproject.toml | 3 +-- src/monty/io.py | 6 +----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3ab2a684..2a219a6e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,7 +38,6 @@ docs = [ "sphinx", "sphinx_rtd_theme", ] -io = ["lzma"] json = [ "bson", "orjson", @@ -48,7 +47,7 @@ json = [ "torch", ] multiprocessing = ["tqdm"] -optional = ["monty[dev,io,json,multiprocessing,serialization]"] +optional = ["monty[dev,json,multiprocessing,serialization]"] serialization = ["msgpack"] [tool.setuptools.packages.find] diff --git a/src/monty/io.py b/src/monty/io.py index f24a63dd..cf6ede4f 100644 --- a/src/monty/io.py +++ b/src/monty/io.py @@ -9,11 +9,7 @@ import errno import gzip import io - -try: - import lzma -except ImportError: - lzma = None # type: ignore[assignment] +import lzma import mmap import os import subprocess From 0c9d844615dce4bbcd028adaa29610a4b64c8b37 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 13:53:37 +0800 Subject: [PATCH 13/38] tweak tqdm import guard --- src/monty/multiprocessing.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/monty/multiprocessing.py b/src/monty/multiprocessing.py index 25511820..4b7c720f 100644 --- a/src/monty/multiprocessing.py +++ b/src/monty/multiprocessing.py @@ -9,8 +9,8 @@ try: from tqdm.autonotebook import tqdm -except ImportError: - tqdm = None +except ImportError as exc: + raise ImportError("tqdm must be installed for this function.") from exc def imap_tqdm(nprocs: int, func: Callable, iterable: Iterable, *args, **kwargs) -> list: @@ -28,8 +28,6 @@ def imap_tqdm(nprocs: int, func: Callable, iterable: Iterable, *args, **kwargs) Returns: Results of Pool.imap. """ - if tqdm is None: - raise ImportError("tqdm must be installed for this function.") data = [] with Pool(nprocs) as pool: try: From 78f16e5e2130dbef3f4bfa1e251df0a7f32066bd Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 13:55:00 +0800 Subject: [PATCH 14/38] drop guard for from ruamel.yaml import YAML --- src/monty/json.py | 8 ++------ src/monty/serialization.py | 5 +---- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index f9899097..282cfad3 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -21,6 +21,8 @@ from typing import Any from uuid import UUID, uuid4 +from ruamel.yaml import YAML + try: import numpy as np except ImportError: @@ -41,17 +43,11 @@ except ImportError: bson = None -try: - from ruamel.yaml import YAML -except ImportError: - YAML = None - try: import orjson except ImportError: orjson = None - try: import torch except ImportError: diff --git a/src/monty/serialization.py b/src/monty/serialization.py index a17b9913..6732a674 100644 --- a/src/monty/serialization.py +++ b/src/monty/serialization.py @@ -9,10 +9,7 @@ import os from typing import TYPE_CHECKING -try: - from ruamel.yaml import YAML -except ImportError: - YAML = None # type: ignore[arg-type] +from ruamel.yaml import YAML from monty.io import zopen from monty.json import MontyDecoder, MontyEncoder From fe58998e518d9ffc4339f6c428cd7e1bc91dc0e3 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 13:55:50 +0800 Subject: [PATCH 15/38] drop import guard for numpy --- src/monty/itertools.py | 5 +---- src/monty/json.py | 38 ++++++++++++++++++-------------------- tests/test_json.py | 10 ++-------- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/monty/itertools.py b/src/monty/itertools.py index 5a112a03..c4ed76d5 100644 --- a/src/monty/itertools.py +++ b/src/monty/itertools.py @@ -7,10 +7,7 @@ import itertools from typing import TYPE_CHECKING -try: - import numpy as np -except ImportError: - np = None +import numpy as np if TYPE_CHECKING: from typing import Iterable diff --git a/src/monty/json.py b/src/monty/json.py index 282cfad3..f03a7168 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -21,13 +21,9 @@ from typing import Any from uuid import UUID, uuid4 +import numpy as np from ruamel.yaml import YAML -try: - import numpy as np -except ImportError: - np = None - try: import pydantic except ImportError: @@ -595,23 +591,22 @@ def default(self, o) -> dict: d["data"] = o.numpy().tolist() return d - if np is not None: - if isinstance(o, np.ndarray): - if str(o.dtype).startswith("complex"): - return { - "@module": "numpy", - "@class": "array", - "dtype": str(o.dtype), - "data": [o.real.tolist(), o.imag.tolist()], - } + if isinstance(o, np.ndarray): + if str(o.dtype).startswith("complex"): return { "@module": "numpy", "@class": "array", "dtype": str(o.dtype), - "data": o.tolist(), + "data": [o.real.tolist(), o.imag.tolist()], } - if isinstance(o, np.generic): - return o.item() + return { + "@module": "numpy", + "@class": "array", + "dtype": str(o.dtype), + "data": o.tolist(), + } + if isinstance(o, np.generic): + return o.item() if _check_type(o, "pandas.core.frame.DataFrame"): return { @@ -809,7 +804,7 @@ def process_decoded(self, d): ).type(d["dtype"]) return torch.tensor(d["data"]).type(d["dtype"]) # pylint: disable=E1101 - elif np is not None and modname == "numpy" and classname == "array": + elif modname == "numpy" and classname == "array": if d["dtype"].startswith("complex"): return np.array( [ @@ -932,7 +927,8 @@ def jsanitize( ) for i in obj ] - if np is not None and isinstance(obj, np.ndarray): + + if isinstance(obj, np.ndarray): try: return [ jsanitize( @@ -946,8 +942,10 @@ def jsanitize( ] except TypeError: return obj.tolist() - if np is not None and isinstance(obj, np.generic): + + if isinstance(obj, np.generic): return obj.item() + if _check_type( obj, ( diff --git a/tests/test_json.py b/tests/test_json.py index 9c9e572c..097f1f53 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -8,10 +8,7 @@ from enum import Enum from typing import Union -try: - import numpy as np -except ImportError: - np = None +import numpy as np try: import pandas as pd @@ -564,7 +561,6 @@ def test_nan(self): d = json.loads(djson) assert isinstance(d[0], float) - @pytest.mark.skipif(np is None, reason="numpy not present") def test_numpy(self): x = np.array([1, 2, 3], dtype="int64") with pytest.raises(TypeError): @@ -872,9 +868,7 @@ def test_jsanitize_pandas(self): clean = jsanitize(s) assert clean == s.to_dict() - @pytest.mark.skipif( - np is None or ObjectId is None, reason="numpy and bson not present" - ) + @pytest.mark.skipif(ObjectId is None, reason="bson not present") def test_jsanitize_numpy_bson(self): d = { "a": ["b", np.array([1, 2, 3])], From 200da7ecbdf1adba6ed1516cf334814470d5ffcd Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 14:06:02 +0800 Subject: [PATCH 16/38] try to fix linting error, #702 --- pyproject.toml | 1 + tests/test_json.py | 25 ++++++++++++------------- tests/test_serialization.py | 5 ++--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2a219a6e..ea0cc143 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -106,3 +106,4 @@ lint.select = [ ] lint.isort.required-imports = ["from __future__ import annotations"] +lint.isort.known-first-party = ["monty"] diff --git a/tests/test_json.py b/tests/test_json.py index 097f1f53..055c8428 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -9,6 +9,17 @@ from typing import Union import numpy as np +import pytest +from monty.json import ( + MontyDecoder, + MontyEncoder, + MSONable, + _load_redirect, + jsanitize, + load, +) + +from . import __version__ as TESTS_VERSION try: import pandas as pd @@ -35,18 +46,6 @@ except ImportError: ObjectId = None -import pytest -from monty.json import ( - MontyDecoder, - MontyEncoder, - MSONable, - _load_redirect, - jsanitize, - load, -) - -from . import __version__ as tests_version - TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_files") @@ -317,7 +316,7 @@ def test_unsafe_hash(self): def test_version(self): obj = self.good_cls("Hello", "World", "Python") d = obj.as_dict() - assert d["@version"] == tests_version + assert d["@version"] == TESTS_VERSION def test_nested_to_from_dict(self): GMC = GoodMSONClass diff --git a/tests/test_serialization.py b/tests/test_serialization.py index d2e0cf9c..6d1e8ab9 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -6,15 +6,14 @@ import unittest import pytest +from monty.serialization import dumpfn, loadfn +from monty.tempfile import ScratchDir try: import msgpack except ImportError: msgpack = None -from monty.serialization import dumpfn, loadfn -from monty.tempfile import ScratchDir - class TestSerial: @classmethod From f27b13421a3a35f574a16a251c02dfd1a8c4387c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 20 Oct 2024 06:09:02 +0000 Subject: [PATCH 17/38] pre-commit auto-fixes --- tests/test_collections.py | 1 + tests/test_design_patterns.py | 1 + tests/test_dev.py | 1 + tests/test_fractions.py | 1 + tests/test_functools.py | 1 + tests/test_io.py | 1 + tests/test_json.py | 1 + tests/test_os.py | 1 + tests/test_serialization.py | 1 + tests/test_shutil.py | 1 + tests/test_tempfile.py | 1 + 11 files changed, 11 insertions(+) diff --git a/tests/test_collections.py b/tests/test_collections.py index 8057f7bc..58d2af9e 100644 --- a/tests/test_collections.py +++ b/tests/test_collections.py @@ -3,6 +3,7 @@ import os import pytest + from monty.collections import AttrDict, FrozenAttrDict, Namespace, frozendict, tree TEST_DIR = os.path.join(os.path.dirname(__file__), "test_files") diff --git a/tests/test_design_patterns.py b/tests/test_design_patterns.py index c37bc87d..67a7ffc2 100644 --- a/tests/test_design_patterns.py +++ b/tests/test_design_patterns.py @@ -6,6 +6,7 @@ from typing import Any import pytest + from monty.design_patterns import cached_class, singleton diff --git a/tests/test_dev.py b/tests/test_dev.py index 8c44d8ae..13bb211b 100644 --- a/tests/test_dev.py +++ b/tests/test_dev.py @@ -7,6 +7,7 @@ from unittest.mock import patch import pytest + from monty.dev import deprecated, install_excepthook, requires # Set all warnings to always be triggered. diff --git a/tests/test_fractions.py b/tests/test_fractions.py index 7d5f2770..230dcf5a 100644 --- a/tests/test_fractions.py +++ b/tests/test_fractions.py @@ -1,6 +1,7 @@ from __future__ import annotations import pytest + from monty.fractions import gcd, gcd_float, lcm diff --git a/tests/test_functools.py b/tests/test_functools.py index 8bf3fabf..120e6bd7 100644 --- a/tests/test_functools.py +++ b/tests/test_functools.py @@ -5,6 +5,7 @@ import unittest import pytest + from monty.functools import ( TimeoutError, lazy_property, diff --git a/tests/test_io.py b/tests/test_io.py index 9daa17be..052bc471 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -5,6 +5,7 @@ from unittest.mock import patch import pytest + from monty.io import ( FileLock, FileLockException, diff --git a/tests/test_json.py b/tests/test_json.py index 055c8428..e59e89d5 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -10,6 +10,7 @@ import numpy as np import pytest + from monty.json import ( MontyDecoder, MontyEncoder, diff --git a/tests/test_os.py b/tests/test_os.py index c6ca8d7c..af5b156f 100644 --- a/tests/test_os.py +++ b/tests/test_os.py @@ -4,6 +4,7 @@ from pathlib import Path import pytest + from monty.os import cd, makedirs_p from monty.os.path import find_exts, zpath diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 6d1e8ab9..e7c853a5 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -6,6 +6,7 @@ import unittest import pytest + from monty.serialization import dumpfn, loadfn from monty.tempfile import ScratchDir diff --git a/tests/test_shutil.py b/tests/test_shutil.py index 16cd9aa7..e506b4ae 100644 --- a/tests/test_shutil.py +++ b/tests/test_shutil.py @@ -9,6 +9,7 @@ from pathlib import Path import pytest + from monty.shutil import ( compress_dir, compress_file, diff --git a/tests/test_tempfile.py b/tests/test_tempfile.py index 4dfd3b17..088aec67 100644 --- a/tests/test_tempfile.py +++ b/tests/test_tempfile.py @@ -4,6 +4,7 @@ import shutil import pytest + from monty.tempfile import ScratchDir TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_files") From d18a1340d9cd5ebeb67ca7fe54a2e2f1763228d9 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 14:14:43 +0800 Subject: [PATCH 18/38] also add opt in task.py --- pyproject.toml | 2 ++ tasks.py | 1 + 2 files changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index ea0cc143..08798faa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ ci = [ "types-orjson", "types-requests", ] +# dev is for "dev" module, not for development dev = ["ipython"] docs = [ "sphinx", @@ -49,6 +50,7 @@ json = [ multiprocessing = ["tqdm"] optional = ["monty[dev,json,multiprocessing,serialization]"] serialization = ["msgpack"] +task = ["requests", "invoke"] [tool.setuptools.packages.find] where = ["src"] diff --git a/tasks.py b/tasks.py index 921412ad..7432034a 100755 --- a/tasks.py +++ b/tasks.py @@ -11,6 +11,7 @@ import requests from invoke import task + from monty import __version__ as ver from monty.os import cd From 20e81bbae60dd01da5c18bbc94b1ae36c51d7f4e Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 14:18:35 +0800 Subject: [PATCH 19/38] suppress mypy error --- src/monty/json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monty/json.py b/src/monty/json.py index f03a7168..67c3e0ed 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -659,7 +659,7 @@ def default(self, o) -> dict: and dataclasses.is_dataclass(o) ): # This handles dataclasses that are not subclasses of MSONAble. - d = dataclasses.asdict(o) # type: ignore[call-overload] + d = dataclasses.asdict(o) # type: ignore[call-overload, arg-type] elif hasattr(o, "as_dict"): d = o.as_dict() elif isinstance(o, Enum): From 002f5af9e61cfce1c8dc97d34c86e846846c3175 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 14:23:04 +0800 Subject: [PATCH 20/38] revert lazy import ruamel, no obvious improvement --- src/monty/json.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index 28311176..d97d2bb6 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -21,6 +21,8 @@ from typing import Any from uuid import UUID, uuid4 +from ruamel.yaml import YAML + try: import bson except ImportError: @@ -37,8 +39,6 @@ def _load_redirect(redirect_file): try: - from ruamel.yaml import YAML - with open(redirect_file) as f: yaml = YAML() d = yaml.load(f) From d3cf3651eb5ff6d3f6a79b4a2560d14e9d1c6bbb Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 14:40:44 +0800 Subject: [PATCH 21/38] tweak error msg when ipython not installed --- src/monty/dev.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/monty/dev.py b/src/monty/dev.py index 36bd0ab7..cf468bce 100644 --- a/src/monty/dev.py +++ b/src/monty/dev.py @@ -231,9 +231,8 @@ def install_excepthook(hook_type: str = "color", **kwargs) -> int: """ try: from IPython.core import ultratb # pylint: disable=import-outside-toplevel - except ImportError: - warnings.warn("Cannot install excepthook, IPython not installed") - return 1 + except ImportError as exc: + raise ImportError("Cannot install excepthook, IPython not installed") from exc # Select the hook. hook = dict( From b558f211ff1b5f86a065a9f94fd0fd499671a38b Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 14:41:47 +0800 Subject: [PATCH 22/38] remove lzma check --- src/monty/io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monty/io.py b/src/monty/io.py index cf6ede4f..9ca0f2cb 100644 --- a/src/monty/io.py +++ b/src/monty/io.py @@ -45,7 +45,7 @@ def zopen(filename: Union[str, Path], *args, **kwargs) -> IO: return bz2.open(filename, *args, **kwargs) if ext in {".GZ", ".Z"}: return gzip.open(filename, *args, **kwargs) - if lzma is not None and ext in {".XZ", ".LZMA"}: + if ext in {".XZ", ".LZMA"}: return lzma.open(filename, *args, **kwargs) return open(filename, *args, **kwargs) From b8e0f51b1a5aab4883811bf2996160b70ae01299 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 14:58:26 +0800 Subject: [PATCH 23/38] remove type stub for orjson --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 08798faa..cec518cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,6 @@ ci = [ "monty[optional]", "pytest>=8", "pytest-cov>=4", - "types-orjson", "types-requests", ] # dev is for "dev" module, not for development From 689de319d079cbfad59e14c17f814f8f57d10a09 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 15:01:27 +0800 Subject: [PATCH 24/38] continue test upon failure --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 24e816d9..0a70457b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,6 +5,7 @@ on: [push, pull_request, workflow_call] jobs: build: strategy: + fail-fast: false max-parallel: 20 matrix: os: [ubuntu-latest, macos-14, windows-latest] From 3edd4d5009d37d901ca1b5da055110fe607ab8f4 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 15:02:14 +0800 Subject: [PATCH 25/38] perhaps set orjson lower bound just in case --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index cec518cf..7c10b37b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,7 @@ docs = [ ] json = [ "bson", - "orjson", + "orjson>=3.6.1", "pandas", "pydantic", "pint", From b89d7a913c05684a87ec100bb4705919764b6195 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 15:02:35 +0800 Subject: [PATCH 26/38] remove necessary version as could be retrieved from install log --- .github/workflows/lint.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 5e4be400..2e7472b6 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -19,12 +19,9 @@ jobs: - name: ruff run: | - ruff --version ruff check . ruff format --check . - name: mypy run: | - mypy --version - rm -rf .mypy_cache mypy src From 8a4d33f99ac9cfc45b1ed813d7629184c64f7310 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 17:54:37 +0800 Subject: [PATCH 27/38] Revert "remove necessary version as could be retrieved from install log" This reverts commit b89d7a913c05684a87ec100bb4705919764b6195. --- .github/workflows/lint.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 2e7472b6..5e4be400 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -19,9 +19,12 @@ jobs: - name: ruff run: | + ruff --version ruff check . ruff format --check . - name: mypy run: | + mypy --version + rm -rf .mypy_cache mypy src From f67796c4eda916ce1cc06acad7c47d568bcf215a Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 18:38:10 +0800 Subject: [PATCH 28/38] E1101 should disappear --- src/monty/json.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index d97d2bb6..701f4f93 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -778,7 +778,7 @@ def process_decoded(self, d): try: import pydantic - if issubclass(cls_, pydantic.BaseModel): # pylint: disable=E1101 + if issubclass(cls_, pydantic.BaseModel): d = { k: self.process_decoded(v) for k, v in data.items() } @@ -800,13 +800,13 @@ def process_decoded(self, d): import torch if "Complex" in d["dtype"]: - return torch.tensor( # pylint: disable=E1101 + return torch.tensor( [ np.array(r) + np.array(i) * 1j for r, i in zip(*d["data"]) ], ).type(d["dtype"]) - return torch.tensor(d["data"]).type(d["dtype"]) # pylint: disable=E1101 + return torch.tensor(d["data"]).type(d["dtype"]) except ImportError: pass @@ -871,8 +871,8 @@ def decode(self, s): """ if orjson is not None: try: - d = orjson.loads(s) # pylint: disable=E1101 - except orjson.JSONDecodeError: # pylint: disable=E1101 + d = orjson.loads(s) + except orjson.JSONDecodeError: d = json.loads(s) else: d = json.loads(s) From 69cc7d9c949e3ff1464d0073cff118be0709a29e Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 18:43:58 +0800 Subject: [PATCH 29/38] use f-string --- src/monty/json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monty/json.py b/src/monty/json.py index 701f4f93..d7d11d5f 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -98,7 +98,7 @@ class B(A): mro = type(obj).mro() except TypeError: return False - return any(o.__module__ + "." + o.__name__ == ts for o in mro for ts in type_str) + return any(f"{o.__module__}.{o.__name__}" == ts for o in mro for ts in type_str) class MSONable: From 14c1e3e981308d73bc2c36612c1d032635c703a9 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sun, 20 Oct 2024 18:49:04 +0800 Subject: [PATCH 30/38] add type --- src/monty/json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monty/json.py b/src/monty/json.py index d7d11d5f..f065a2f6 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -64,7 +64,7 @@ def _load_redirect(redirect_file): return dict(redirect_dict) -def _check_type(obj, type_str) -> bool: +def _check_type(obj, type_str: tuple[str, ...] | str) -> bool: """Alternative to isinstance that avoids imports. Checks whether obj is an instance of the type defined by type_str. This From 3b8d854ffd0629a518bf4ca647c98981a160b574 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Mon, 21 Oct 2024 10:15:52 +0800 Subject: [PATCH 31/38] eagerly import numpy --- src/monty/json.py | 98 +++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 55 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index f065a2f6..06574135 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -18,11 +18,15 @@ from importlib import import_module from inspect import getfullargspec from pathlib import Path -from typing import Any +from typing import TYPE_CHECKING from uuid import UUID, uuid4 +import numpy as np from ruamel.yaml import YAML +if TYPE_CHECKING: + from typing import Any + try: import bson except ImportError: @@ -521,7 +525,7 @@ def _recursive_name_object_map_replacement(d, name_object_map): class MontyEncoder(json.JSONEncoder): """ A Json Encoder which supports the MSONable API, plus adds support for - numpy arrays, datetime objects, bson ObjectIds (requires bson). + NumPy arrays, datetime objects, bson ObjectIds (requires bson). Usage:: # Add it as a *cls* keyword when using json.dump json.dumps(object, cls=MontyEncoder) @@ -579,27 +583,23 @@ def default(self, o) -> dict: d["data"] = o.numpy().tolist() return d - try: - import numpy as np - - if isinstance(o, np.ndarray): - if str(o.dtype).startswith("complex"): - return { - "@module": "numpy", - "@class": "array", - "dtype": str(o.dtype), - "data": [o.real.tolist(), o.imag.tolist()], - } + if isinstance(o, np.ndarray): + if str(o.dtype).startswith("complex"): return { "@module": "numpy", "@class": "array", "dtype": str(o.dtype), - "data": o.tolist(), + "data": [o.real.tolist(), o.imag.tolist()], } - if isinstance(o, np.generic): - return o.item() - except ImportError: - pass + return { + "@module": "numpy", + "@class": "array", + "dtype": str(o.dtype), + "data": o.tolist(), + } + + if isinstance(o, np.generic): + return o.item() if _check_type(o, "pandas.core.frame.DataFrame"): return { @@ -796,7 +796,6 @@ def process_decoded(self, d): elif modname == "torch" and classname == "Tensor": try: - import numpy as np import torch if "Complex" in d["dtype"]: @@ -812,21 +811,15 @@ def process_decoded(self, d): pass elif modname == "numpy" and classname == "array": - try: - import numpy as np - - if d["dtype"].startswith("complex"): - return np.array( - [ - np.array(r) + np.array(i) * 1j - for r, i in zip(*d["data"]) - ], - dtype=d["dtype"], - ) - return np.array(d["data"], dtype=d["dtype"]) - - except ImportError: - pass + if d["dtype"].startswith("complex"): + return np.array( + [ + np.array(r) + np.array(i) * 1j + for r, i in zip(*d["data"]) + ], + dtype=d["dtype"], + ) + return np.array(d["data"], dtype=d["dtype"]) elif modname == "pandas": import pandas as pd @@ -942,28 +935,23 @@ def jsanitize( for i in obj ] - try: - import numpy as np + if isinstance(obj, np.ndarray): + try: + return [ + jsanitize( + i, + strict=strict, + allow_bson=allow_bson, + enum_values=enum_values, + recursive_msonable=recursive_msonable, + ) + for i in obj.tolist() + ] + except TypeError: + return obj.tolist() - if isinstance(obj, np.ndarray): - try: - return [ - jsanitize( - i, - strict=strict, - allow_bson=allow_bson, - enum_values=enum_values, - recursive_msonable=recursive_msonable, - ) - for i in obj.tolist() - ] - except TypeError: - return obj.tolist() - - if isinstance(obj, np.generic): - return obj.item() - except ImportError: - pass + if isinstance(obj, np.generic): + return obj.item() if _check_type( obj, From 100055d4c67b435ebf29acc18d3f6fbfec835c9a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2024 03:22:06 +0000 Subject: [PATCH 32/38] Bump webrick from 1.8.1 to 1.8.2 in /docs Bumps [webrick](https://github.com/ruby/webrick) from 1.8.1 to 1.8.2. - [Release notes](https://github.com/ruby/webrick/releases) - [Commits](https://github.com/ruby/webrick/compare/v1.8.1...v1.8.2) --- updated-dependencies: - dependency-name: webrick dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- docs/Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Gemfile.lock b/docs/Gemfile.lock index 9ecbb76b..33b6e7a2 100644 --- a/docs/Gemfile.lock +++ b/docs/Gemfile.lock @@ -251,7 +251,7 @@ GEM unf_ext unf_ext (0.0.8.2) unicode-display_width (1.8.0) - webrick (1.8.1) + webrick (1.8.2) PLATFORMS arm64-darwin-22 From 226194b56567f17897bc1ccca9f0ad3d9a2874cd Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Mon, 21 Oct 2024 11:30:03 +0800 Subject: [PATCH 33/38] push a random change to rerun CI, torch install is flaky as always --- src/monty/json.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/monty/json.py b/src/monty/json.py index b18e4925..46c1807e 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -41,7 +41,7 @@ __version__ = "3.0.0" -def _load_redirect(redirect_file): +def _load_redirect(redirect_file) -> dict: try: with open(redirect_file) as f: yaml = YAML() @@ -52,7 +52,7 @@ def _load_redirect(redirect_file): return {} # Convert the full paths to module/class - redirect_dict = defaultdict(dict) + redirect_dict: dict = defaultdict(dict) for old_path, new_path in d.items(): old_class = old_path.split(".")[-1] old_module = ".".join(old_path.split(".")[:-1]) From bc58f699bba5a271ced5c13c1f529e8cc55d9eaf Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Mon, 21 Oct 2024 11:35:47 +0800 Subject: [PATCH 34/38] add comment about lazy import --- src/monty/json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monty/json.py b/src/monty/json.py index 46c1807e..d0626d3e 100644 --- a/src/monty/json.py +++ b/src/monty/json.py @@ -796,7 +796,7 @@ def process_decoded(self, d): elif modname == "torch" and classname == "Tensor": try: - import torch + import torch # import torch is very expensive if "Complex" in d["dtype"]: return torch.tensor( From 6f9580ca0851ced335eb586e43ea2ed0afe84042 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Mon, 21 Oct 2024 11:56:48 +0800 Subject: [PATCH 35/38] bump setup python version --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 5e4be400..170abea9 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -9,7 +9,7 @@ jobs: - uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: "3.x" From 4a8377b0213e4f177cd5f0df186dedf7c04882ac Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Mon, 21 Oct 2024 19:07:27 +0800 Subject: [PATCH 36/38] pre-commit migrate-config --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ca69d8fa..5ce97986 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,7 +32,7 @@ repos: rev: v2.3.0 hooks: - id: codespell - stages: [commit, commit-msg] + stages: [pre-commit, commit-msg] exclude_types: [html] additional_dependencies: [tomli] # needed to read pyproject.toml below py3.11 From f33fd5a0ac5942a4b38ddbb5c29ae2877f944208 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Mon, 21 Oct 2024 19:17:31 +0800 Subject: [PATCH 37/38] pin 3.12 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0a70457b..77a3f9a7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ jobs: max-parallel: 20 matrix: os: [ubuntu-latest, macos-14, windows-latest] - python-version: ["3.9", "3.x"] + python-version: ["3.9", "3.12"] runs-on: ${{ matrix.os }} From 7c38a27f24c61a0687253de651f666c6d9b61e28 Mon Sep 17 00:00:00 2001 From: Shyue Ping Ong Date: Mon, 21 Oct 2024 09:04:59 -0700 Subject: [PATCH 38/38] Add monty.* --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 8652a3cd..8c39c6fd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,7 +46,7 @@ docs = [ [tool.setuptools.packages.find] where = ["src"] -include = ["monty"] +include = ["monty", "monty.*"] [tool.black] line-length = 120