Skip to content

Commit 9eb253c

Browse files
committed
ruff unignore SIM115, fix pymatgen/io/qchem/tests/test_inputs.py
1 parent 742f461 commit 9eb253c

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

pymatgen/alchemy/tests/test_materials.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ def test_final_structure(self):
6363
assert self.trans.final_structure.composition.reduced_formula == "NaFePO4"
6464

6565
def test_from_dict(self):
66-
d = json.load(open(os.path.join(PymatgenTest.TEST_FILES_DIR, "transformations.json")))
67-
d["other_parameters"] = {"tags": ["test"]}
68-
ts = TransformedStructure.from_dict(d)
66+
with open(os.path.join(PymatgenTest.TEST_FILES_DIR, "transformations.json")) as file:
67+
dct = json.load(file)
68+
dct["other_parameters"] = {"tags": ["test"]}
69+
ts = TransformedStructure.from_dict(dct)
6970
ts.other_parameters["author"] = "Will"
7071
ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
7172
assert ts.final_structure.composition.reduced_formula == "MnPO4"

pymatgen/io/abinit/abitimer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def parse(self, filenames):
111111
read_ok = []
112112
for fname in filenames:
113113
try:
114-
fh = open(fname) # pylint: disable=R1732
114+
fh = open(fname) # noqa: SIM115
115115
except OSError:
116116
logger.warning(f"Cannot open file {fname}")
117117
continue
@@ -695,7 +695,7 @@ def to_csv(self, fileobj=sys.stdout):
695695
openclose = is_string(fileobj)
696696

697697
if openclose:
698-
fileobj = open(fileobj, "w") # pylint: disable=R1732
698+
fileobj = open(fileobj, "w") # noqa: SIM115
699699

700700
for idx, section in enumerate(self.sections):
701701
fileobj.write(section.to_csvline(with_header=(idx == 0)))

pymatgen/io/qchem/tests/test_inputs.py

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,15 +1181,14 @@ def test_write_file_from_OptSet(self):
11811181
odd_mol = odd_dict["spec"]["_tasks"][0]["molecule"]
11821182
qcinp = OptSet(odd_mol)
11831183
qcinp.write_file(os.path.join(os.path.dirname(__file__), "test.qin"))
1184-
test_file = open(os.path.join(os.path.dirname(__file__), "test.qin"))
1185-
ref_file = open(os.path.join(os.path.dirname(__file__), "test_ref.qin"))
1184+
test_path = os.path.join(os.path.dirname(__file__), "test.qin")
1185+
ref_path = os.path.join(os.path.dirname(__file__), "test_ref.qin")
11861186

1187-
for l_test, l_ref in zip(test_file, ref_file):
1188-
# By default, if this statement fails the offending line will be printed
1189-
assert l_test == l_ref
1187+
with open(ref_path) as ref_file, open(test_path) as test_file:
1188+
for l_test, l_ref in zip(test_file, ref_file):
1189+
# By default, if this statement fails the offending line will be printed
1190+
assert l_test == l_ref
11901191

1191-
test_file.close()
1192-
ref_file.close()
11931192
os.remove(os.path.join(os.path.dirname(__file__), "test.qin"))
11941193

11951194
def test_write_file_from_OptSet_with_vdw(self):
@@ -1199,59 +1198,55 @@ def test_write_file_from_OptSet_with_vdw(self):
11991198
odd_mol = odd_dict["spec"]["_tasks"][0]["molecule"]
12001199
qcinp = OptSet(odd_mol, overwrite_inputs={"van_der_waals": {"16": 3.14159}})
12011200
qcinp.write_file(os.path.join(os.path.dirname(__file__), "test_vdw.qin"))
1202-
test_file = open(os.path.join(os.path.dirname(__file__), "test_vdw.qin"))
1203-
ref_file = open(os.path.join(os.path.dirname(__file__), "test_ref_vdw.qin"))
1201+
test_path = os.path.join(os.path.dirname(__file__), "test_vdw.qin")
1202+
ref_path = os.path.join(os.path.dirname(__file__), "test_ref_vdw.qin")
12041203

1205-
for l_test, l_ref in zip(test_file, ref_file):
1206-
# By default, if this statement fails the offending line will be printed
1207-
assert l_test == l_ref
1204+
with open(ref_path) as ref_file, open(test_path) as test_file:
1205+
for l_test, l_ref in zip(test_file, ref_file):
1206+
# By default, if this statement fails the offending line will be printed
1207+
assert l_test == l_ref
12081208

1209-
test_file.close()
1210-
ref_file.close()
12111209
os.remove(os.path.join(os.path.dirname(__file__), "test_vdw.qin"))
12121210

12131211
def test_read_write_nbo7(self):
12141212
qcinp = QCInput.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "nbo7.qin"))
12151213
qcinp.write_file(os.path.join(os.path.dirname(__file__), "test_nbo7.qin"))
1216-
test_file = open(os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "nbo7.qin"))
1217-
ref_file = open(os.path.join(os.path.dirname(__file__), "test_nbo7.qin"))
1214+
test_path = os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "nbo7.qin")
1215+
ref_path = os.path.join(os.path.dirname(__file__), "test_nbo7.qin")
12181216

1219-
for l_test, l_ref in zip(test_file, ref_file):
1220-
# By default, if this statement fails the offending line will be printed
1221-
assert l_test == l_ref
1217+
with open(ref_path) as ref_file, open(test_path) as test_file:
1218+
for l_test, l_ref in zip(test_file, ref_file):
1219+
# By default, if this statement fails the offending line will be printed
1220+
assert l_test == l_ref
12221221

1223-
test_file.close()
1224-
ref_file.close()
12251222
os.remove(os.path.join(os.path.dirname(__file__), "test_nbo7.qin"))
12261223

12271224
def test_read_write_nbo_e2pert(self):
12281225
qcinp = QCInput.from_file(
12291226
os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "e2pert.qin")
12301227
)
12311228
qcinp.write_file(os.path.join(os.path.dirname(__file__), "test_e2pert.qin"))
1232-
test_file = open(os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "e2pert.qin"))
1233-
ref_file = open(os.path.join(os.path.dirname(__file__), "test_e2pert.qin"))
1229+
test_path = os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "e2pert.qin")
1230+
ref_path = os.path.join(os.path.dirname(__file__), "test_e2pert.qin")
12341231

1235-
for l_test, l_ref in zip(test_file, ref_file):
1236-
assert l_test == l_ref
1232+
with open(ref_path) as ref_file, open(test_path) as test_file:
1233+
for l_test, l_ref in zip(test_file, ref_file):
1234+
assert l_test == l_ref
12371235

1238-
test_file.close()
1239-
ref_file.close()
12401236
os.remove(os.path.join(os.path.dirname(__file__), "test_e2pert.qin"))
12411237

12421238
def test_read_write_custom_smd(self):
12431239
qcinp = QCInput.from_file(
12441240
os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "custom_smd.qin")
12451241
)
12461242
qcinp.write_file(os.path.join(os.path.dirname(__file__), "test_custom_smd.qin"))
1247-
test_file = open(os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "custom_smd.qin"))
1248-
ref_file = open(os.path.join(os.path.dirname(__file__), "test_custom_smd.qin"))
1243+
test_path = os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "custom_smd.qin")
1244+
ref_path = os.path.join(os.path.dirname(__file__), "test_custom_smd.qin")
12491245

1250-
for l_test, l_ref in zip(test_file, ref_file):
1251-
assert l_test == l_ref
1246+
with open(ref_path) as ref_file, open(test_path) as test_file:
1247+
for l_test, l_ref in zip(test_file, ref_file):
1248+
assert l_test == l_ref
12521249

1253-
test_file.close()
1254-
ref_file.close()
12551250
os.remove(os.path.join(os.path.dirname(__file__), "test_custom_smd.qin"))
12561251

12571252

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ ignore = [
8383
"RET501", # unnecessary-return-none
8484
"RET504", # unnecessary-assign
8585
"SIM105", # Use contextlib.suppress(OSError) instead of try-except-pass
86-
"SIM115", # Use context handler for opening files
8786
]
8887
pydocstyle.convention = "google"
8988
isort.required-imports = ["from __future__ import annotations"]

0 commit comments

Comments
 (0)