Skip to content

Commit 26f7cd3

Browse files
authored
Add material_id: str | int to PhononBSDOSDoc schema (#669)
* add material_id: str | int to PhononBSDOSDoc schema * set extra="allow" on PhononBSDOSDoc, drop material_id
1 parent da8b754 commit 26f7cd3

File tree

4 files changed

+18
-38
lines changed

4 files changed

+18
-38
lines changed

src/atomate2/common/schemas/phonons.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ class PhononJobDirs(BaseModel):
100100
)
101101

102102

103-
class PhononBSDOSDoc(StructureMetadata):
103+
class PhononBSDOSDoc(StructureMetadata, extra="allow"): # type: ignore[call-arg]
104104
"""Collection of all data produced by the phonon workflow."""
105105

106106
structure: Optional[Structure] = Field(
107-
None,
108-
description="Structure of Materials Project.",
107+
None, description="Structure of Materials Project."
109108
)
110109

111110
phonon_bandstructure: Optional[PhononBandStructureSymmLine] = Field(

tests/common/schemas/test_elastic.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@ def test_elastic_document(test_dir):
1616
schema_ref = json.loads(schema_path.read_text())
1717

1818
doc = ElasticDocument(**schema_ref)
19-
ElasticDocument.model_validate_json(json.dumps(doc, cls=MontyEncoder))
19+
validated = ElasticDocument.model_validate_json(json.dumps(doc, cls=MontyEncoder))
20+
assert isinstance(validated, ElasticDocument)
2021

2122

2223
# schemas where all fields have default values
2324
@pytest.mark.parametrize(
2425
"model_cls",
25-
[
26-
ElasticDocument,
27-
ElasticTensorDocument,
28-
DerivedProperties,
29-
FittingData,
30-
],
26+
[ElasticDocument, ElasticTensorDocument, DerivedProperties, FittingData],
3127
)
3228
def test_model_validate(model_cls):
3329
model_cls.model_validate_json(json.dumps(model_cls(), cls=MontyEncoder))

tests/common/schemas/test_phonons.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,18 @@ def test_phonon_bs_dos_doc():
4040
validated = PhononBSDOSDoc.model_validate_json(json.dumps(doc, cls=MontyEncoder))
4141
assert isinstance(validated, PhononBSDOSDoc)
4242

43+
# test invalid supercell_matrix type fails
4344
with pytest.raises(ValidationError):
4445
doc = PhononBSDOSDoc(**kwargs | {"supercell_matrix": (1, 1, 1)})
4546

47+
# test optional material_id
48+
doc = PhononBSDOSDoc(**kwargs | {"material_id": 1234})
49+
assert doc.material_id == 1234
50+
51+
# test extra="allow" option
52+
doc = PhononBSDOSDoc(**kwargs | {"extra_field": "test"})
53+
assert doc.extra_field == "test"
54+
4655

4756
# schemas where all fields have default values
4857
@pytest.mark.parametrize("model_cls", [PhononJobDirs, PhononUUIDs])

tests/vasp/flows/test_phonons.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -180,44 +180,20 @@ def test_phonon_wf_only_displacements_no_structural_transformation(
180180

181181
assert_allclose(
182182
responses[job.jobs[-1].uuid][1].output.free_energies,
183-
[
184-
5774.56699647,
185-
5616.29786373,
186-
4724.73684926,
187-
3044.19341280,
188-
696.34353154,
189-
],
183+
[5774.56699647, 5616.29786373, 4724.73684926, 3044.19341280, 696.34353154],
190184
)
191185
assert_allclose(
192186
responses[job.jobs[-1].uuid][1].output.entropies,
193-
[
194-
0.0,
195-
4.78666294,
196-
13.02533234,
197-
20.36075467,
198-
26.39807246,
199-
],
187+
[0.0, 4.78666294, 13.02533234, 20.36075467, 26.39807246],
200188
)
201189
assert_allclose(
202190
responses[job.jobs[-1].uuid][1].output.heat_capacities,
203-
[
204-
0.0,
205-
8.04749769,
206-
15.97101906,
207-
19.97032648,
208-
21.87475268,
209-
],
191+
[0.0, 8.04749769, 15.97101906, 19.97032648, 21.87475268],
210192
)
211193

212194
assert_allclose(
213195
responses[job.jobs[-1].uuid][1].output.internal_energies,
214-
[
215-
5774.56699647,
216-
6094.96415750,
217-
7329.80331668,
218-
9152.41981241,
219-
11255.57251541,
220-
],
196+
[5774.56699647, 6094.96415750, 7329.80331668, 9152.41981241, 11255.57251541],
221197
)
222198

223199
assert isinstance(

0 commit comments

Comments
 (0)