Skip to content

Commit 95f57ff

Browse files
authored
core.Lattice cast pbc to bool (possibly np.bool_) (#4356)
* cast to bool * update test
1 parent bf2cd24 commit 95f57ff

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/pymatgen/core/lattice.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from collections import defaultdict
1212
from fractions import Fraction
1313
from functools import reduce
14-
from typing import TYPE_CHECKING, cast
14+
from typing import TYPE_CHECKING
1515

1616
import numpy as np
1717
from monty.dev import deprecated
@@ -183,7 +183,7 @@ def pbc(self, pbc: PbcLike) -> None:
183183
if len(pbc) != 3 or any(item not in {True, False} for item in pbc):
184184
raise ValueError(f"pbc must be a tuple of three True/False values, got {pbc}")
185185

186-
self._pbc = cast("tuple[bool, bool, bool]", tuple(pbc))
186+
self._pbc = tuple(bool(item) for item in pbc)
187187

188188
@property
189189
def is_3d_periodic(self) -> bool:

tests/core/test_structure.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,11 @@ def test_to_from_ase_atoms(self):
964964
assert isinstance(atoms, Atoms)
965965
assert len(atoms) == len(self.struct)
966966

967-
assert AseAtomsAdaptor.get_structure(atoms) == self.struct
967+
structure = AseAtomsAdaptor.get_structure(atoms)
968+
assert structure == self.struct
969+
970+
# Ensure PBC is `bool` type (not `np.bool_`) and JSON serializable
971+
assert "pymatgen.core.structure" in structure.to(fmt="json")
968972

969973
assert IStructure.from_ase_atoms(atoms) == self.struct
970974
assert type(IStructure.from_ase_atoms(atoms)) is IStructure

0 commit comments

Comments
 (0)