Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix TestClass name, make module level variable all capital and other code cleanup #702

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b1bece6
update setup action to v5
DanielYang59 Jul 26, 2024
1847581
fix exclude entry in pre-commit config
DanielYang59 Jul 26, 2024
e914a98
try to commit local pre-commit changes
DanielYang59 Jul 26, 2024
9ea1f23
pre-commit auto-fixes
pre-commit-ci[bot] Jul 26, 2024
b7381d2
test
DanielYang59 Jul 26, 2024
588a3e8
relocate guarded import blocks to the end
DanielYang59 Jul 26, 2024
f75fc0c
pre-commit auto-fixes
pre-commit-ci[bot] Jul 26, 2024
8839c80
debug: trigger workflow
DanielYang59 Jul 26, 2024
6bda30b
avoid print in unit tests
DanielYang59 Jul 26, 2024
134a25d
remove unused and commented out print
DanielYang59 Jul 26, 2024
19d76a5
remove guard for Python 2.7
DanielYang59 Jul 26, 2024
0150810
remove redundant blank line
DanielYang59 Jul 26, 2024
29ce0dc
Use more detailed debug msg
DanielYang59 Jul 26, 2024
31cd6ac
rename test class to avoid PytestCollectionWarning
DanielYang59 Jul 26, 2024
b7e9e47
install ipython in ci for test_install_except_hook
DanielYang59 Jul 26, 2024
1485153
install bson in CI
DanielYang59 Jul 26, 2024
9829038
install pydantic instead of bson
DanielYang59 Jul 26, 2024
a45b3b4
use capital for global var name
DanielYang59 Jul 26, 2024
9161eec
var name tweaks
DanielYang59 Jul 29, 2024
b78e6da
rename to 3000_lines_gz.txt.gz to avoid overlap name
DanielYang59 Jul 29, 2024
24995d8
Revert "rename to 3000_lines_gz.txt.gz to avoid overlap name"
DanielYang59 Jul 29, 2024
a235beb
Merge branch 'master' into consistent-ruff
DanielYang59 Jul 29, 2024
fa96b7d
Merge branch 'master' into consistent-ruff
DanielYang59 Aug 15, 2024
29605f3
bump to ruff 0.6
DanielYang59 Aug 22, 2024
0fcc9ca
ruff fix tasks.py
DanielYang59 Aug 22, 2024
0fd0b3d
Merge remote-tracking branch 'upstream/master' into consistent-ruff
DanielYang59 Oct 21, 2024
242f2de
clean up merge issue
DanielYang59 Oct 21, 2024
aa8e796
Merge branch 'master' into consistent-ruff
DanielYang59 Oct 22, 2024
eee712e
make module level var all cap
DanielYang59 Oct 22, 2024
bfe9431
enhance comment
DanielYang59 Nov 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -26,5 +26,4 @@ jobs:
- name: mypy
run: |
mypy --version
rm -rf .mypy_cache
mypy src
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude: ^(docs|tests/files|cmd_line|tasks.py)
exclude: ^(docs|tests/test_files|tasks.py)
DanielYang59 marked this conversation as resolved.
Show resolved Hide resolved

ci:
autoupdate_schedule: monthly
Expand All @@ -8,7 +8,7 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0
rev: v0.5.5
hooks:
- id: ruff
args: [--fix]
Expand All @@ -24,7 +24,7 @@ repos:
exclude: ^tests

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.1
rev: v1.11.0
hooks:
- id: mypy

Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ ci = [
"orjson",
"types-orjson",
"types-requests",
"torch"
"torch",
"ipython",
"pydantic",
]
docs = [
"sphinx",
Expand Down
10 changes: 5 additions & 5 deletions src/monty/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
import errno
import gzip
import io

try:
import lzma
except ImportError:
lzma = None # type: ignore[assignment]
import mmap
import os
import subprocess
import time
from pathlib import Path
from typing import TYPE_CHECKING

try:
import lzma
except ImportError:
lzma = None # type: ignore[assignment]

if TYPE_CHECKING:
from typing import IO, Generator, Union

Expand Down
1 change: 0 additions & 1 deletion src/monty/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
except ImportError:
orjson = None


try:
import torch
except ImportError:
Expand Down
8 changes: 4 additions & 4 deletions src/monty/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import os
from typing import TYPE_CHECKING

from monty.io import zopen
from monty.json import MontyDecoder, MontyEncoder
from monty.msgpack import default, object_hook

try:
from ruamel.yaml import YAML
except ImportError:
YAML = None # type: ignore[arg-type]

from monty.io import zopen
from monty.json import MontyDecoder, MontyEncoder
from monty.msgpack import default, object_hook

try:
import msgpack
except ImportError:
Expand Down
3 changes: 0 additions & 3 deletions src/monty/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ def run(self, timeout: Optional[float] = None, **kwargs) -> Self:

def target(**kw):
try:
# print('Thread started')
with Popen(self.command, **kw) as self.process:
self.output, self.error = self.process.communicate()
self.retcode = self.process.returncode
# print('Thread stopped')
except Exception:
self.error = traceback.format_exc()
self.retcode = -1
Expand All @@ -96,7 +94,6 @@ def target(**kw):
thread.join(timeout)

if thread.is_alive() and self.process is not None:
# print("Terminating process")
self.process.terminate()
self.killed = True
thread.join()
Expand Down
14 changes: 2 additions & 12 deletions src/monty/termcolor.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,7 @@ def cprint(

It accepts arguments of print function.
"""
try:
print((colored(text, color, on_color, attrs)), **kwargs)
except TypeError:
# flush is not supported by py2.7
kwargs.pop("flush", None)
print((colored(text, color, on_color, attrs)), **kwargs)
print((colored(text, color, on_color, attrs)), **kwargs)


def colored_map(text: str, cmap: dict) -> str:
Expand Down Expand Up @@ -162,12 +157,7 @@ def cprint_map(text: str, cmap: dict, **kwargs) -> None:
Examples:
cprint_map("Hello world", {"Hello": "red"})
"""
try:
print(colored_map(text, cmap), **kwargs)
except TypeError:
# flush is not supported by py2.7
kwargs.pop("flush", None)
print(colored_map(text, cmap), **kwargs)
print(colored_map(text, cmap), **kwargs)


def get_terminal_size():
Expand Down
16 changes: 8 additions & 8 deletions tests/test_design_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,32 @@ def test_pickle(self):


@cached_class
class TestClass:
class ClassForTest:
def __init__(self, value: Any) -> None:
self.value = value


def test_caching():
# Test that instances are cached
inst1 = TestClass(1)
inst2 = TestClass(1)
inst1 = ClassForTest(1)
inst2 = ClassForTest(1)
assert inst1 is inst2

inst3 = TestClass(2)
inst3 = ClassForTest(2)
assert inst1 is not inst3


def test_picklability():
# Test that instances can be pickled and unpickled
original = TestClass(42)
original = ClassForTest(42)
pickled = pickle.dumps(original)
unpickled = pickle.loads(pickled)

# Check that the unpickled instance has the same value
assert original.value == unpickled.value

# Check that the unpickled instance is the same as a newly created instance
new_instance = TestClass(42)
new_instance = ClassForTest(42)
assert unpickled is new_instance


Expand All @@ -97,8 +97,8 @@ def __init__(self):

def test_class_identity():
# Ensure the decorated class is still recognized as the original class
assert isinstance(TestClass(1), TestClass)
assert type(TestClass(1)) is TestClass
assert isinstance(ClassForTest(1), ClassForTest)
assert type(ClassForTest(1)) is ClassForTest


def test_multiple_arguments():
Expand Down
6 changes: 3 additions & 3 deletions tests/test_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class TestClassNew:
"""A dummy class for tests."""

def __post_init__(self):
print("Hello.")
pass

def method_a(self):
pass
Expand All @@ -155,7 +155,7 @@ class TestClassOld:
class_attrib_old = "OLD_ATTRIB"

def __post_init__(self):
print("Hello.")
pass

def method_b(self):
"""This is method_b."""
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_requires(self):

@requires(fictitious_mod is not None, err_msg)
def use_fictitious_mod():
print("success")
pass

with pytest.raises(RuntimeError, match=err_msg):
use_fictitious_mod()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_reverse_readfile_gz(self):
Make sure a file containing line numbers is read in reverse order,
i.e. the first line that is read corresponds to the last line number.
"""
fname = os.path.join(TEST_DIR, "3000_lines.txt.gz")
fname = os.path.join(TEST_DIR, "3000_lines_gz.txt.gz")
for idx, line in enumerate(reverse_readfile(fname)):
assert int(line) == self.NUMLINES - idx

Expand Down
30 changes: 15 additions & 15 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
from enum import Enum
from typing import Union

import pytest
from monty.json import (
MontyDecoder,
MontyEncoder,
MSONable,
_load_redirect,
jsanitize,
load,
)

from . import __version__ as TESTS_VERSION

try:
import numpy as np
except ImportError:
Expand Down Expand Up @@ -38,18 +50,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")


Expand Down Expand Up @@ -320,7 +320,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
Expand Down Expand Up @@ -682,7 +682,7 @@ def test_pint_quantity(self):
cls = ClassContainingQuantity(qty=pint.Quantity("9.81 m/s**2"))

d = json.loads(MontyEncoder().encode(cls))
print(d)
assert isinstance(d, dict)

assert d["qty"]["@module"] == "pint"
assert d["qty"]["@class"] == "Quantity"
Expand Down Expand Up @@ -873,7 +873,7 @@ def test_jsanitize_pandas(self):
assert clean == s.to_dict()

@pytest.mark.skipif(
np is None or ObjectId is None, reason="numpy and bson not present"
np is None or ObjectId is None, reason="numpy or bson not present"
)
def test_jsanitize_numpy_bson(self):
d = {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def __str__(self):
],
)

print(draw_tree(root))
assert draw_tree(root)
5 changes: 2 additions & 3 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ def test_command():
sleep05 = Command("sleep 0.5")

sleep05.run(timeout=1)
print(sleep05)
assert sleep05.retcode == 0
Copy link
Contributor Author

@DanielYang59 DanielYang59 Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unit test seems to fail intermittently (seemingly in Windows only), I guess that's probably the reason the print exists.

First time failure: https://github.com/materialsvirtuallab/monty/actions/runs/10104741275/job/27944181978

Second time: https://github.com/materialsvirtuallab/monty/actions/runs/10104606892/job/27943837091

But still need to recreate this issue in the first place

# DEBUG: this unit test fail in Win CI sometimes
full_msg = f"{sleep05=}\n{sleep05.error=}\n{sleep05.output}"
assert sleep05.retcode == 0, full_msg
DanielYang59 marked this conversation as resolved.
Show resolved Hide resolved
assert not sleep05.killed

sleep05.run(timeout=0.1)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_with_copy_gzip(self):
# We write a pre-scratch file.
with open("pre_scratch_text", "w") as f:
f.write("write")
init_gz = [f for f in os.listdir(os.getcwd()) if f.endswith(".gz")]
init_gz_files = [f for f in os.listdir(os.getcwd()) if f.endswith(".gz")]
with (
ScratchDir(
self.scratch_root,
Expand All @@ -62,10 +62,10 @@ def test_with_copy_gzip(self):
f.write("write")
files = os.listdir(os.getcwd())

# Make sure the stratch_text.gz exists
# Make sure the scratch_text.gz exists
assert "scratch_text.gz" in files
for f in files:
if f.endswith(".gz") and f not in init_gz:
if f.endswith(".gz") and f not in init_gz_files:
os.remove(f)
os.remove("pre_scratch_text")

Expand Down