Skip to content

Commit c2a616c

Browse files
committed
restore notice that was unintentionally removed
1 parent 5511cc9 commit c2a616c

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

Diff for: src/modelbench/hazards.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ def reload(self):
172172
self.metadata = contents.get("_metadata", {})
173173
self.data = contents.get("standards", {})
174174

175-
def save(self):
175+
def save(self, generated_by: str = ""):
176+
if not generated_by:
177+
generated_by = self.__class__.__name__
178+
notice = f"This file is auto-generated by {generated_by}; avoid editing it manually."
179+
self.metadata = {"NOTICE": notice, "run_info": self.metadata.get("run_info", [])}
180+
176181
with open(self.path, "w") as of:
177182
contents = {"_metadata": self.metadata, "standards": self.data}
178183
json.dump(contents, of, indent=4)

Diff for: src/modelbench/run.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def update_standards_to(standards_file):
366366
}
367367
new_standards.data = new_standard_data
368368
new_standards.append_run_info(new_run_info)
369-
new_standards.save()
369+
new_standards.save(generated_by=sys.argv[0])
370370
STANDARDS = new_standards
371371

372372

Diff for: tests/modelbench_tests/test_standards.py

+28-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import pathlib
23

34
import pytest
@@ -41,6 +42,7 @@ def test_reload(standard):
4142
assert isinstance(standard.data, dict)
4243
assert isinstance(standard.metadata, dict)
4344
assert isinstance(standard.metadata["run_info"], dict)
45+
assert standard.metadata["NOTICE"] == "THIS FILE IS ONLY USED IN UNIT TESTS. THE NUMBERS ARE FAKE."
4446

4547

4648
def test_append_run_info(standard, extra1, extra2):
@@ -54,28 +56,32 @@ def test_append_run_info(standard, extra1, extra2):
5456
assert len(standard.metadata["run_info"]) == 3
5557

5658

57-
def test_save(standard, extra1, extra2, tmp_path):
59+
def test_save(standard, tmp_path):
5860
assert isinstance(standard.metadata["run_info"], dict)
59-
file_path = tmp_path / "new_standard.json"
60-
61-
# make sure saving doesn't change the contents
62-
new_standard = Standards(file_path, auto_load=False)
61+
new_file_path = tmp_path / "new_standard_1.json"
62+
new_standard = Standards(new_file_path, auto_load=False)
6363
new_standard.data = standard.data
6464
new_standard.metadata = standard.metadata
65-
new_standard.save()
66-
assert pathlib.Path.exists(file_path)
67-
68-
newer_standard = Standards(file_path)
69-
assert newer_standard.data == standard.data
70-
assert newer_standard.metadata == standard.metadata
71-
72-
newer_standard.append_run_info(extra1)
73-
newer_standard.append_run_info(extra2)
74-
newer_standard.save()
75-
newer_standard.reload()
76-
77-
assert isinstance(newer_standard.metadata["run_info"], list)
78-
assert len(newer_standard.metadata["run_info"]) == 3
79-
assert newer_standard.metadata["run_info"][0] == standard.metadata["run_info"]
80-
assert newer_standard.metadata["run_info"][1] == extra1
81-
assert newer_standard.metadata["run_info"][2] == extra2
65+
new_standard.save("pytest")
66+
assert pathlib.Path.exists(new_file_path)
67+
new_standard.reload()
68+
assert new_standard.metadata["NOTICE"] == "This file is auto-generated by pytest; avoid editing it manually."
69+
70+
71+
def test_save_notice(standard, extra1, extra2, tmp_path):
72+
new_standard = copy.deepcopy(standard)
73+
new_standard.path = tmp_path / "new_standard_2.json"
74+
assert new_standard.data == standard.data
75+
assert new_standard.metadata == standard.metadata
76+
77+
new_standard.append_run_info(extra1)
78+
new_standard.append_run_info(extra2)
79+
new_standard.save("pytest again")
80+
new_standard.reload()
81+
82+
assert isinstance(new_standard.metadata["run_info"], list)
83+
assert new_standard.metadata["NOTICE"] == "This file is auto-generated by pytest again; avoid editing it manually."
84+
assert len(new_standard.metadata["run_info"]) == 3
85+
assert new_standard.metadata["run_info"][0] == standard.metadata["run_info"]
86+
assert new_standard.metadata["run_info"][1] == extra1
87+
assert new_standard.metadata["run_info"][2] == extra2

0 commit comments

Comments
 (0)